Skip to content

Instantly share code, notes, and snippets.

@DeepInEvil
Created September 25, 2015 05:07
Show Gist options
  • Save DeepInEvil/719cb0416161535dbbab to your computer and use it in GitHub Desktop.
Save DeepInEvil/719cb0416161535dbbab to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import numpy as np
from sklearn import cross_validation
from sklearn import svm
import pandas as pd
import math as m
from sklearn import preprocessing
from scipy import signal
from sklearn.decomposition import FastICA, PCA
from sklearn.metrics import mean_squared_error as MSE
subjects = range(3, 5) #13)
train_series = range(1, 2) # 9
test_series = range(9, 10) # 11
ica = FastICA(n_components=32)
for subject in subjects:
fname = ('/home/debanjan/EEGTest/subj%d_series1_data.csv' % (subject))
train = pd.read_csv(fname)
fname = ('/home/debanjan/EEGTest/subj%d_series1_events.csv' % (subject))
labels = pd.read_csv(fname)
train = pd.merge(train, labels, on ='id')
idx = train.id.values
train = train.drop(['id'], axis = 1)
columns = train.columns[ :-6 ]
train = np.array(train).astype(float)
labels = np.array(labels)
x_train = train[ :, :-6 ]
print "ica transforming"
x_train = ica.fit_transform(x_train)
y_train = train[ :, 32: ]
colcount = x_train.shape[1]
rowcount = x_train.shape[0]
for i in range(0, colcount):
x_train[:, i] = preprocessing.scale(x_train[:, i])
mean = np.mean(x_train[:, i])
max = np.max(x_train[:, i])
min = np.min(x_train[:, i])
for j in range(0, rowcount):
x_train[j, i] = ( x_train[j, i] - min )/( max - min )
# preprocessing_train_file = ('s_%d_train.csv' % (subject))
# preprocessed = pd.DataFrame(index=idx, columns=columns, data=x_train)
# preprocessed.to_csv(preprocessing_train_file, index_label='id', float_format='%.6f')
#mse = MSE( y, pred_tot )
#rmse = sqrt( mse )
#print "testing RMSE:", rmse
x_train = np.array(x_train).astype(float)
y_train = np.array(y_train).astype(int)
#zeroes = np.array(y_train)
#for i in range(0, 119496):
# for j in range (0, 6):
# zeroes[i, j] = 0
#mse = MSE( zeroes, y_train )
#rmse = m.sqrt( mse )
#print ("zeroes RMSE:", rmse)
#We can now quickly sample a training set while holding out 40% of the data for testing (evaluating) our classifier:
print "Generating test and train set"
X_train, X_test, y_tr, y_te = cross_validation.train_test_split(x_train, y_train, test_size=0.4, random_state=0)
print "Modelling ####"
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier()
clf = knn.fit(x_train, y_train)
#print ( 'KNN train score: ' + str(clf2.score(X_train, y_tr[:,0])) )
#print ( 'test score: ' + str(clf2.score(X_test, y_te[:,0])) )
#Testing with new data
print "Checking accuracy for new data#########"
fname12 = ('/home/debanjan/EEGTest/subj9_series4_data.csv')
train12 = pd.read_csv(fname12)
fname12 = ('/home/debanjan/EEGTest/subj9_series4_events.csv')
labels12 = pd.read_csv(fname12)
train12 = pd.merge(train12, labels12, on ='id')
idx12 = train12.id.values
train12 = train12.drop(['id'], axis = 1)
columns12 = train12.columns[ :-6 ]
train12 = np.array(train12).astype(float)
labels12 = np.array(labels12)
x_train12 = train12[ :, :-6 ]
x_train12 = ica.fit_transform(x_train12)
y_train12 = train12[ :, 32: ]
colcount12 = x_train12.shape[1]
rowcount12 = x_train12.shape[0]
for i in range(0, colcount12):
x_train12[:, i] = preprocessing.scale(x_train12[:, i])
mean = np.mean(x_train12[:, i])
max = np.max(x_train12[:, i])
min = np.min(x_train12[:, i])
for j in range(0, rowcount12):
x_train12[j, i] = ( x_train12[j, i] - min )/( max - min )
# preprocessing_train_file = ('s_%d_train.csv' % (subject))
# preprocessed = pd.DataFrame(index=idx, columns=columns, data=x_train)
# preprocessed.to_csv(preprocessing_train_file, index_label='id', float_format='%.6f')
#mse = MSE( y, pred_tot )
#rmse = sqrt( mse )
#print "testing RMSE:", rmse
x_train12 = np.array(x_train12).astype(float)
y_train12 = np.array(y_train12).astype(int)
print "CHecking accuracy of data for 9th subject: "
print ( 'KNN train score for subject12: ' + str(clf.score(x_train12, y_train12[:,0])) )
#print ( 'test score: ' + str(clf.score(X_test, y_te[:,0])) )
#print ("output for y"+clf.predict(x_train12))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment