-
Notifications
You must be signed in to change notification settings - Fork 0
/
NN.py
108 lines (88 loc) · 3.13 KB
/
NN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import ConfusionMatrixDisplay
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
seed = 42
df = pd.read_csv("Particle_Data.csv")
df = df.sample(frac=0.5, random_state=seed) # Take only 50% of data for training and testing
# pairplots
sns.pairplot(df.sample(frac=0.5), hue='PID')
plt.savefig("pp_data.png")
#print(df) # first look at the data
Y = df.iloc[:,3]
X = df.iloc[:,0:3]
print("Check for null values: ")
print(df.isna().sum()) # check if anything is missing
#df = df.dropna() # drop any null values in data
print("Statistical Summary of the Data: ")
print(X.describe()) # statistical summary of the variables
print("Class Imbalance: ")
print(df.groupby(Y).size()) # check for class imbalance
# Normalize features within range 0 to 1
sc = MinMaxScaler(feature_range=(0,1))
X = sc.fit_transform(X)
X = pd.DataFrame(X)
# Convert df to array values
X = X.values
Y = Y.values
# Train Test Split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=seed, shuffle=True)
# One Hot Encoding
y_train = pd.get_dummies(y_train)
y_test_t = pd.get_dummies(y_test)
# Build Neural Network
model = Sequential()
model.add(Dense(20, input_dim=3, activation='relu'))
model.add(Dropout(rate=0.10))
model.add(Dense(10, activation='relu'))
# model.add(Dropout(rate=0.10))
model.add(Dense(4, activation='softmax'))
#compile model here
model.compile(loss = 'categorical_crossentropy', optimizer = Adam(learning_rate=0.001), metrics=['accuracy'])
#compile model here
history = model.fit(x_train, y_train, validation_data = (x_test, y_test_t), batch_size=30, epochs=30)
print(history.history.keys)
# Model Summary
summary = model.summary()
print("Model Summary")
print(summary)
# PLOTS !!!
plt.figure()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig("NN_acc.png")
plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig("NN_loss.png")
# Evaluate Model Performance
results = model.evaluate(x_test, y_test_t)
print("RESULTS")
print("Evaluation of model on test data set: ")
print("Test Loss and Test Accuracy", results)
# All about Confusion Matrix
# Generate predictions
y_pred = np.argmax(model.predict(x_test),axis=-1)
ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
plt.title("Confusion Matrix")
plt.savefig("CM_NN.png")
# Predicted Outputs
dic = {"P" : x_test.transpose()[0], "TPC" : x_test.transpose()[1], "TOF": x_test.transpose()[2], "PID" : y_pred}
df = pd.DataFrame(data=dic)
sns.pairplot(df, hue='PID')
plt.savefig("pp_NN.png")
plt.show()