In [34]:
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import normalize
import numpy as np
import pandas as pd
In [32]:
data=pd.read_csv("eeg_data.csv")
data.head()
Out[32]:
In [41]:
Norm_feature=normalize(data.loc[:,data.columns != '1'], norm='l2', axis=1, copy=True)
labels=np.array(data.loc[:,data.columns == '1'])
In [123]:
print "Total feature ", len(Norm_feature[1])
print "Total data ", len(Norm_feature),len(labels)
In [95]:
train_x=np.concatenate((Norm_feature[:730],Norm_feature[800:2061],Norm_feature[2150:]),axis=0)
train_y=np.concatenate((labels[:730],labels[800:2061],labels[2150:]),axis=0)
test_x=np.concatenate((Norm_feature[700:801],Norm_feature[2060:2150]),axis=0)
test_y=np.concatenate((labels[700:801],labels[2060:2150]))
In [96]:
print "training data",len(train_x)
print "testing data",len(test_x)
In [97]:
# clsf = classifier
clsf=SVC(kernel='rbf',gamma=.8,C=4) #SVM Classier
svm={} #store accuracy data for plotting
In [98]:
#training
clsf.fit(train_x, train_y)
Out[98]:
In [99]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clsf.predict(train_x)
svm["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',svm["trainig_set"],'%'
In [100]:
prediction = clsf.predict(test_x)
svm["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',svm["test_set"],'%'
In [101]:
svm
Out[101]:
In [102]:
decision={}
clf=DecisionTreeClassifier(max_depth=10,min_samples_split=4)
clf
Out[102]:
In [103]:
#train
clf = clf.fit(train_x, train_y)
In [104]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clf.predict(train_x)
decision["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',decision["trainig_set"],'%'
In [105]:
prediction = clf.predict(test_x)
decision["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',decision["test_set"],'%'
In [106]:
decision
Out[106]:
In [108]:
from sklearn.neural_network import MLPClassifier
In [109]:
neural=dict()
clf = MLPClassifier(algorithm='l-bfgs',hidden_layer_sizes=(50,), alpha=1e-5, random_state=1)
clf
Out[109]:
In [110]:
#trainig Network
clf.fit(train_x, train_y)
Out[110]:
In [112]:
#Now testing for trainig data
#Now predict values for given classifier
prediction = clf.predict(train_x)
neural["trainig_set"]=accuracy_score(prediction,train_y)*100
print 'Accuracy Check ',neural["trainig_set"],'%'
In [114]:
prediction = clf.predict(test_x)
neural["test_set"]=accuracy_score(prediction,test_y)*100
print 'Accuracy Check ',neural["test_set"],'%'
In [121]:
import pylab as plt
plt.rcParams['figure.figsize'] = 16, 12
In [122]:
#%pylab inline
classifier = [1,2,3]
accuracy_test_data=[svm["test_set"],decision["test_set"],neural["test_set"]]
LABELS=["SVM","Decision-Tree","Neural-net"]
plt.bar(classifier,accuracy_test_data,align='center')
plt.xticks(classifier, LABELS) #binding label with x axis data
plt.xlabel('Classifier')
plt.ylabel('Accuracy')
plt.title('Analysis with 2 Class')
plt.show()
In [ ]: