Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Dense
- from sklearn.metrics import confusion_matrix
- import matplotlib.pyplot as plt
- import seaborn as sns
- # Training data
- train_data = np.array([
- [332, 1], [289, 1], [250, 1], [91, 1], [-91, 1], [-206, 1], [-206, 1],
- [-228, 1], [-239, 1], [-256, 1], [-266, 1], [-278, 1], [-279, 1],
- [-289, 1], [-297, 1], [-297, 1], [-311, 1], [-312, 1], [-324, 1],
- [-348, 1], [-396, 1], [-509, 1], [-521, 1], [-526, 1], [-550, 1],
- [-606, 1], [-633, 1], [-728, 1], [550, 0], [385, 0], [341, 0], [289, 0],
- [228, 0], [137, 0], [110, 0], [75, 0], [-33, 0], [-70, 0], [-226, 0],
- [-251, 0], [-263, 0], [-273, 0], [-280, 0], [-390, 0], [-440, 0],
- [792, 0], [757, 0], [718, 0], [591, 0], [585, 0], [578, 0], [524, 0],
- [512, 0], [463, 0], [455, 0], [431, 0], [421, 0], [396, 0], [387, 0],
- [386, 0], [379, 0], [370, 0], [361, 0], [341, 0], [339, 0], [338, 0],
- [328, 0], [323, 0], [307, 0], [303, 0], [288, 0], [284, 0], [282, 0],
- [277, 0], [232, 0], [164, 0], [131, 0], [108, 0], [35, 0], [8, 0],
- [-87, 0], [-228, 0], [-230, 0], [-276, 0], [-347, 0], [-367, 0],
- [-543, 0], [294, 1], [284, 1], [104, 1], [-186, 1], [-238, 1], [-262, 1],
- [-280, 1], [-325, 1], [-326, 1], [-384, 1], [-422, 1], [-498, 1],
- [390, 0], [266, 0], [-297, 0], [-331, 0], [585, 0], [531, 0], [520, 0],
- [465, 0], [251, 0], [133, 0], [55, 0], [-228, 0], [-248, 0], [-306, 0]
- ])
- # Test data
- test_data = np.array([
- [559, 1], [359, 1], [191, 1], [-62, 1], [-177, 1], [-199, 1],
- [-261, 1], [-265, 1], [-298, 1], [-300, 1], [-301, 1], [-318, 1],
- [-321, 1], [-368, 1], [-393, 1], [-469, 1], [-513, 1], [-584, 1],
- [-586, 1], [-745, 1], [-960, 1], [376, 0], [329, 0], [756, 0], [562, 0],
- [511, 0], [445, 0], [402, 0], [341, 0], [340, 0], [312, 0], [305, 0],
- [264, 0], [-216, 0], [231, 1], [208, 1], [155, 1], [-200, 1], [-258, 1],
- [-364, 1], [-566, 1], [414, 0], [-240, 0], [599, 0], [521, 0], [428, 0],
- [415, 0], [337, 0]
- ])
- # Splitting into inputs (X) and labels (y)
- X_train = train_data[:, 0].reshape(-1, 1)
- y_train = train_data[:, 1]
- X_test = test_data[:, 0].reshape(-1, 1)
- y_test = test_data[:, 1]
- # Define the model
- model = Sequential([
- Dense(10, input_dim=1, activation='relu'),
- Dense(8, activation='relu'),
- Dense(1, activation='sigmoid')
- ])
- # Compile the model
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- # Train the model
- model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=1)
- # Evaluate the model
- _, accuracy = model.evaluate(X_test, y_test)
- print(f'Test accuracy: {accuracy:.2f}')
- # Predict the test set
- y_pred = (model.predict(X_test) > 0.5).astype("int32")
- # Compute confusion matrix
- conf_matrix = confusion_matrix(y_test, y_pred)
- # Plot confusion matrix
- plt.figure(figsize=(10, 7))
- sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')
- plt.title('Confusion Matrix')
- plt.ylabel('Actual Label')
- plt.xlabel('Predicted Label')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement