Skip to content

Commit

Permalink
Added code for checking accuracy for 2Hz frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
iankurgarg committed Apr 24, 2017
1 parent 7be9c8d commit b170885
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
20 changes: 16 additions & 4 deletions python/feedforward_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from keras.utils import np_utils
#from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
from linearDiscriminantAnalysis import mergePredictions


# fix random seed for reproducibility
Expand Down Expand Up @@ -48,15 +49,19 @@ def saveModel(model, path):
print 'Model saved'
#model.save('../trained_models/ff_nn_3.h5')

def predictFromModel(model, testX, testY, classes):
def predictFromModel(model, testX, classes):
testX = testX.values.astype(float)
testY = testY.values
predictions = model.predict(testX)
#print predictions
predictions = np.argmax(predictions,axis=1)
p = classes[predictions]

return p


def calcAccuracy(testY, p):
# print the accuracy
testY = testY.values
accuracy = sum(testY == p)/float(len(testY))
return accuracy

Expand All @@ -79,14 +84,21 @@ def runForSubject(subject, savepath, load=False):
print 'Using existing model'
model = load_model(savepath)

print 'Training accuracy = ' + str(predictFromModel(model, X, Y, classes))
p = predictFromModel(model, X, classes)

print 'Training accuracy = ' + str(calcAccuracy(Y, p))

test_data = pd.read_csv('../data/testing/pca_data_v2/pca_subject'+str(subject)+'.csv')
actual = pd.read_csv('../data/testing/ActualLables/labels_subject'+str(subject)+'_psd.csv', header=None)
actual8 = pd.read_csv('../data/testing/ActualLables/labels8_subject'+str(subject)+'_psd.csv', header=None)
#only get the values from the actual dataframe
actual = actual[0]
actual8 = actual8[0]

print 'Testing Accuracy = ' + str(predictFromModel(model, test_data, actual, classes))
p = predictFromModel(model, test_data, classes)
p8 = mergePredictions(p, 8)
print 'Testing Accuracy = ' + str(calcAccuracy(actual, p))
print 'Testing Accuracy for step 8 = ' + str(calcAccuracy(actual8, p8))


if __name__ == '__main__':
Expand Down
71 changes: 50 additions & 21 deletions python/linearDiscriminantAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
import sys
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.model_selection import cross_val_score
from scipy.stats import mode


def LDA(input_data, test, actual):
def LDA(input_data, test, actual, actual8):
model = LinearDiscriminantAnalysis()
scores = cross_val_score(model, input_data.iloc[:,:-1], input_data.iloc[:,-1], cv = 10)
print 'Cross Validation Accuracy = ' + str(scores.mean())
model.fit(input_data.iloc[:,:-1], input_data.iloc[:,-1])
pred = model.predict(test)
accuracy = sum(pred == actual)/float(len(actual))
pred8 = mergePredictions(pred, 8)
accuracy8 = sum(pred8 == actual8)/float(len(actual8))
print 'Test Accuracy for the subject is = ' + str(accuracy)
print 'Test Accuracy for the subject at step 8 is = ' + str(accuracy8)


def mergePredictions(predictions, step=8):
L = len(predictions)/step
output = []
for i in range(L):
m = mode(predictions[range(i*step, (i+1)*step)])[0][0]
output.append(m)
return output


def mainRawData():
Expand All @@ -35,29 +48,36 @@ def mainRawData():
#Loading Test data (all subjects)
test_s1 = pd.read_csv('../data/testing/test_subject1_psd04.csv', header=None)
actual_s1 = pd.read_csv('../data/testing/ActualLables/labels_subject1_psd.csv', header=None)
actual8_s1 = pd.read_csv('../data/testing/ActualLables/labels8_subject1_psd.csv', header=None)
actual_s1 = actual_s1[0]
actual8_s1 = actual8_s1[0]

test_s2 = pd.read_csv('../data/testing/test_subject2_psd04.csv', header=None)
actual_s2 = pd.read_csv('../data/testing/ActualLables/labels_subject2_psd.csv', header=None)
actual8_s2 = pd.read_csv('../data/testing/ActualLables/labels8_subject2_psd.csv', header=None)
actual_s2 = actual_s2[0]
actual8_s2 = actual8_s2[0]

test_s3 = pd.read_csv('../data/testing/test_subject3_psd04.csv', header=None)
actual_s3 = pd.read_csv('../data/testing/ActualLables/labels_subject3_psd.csv', header=None)
actual8_s3 = pd.read_csv('../data/testing/ActualLables/labels8_subject3_psd.csv', header=None)
actual_s3 = actual_s3[0]
actual8_s3 = actual8_s3[0]

LDA(input_data_s1, test_s1, actual_s1)
LDA(input_data_s2, test_s2, actual_s2)
LDA(input_data_s3, test_s3, actual_s3)
LDA(input_data_s1, test_s1, actual_s1, actual8_s1)
LDA(input_data_s2, test_s2, actual_s2, actual8_s2)
LDA(input_data_s3, test_s3, actual_s3, actual8_s3)

# Results:
#Cross Validation Accuracy = 0.69484335286
#Test Accuracy for the subject is = 0.714611872146
#
#Cross Validation Accuracy = 0.539059371463
#Test Accuracy for the subject is = 0.581221198157
#
#Cross Validation Accuracy = 0.461690546146
#Test Accuracy for the subject is = 0.491685779817
# Cross Validation Accuracy = 0.69484335286
# Test Accuracy for the subject is = 0.714611872146
# Test Accuracy for the subject at step 8 is = 0.728310502283
# Cross Validation Accuracy = 0.539059371463
# Test Accuracy for the subject is = 0.581221198157
# Test Accuracy for the subject at step 8 is = 0.582949308756
# Cross Validation Accuracy = 0.461690546146
# Test Accuracy for the subject is = 0.491685779817
# Test Accuracy for the subject at step 8 is = 0.502293577982

def mainPCAData():
input_data_s1 = pd.read_csv('../data/training/pca_data_v2/pca_subject1.csv')
Expand All @@ -69,27 +89,36 @@ def mainPCAData():
#Loading Test data (all subjects)
test_s1 = pd.read_csv('../data/testing/pca_data_v2/pca_subject1.csv')
actual_s1 = pd.read_csv('../data/testing/ActualLables/labels_subject1_psd.csv', header=None)
actual8_s1 = pd.read_csv('../data/testing/ActualLables/labels8_subject1_psd.csv', header=None)
actual_s1 = actual_s1[0]
actual8_s1 = actual8_s1[0]

test_s2 = pd.read_csv('../data/testing/pca_data_v2/pca_subject2.csv')
actual_s2 = pd.read_csv('../data/testing/ActualLables/labels_subject2_psd.csv', header=None)
actual8_s2 = pd.read_csv('../data/testing/ActualLables/labels8_subject2_psd.csv', header=None)
actual_s2 = actual_s2[0]
actual8_s2 = actual8_s2[0]

test_s3 = pd.read_csv('../data/testing/pca_data_v2/pca_subject3.csv')
actual_s3 = pd.read_csv('../data/testing/ActualLables/labels_subject3_psd.csv', header=None)
actual8_s3 = pd.read_csv('../data/testing/ActualLables/labels8_subject3_psd.csv', header=None)
actual_s3 = actual_s3[0]
actual8_s3 = actual8_s3[0]

LDA(input_data_s1, test_s1, actual_s1)
LDA(input_data_s2, test_s2, actual_s2)
LDA(input_data_s3, test_s3, actual_s3)
LDA(input_data_s1, test_s1, actual_s1, actual8_s1)
LDA(input_data_s2, test_s2, actual_s2, actual8_s2)
LDA(input_data_s3, test_s3, actual_s3, actual8_s3)

# Results:
#Cross Validation Accuracy = 0.719154247244
#Test Accuracy for the subject is = 0.732591324201
#Cross Validation Accuracy = 0.565501871535
#Test Accuracy for the subject is = 0.613479262673
#Cross Validation Accuracy = 0.482587359569
#Test Accuracy for the subject is = 0.505447247706
# Cross Validation Accuracy = 0.719154247244
# Test Accuracy for the subject is = 0.732591324201
# Test Accuracy for the subject at step 8 is = 0.748858447489
# Cross Validation Accuracy = 0.565501871535
# Test Accuracy for the subject is = 0.613479262673
# Test Accuracy for the subject at step 8 is = 0.624423963134
# Cross Validation Accuracy = 0.482587359569
# Test Accuracy for the subject is = 0.505447247706
# Test Accuracy for the subject at step 8 is = 0.504587155963

if __name__ == '__main__':
mainPCAData()

0 comments on commit b170885

Please sign in to comment.