-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJetClassification.py
More file actions
320 lines (182 loc) · 8.13 KB
/
JetClassification.py
File metadata and controls
320 lines (182 loc) · 8.13 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils import np_utils # For y values
from keras.models import Sequential
from keras import metrics
from keras.layers.core import Dense, Dropout
from keras import backend as K
import matplotlib.pyplot as plt
#import plotly.plotly as py
import plotly
plotly.offline.init_notebook_mode()
import plotly.offline as py
import plotly.tools as tls
# In[2]:
np.random.seed(123) # for reproducibility
# ## Some Helping Functions
# In[3]:
def print_heatmap(data):
fig = plt.figure()
fig.add_subplot(111)
plotly_fig = tls.mpl_to_plotly(fig)
trace = dict(z=data, type="heatmap")
plotly_fig['data'] = [trace]
plotly_fig['layout']['xaxis'].update({'autorange':True})
plotly_fig['layout']['yaxis'].update({'autorange':True})
py.iplot(plotly_fig, filename='jupyter/heatmap')
# In[4]:
def get_predicted_values(output_values):
predicted_values = []
for probability in output_values:
if probability[0] > probability[1]:
predicted_values.append(0)
else:
predicted_values.append(1)
return predicted_values
# # Reading the data
# First we read the Signal Data and produce a heatmap from the average of all lines. We do so, in order to get the feeling of what our data looks like.
# In[5]:
signal_data = pd.read_table('signal_PU0_13TeV_MJ-65-95_PTJ-250-300_ext.txt', header=None, dtype=np.float32)
print(signal_data.shape)
print(signal_data.iloc[1:10,625])
# As seen, the data read has a last column (no. 626) without data. This happens because the data read has trailing spaces. We need to remove such data.
# In[6]:
signal_data.drop(625, axis=1, inplace=True)
print(signal_data.shape)
# Then, we calculate the average of all observations.
# In[7]:
mean_signal = signal_data.mean()
print(mean_signal.iloc[1:10,])
# Now draw the Heatmap
# In[8]:
mean_signal_matrix = mean_signal.as_matrix()
print(mean_signal_matrix.shape)
mean_signal_matrix = mean_signal_matrix.reshape((25,25))
print_heatmap(mean_signal_matrix)
# We then add a last column, with the value of `1`. This last column classify this data as signal data. After creating the full dataset we will rename this column to _class_.
# In[9]:
signal_data[625] = 1
# In[10]:
signal_data.iloc[0:10,625]
# After preprocessing the _signal_ data, we will perform the same steps with the _background_ data.
# In[11]:
backgroud_data = pd.read_table('background_PU0_13TeV_MJ-65-95_PTJ-250-300_ext.txt', header=None, dtype=np.float32)
print(backgroud_data.shape)
# In[12]:
backgroud_data.drop(625, axis=1, inplace=True)
print(backgroud_data.shape)
# In[13]:
mean_background = backgroud_data.mean()
print(mean_background.iloc[1:10,])
# In[14]:
mean_background_matrix = mean_background.as_matrix()
print(mean_background_matrix.shape)
mean_background_matrix = mean_background_matrix.reshape((25,25))
print_heatmap(mean_background_matrix)
# We modify this data, to point out that this is the background data, by setting the last column to `0`.
# In[15]:
backgroud_data[625] = 0
# In[16]:
backgroud_data.iloc[0:10,625]
# Now we concatenate the data and shuffle it in order to randomize its columns.
# In[17]:
frames = [signal_data, backgroud_data]
full_data = pd.concat(frames)
# In[18]:
full_data = full_data.sample(frac=1).reset_index(drop=True)
full_data.rename(columns={625:'class'}, inplace=True)
# Now let's examine how our data looks like.
# In[19]:
from IPython.core.display import display
with pd.option_context('display.max_rows', 200, 'display.max_columns', 15,
'display.float_format', '{:.2f}'.format):
display(full_data)
# # Defining and Training the model
# Split the full dataset into _test_ and _train_ data in a 70-30% rate.
# In[20]:
train_data, test_data = train_test_split(full_data, test_size = 0.3)
print(train_data.shape)
print(test_data.shape)
# The _Keras_ framework, in order to train its network must receive the dependent and independent variables in separated tables.
# In[21]:
X_train = train_data.iloc[:, :-1]
print(X_train.shape)
Y_train = train_data.iloc[:,-1:]
print(Y_train.shape)
# In[22]:
X_test = test_data.iloc[:, :-1]
print(X_test.shape)
Y_test = test_data.iloc[:,-1:]
print(Y_test.shape)
# We must convert the dependent variable to be a probability distribution of the possible outcomes, thus, a value of output `1` must become the probabilities `(0, 1)`. Conversely, a `0` outcome value must become the pair `(1, 0)`.
# In[23]:
Y_train = Y_train.as_matrix()
Y_train = np_utils.to_categorical(Y_train, num_classes=2)
# ## Neural Network Architecture Definition
# We defined a simple NN, with only two hidden layer, each with 256 neurons and a _Dropout_, discarding 20% of the neurons at each layer. We used a rectified linear unit as the activation function.
# For the training phase we used a categorical crossentropy loss function, that is responsible for calculating how wrong our guess is, compared to the true value.
# The value of the loss function is then used to update the weights in the neurons connections. It uses an optimization function to minimize the loss value at the specific layer. In our example we used the _Adam_ optimizer, a version of the stochastic gradient descent.
# In[24]:
dimof_input = X_train.shape[1]
model = Sequential()
model.add(Dense(units=256, kernel_initializer='uniform', activation='relu', input_dim=dimof_input))
model.add(Dropout(0.2))
model.add(Dense(units=256, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=2, kernel_initializer='uniform', activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=[metrics.categorical_accuracy, metrics.mean_squared_error])
# After defining the NN architecture we train it using the `fit` method. We trained it for 50 epochs (backpropagation cycles).
# In[25]:
model.fit(X_train.as_matrix(), Y_train, batch_size=500, epochs=50, verbose=0)
# After training the model we need to evaluate it.
# In[26]:
Y_test = Y_test.as_matrix()
Y_test = np_utils.to_categorical(Y_test, num_classes=2)
# In[27]:
score = model.evaluate(X_test.as_matrix(), Y_test, verbose=0)
print(model.metrics_names)
print(score)
# Now let's take a look at the missclassified observations.
# In[28]:
Y_test_predicted = model.predict(X_test.as_matrix(), verbose=0)
# In[29]:
Y_test_predicted
# Now we calculate the false negatives and also the false positives by comparing the true value with the predicted one.
# In[30]:
Y_test_predicted_values = get_predicted_values(Y_test_predicted)
Y_test_values = get_predicted_values(Y_test)
print(Y_test_predicted_values[1:10])
print(Y_test_values[1:10])
false_positives = []
false_negatives = []
for i in range(len(Y_test_values)):
if Y_test_values[i] == 0 and Y_test_predicted_values[i] == 1:
false_positives.append(i)
elif Y_test_values[i] == 1 and Y_test_predicted_values[i] == 0:
false_negatives.append(i)
print("False Positive Rate: {:.2f}".format(len(false_positives)/len(Y_test_values)))
print("False Negative Rate: {:.2f}".format(len(false_negatives)/len(Y_test_values)))
# And try to visualize the heatmaps for false positives and negatives. Firstly the false negatives, where the network was supposed to answer _Signal_, but instead, it answered _Background_.
# In[31]:
print(false_negatives[1:10])
false_negatives_values = X_test.iloc[false_negatives,]
print(false_negatives_values.shape)
mean_false_negatives = false_negatives_values.mean().as_matrix()
print(mean_false_negatives.shape)
mean_false_negatives = mean_false_negatives.reshape((25,25))
print_heatmap(mean_false_negatives)
# Then we examine the cases in which the network should have responded _Background_, but it answered _Signal_.
# In[32]:
print(false_positives[1:10])
false_positives_values = X_test.iloc[false_positives,]
print(false_positives_values.shape)
mean_false_positives = false_positives_values.mean().as_matrix()
print(mean_false_positives.shape)
mean_false_positives = mean_false_positives.reshape((25,25))
print_heatmap(mean_false_positives)
# In[33]:
K.clear_session()