Binary-Classification / Python / n7point / test_fnn_n7point.py
test_fnn_n7point.py
Raw
from google.colab import files
setdate = files.upload()

# citire fisier .csv
import pandas as pd
dataset_n7point = pd.read_csv('n7point.csv')

dataset_n7point.head()
dataset_n7point.shape

x = dataset_n7point.iloc[:,0:7].values
y = dataset_n7point.iloc[:,7].values

x[1,:]

x.shape, y.shape

# split dataset
from sklearn.model_selection import train_test_split
x_train,x_test, y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=0)

x_train.shape, y_train.shape, x_test.shape, y_test.shape
x_train.shape, x_test.shape

x_train = x_train.reshape(11779, 7, 1)
x_test = x_test.reshape(2945, 7, 1)

from google.colab import drive
drive.mount('/content/drive')

from tensorflow.keras.models import Sequential, model_from_json
# load json and create model
json_file = open('/content/drive/MyDrive/IA_Samples/Doctorat/model1_fnn.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model1 = model_from_json(loaded_model_json)
loaded_model1.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
loaded_model1.summary()

hist_new = loaded_model1.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=100, batch_size=100)

# Commented out IPython magic to ensure Python compatibility.
import matplotlib.pyplot as plt
# %matplotlib inline
import seaborn as sns
sns.set()
 
acc = hist_new.history['accuracy']
val = hist_new.history['val_accuracy']
epochs = range(1, len(acc) + 1)
 
plt.plot(epochs, acc, '-', label='Training accuracy')
plt.plot(epochs, val, ':', label='Validation accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.plot()

# Evaluate the model
test_loss, test_acc = loaded_model1.evaluate(x_test, y_test)

print('Test Accuracy: {}'.format(test_acc))

# Evaluate the model
train_loss, train_acc = loaded_model1.evaluate(x_train, y_train)

print('Train Accuracy: {}'.format(train_acc))

from sklearn.metrics import confusion_matrix
 
y_predicted = loaded_model1.predict(x_test) > 0.5
mat = confusion_matrix(y_test, y_predicted)
labels = ['class 0', 'class 1']
 
sns.heatmap(mat, square=True, annot=True, fmt='d', cbar=False, cmap='Blues',
            xticklabels=labels, yticklabels=labels)
 
plt.xlabel('Predicted label')
plt.ylabel('Actual label')

from sklearn.metrics import classification_report

print(classification_report(y_test, y_predicted))

import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score, roc_curve

def plot_roc_curve(true_y, y_prob):
    """
    plots the roc curve based of the probabilities
    """

    fpr, tpr, thresholds = roc_curve(true_y, y_prob)
    plt.plot(fpr, tpr)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

plot_roc_curve(y_test, y_predicted)
print(f'model FNN AUC score: {roc_auc_score(y_test, y_predicted)}')