Here is simple network to identify Higgs signal using Neural network.
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import csv
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
import random
import seaborn as sns
Filename = "training.csv"
n = sum(1 for line in open(Filename)) - 1
s = 200000 #desired sample size
skip = sorted(random.sample(range(1,n+1),n-s))
df = pd.read_csv(Filename, skiprows=skip)
df = df.replace({'s': 1, 'b': 0})
print(df.head())
corr_df = df.corr(method='pearson')
mask = np.zeros_like(corr_df)
mask[np.triu_indices_from(mask)] = True
plt.figure(figsize=(10,10))
# Create the heatmap using seaborn library.
sns.heatmap(corr_df, cmap='RdYlGn_r', vmax=1.0, vmin=-1.0 , mask = mask, linewidths=2.5)
# The plot is reoriented the labels for each column and row to make them easier to read.
plt.yticks(rotation=0)
plt.xticks(rotation=90)
plt.show()
columns = ['EventId','PRI_tau_phi', 'PRI_lep_phi', 'PRI_met_phi', 'PRI_tau_eta', 'PRI_lep_eta', 'Weight']
df = df.drop(columns, axis=1 )
corr_df = df.corr(method='pearson')
mask = np.zeros_like(corr_df)
mask[np.triu_indices_from(mask)] = True
plt.figure(figsize=(10,10))
sns.heatmap(corr_df, cmap='RdYlGn_r', vmax=1.0, vmin=-1.0 , mask = mask, linewidths=2.5)
plt.yticks(rotation=0)
plt.xticks(rotation=90)
plt.show()
Filename = "training.csv"
n = sum(1 for line in open(Filename)) - 1
s = 10000
skip = sorted(random.sample(range(1,n+1),n-s))
df_test= pd.read_csv(Filename, skiprows=skip)
df_test = df_test.replace({'s': 1, 'b': 0})
df_test = df_test.drop(columns, axis=1 )
X = np.array(df.drop(['Label'], 1))
y = np.array(df['Label'])
y = np.array(df['Label']).reshape(len(df.index),1)
X_train = X
encoder = OneHotEncoder(sparse=False)
y_onehot = encoder.fit_transform(y)
y_train = y_onehot
print ((X_train.shape))
X_test = np.array(df_test.drop(['Label'], 1))
y_test = np.array(df_test['Label'])
y_test = np.array(df_test['Label']).reshape(len(df_test.index),1)
encoder = OneHotEncoder(sparse=False)
y_onehot = encoder.fit_transform(y_test)
y_test = y_onehot
print ((X_test.shape, y_test.shape))
n_nodes_hl1 = 400
n_nodes_hl2 = 400
n_nodes_hl3 = 400
n_classes = 2
batch_size = 1000
n_batches = -(-X_train.shape[0] // batch_size)
print('n_batches =', n_batches)
n_feature = X_test.shape[1]
X = tf.placeholder('float', [None, n_feature])
y = tf.placeholder('float')
print(n_feature)
Simple 3 layered network trained with AdamOptimizer, no dropout
def neural_network_model(data):
hidden_1_layer = {'weights': tf.Variable(tf.random_normal([n_feature,n_nodes_hl1])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
'biases': tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(X, hidden_1_layer['weights']) , hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return(output)
Simple 3 layered network trained with AdamOptimizer, with dropout
def neural_network_model_dropout(data):
hidden_1_layer = {'weights': tf.Variable(tf.random_normal([n_feature,n_nodes_hl1])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
'biases': tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(X, hidden_1_layer['weights']) , hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l1 = tf.nn.dropout(l1, 0.8)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']) , hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l2 = tf.nn.dropout(l2, 0.8)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']) , hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return(output)
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs =501
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
epoch_loss=0
_, c = sess.run([optimizer, cost], feed_dict={x:X_train, y:y_train})
epoch_loss += c
if epoch%10 == 0:
print('Epoch', epoch, 'Out of', hm_epochs, 'loss:', epoch_loss )
correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy:', accuracy.eval({x:X_test, y:y_test}))
def train_neural_network_bd(x):
prediction = neural_network_model_dropout(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs =501
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
epoch_loss=0
for i in range(n_batches):
_, c = sess.run([optimizer, cost], feed_dict={x:X_train[i*batch_size : ((i+1)*batch_size)],
y:y_train[i*batch_size : ((i+1)*batch_size)]})
epoch_loss += c
if epoch%10 == 0:
print('Epoch', epoch, 'Out of', hm_epochs, 'loss:', epoch_loss )
correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy:', accuracy.eval({x:X_test, y:y_test}))
train_neural_network(X)
train_neural_network_bd(X)