def PersonWorker(person): print('starting on person: ', str(person)) #data = 40 videos x 32 alpha(csp channel) X_train, y_train, X_test, y_test, csp = DL.loadPerson(person=person, featureFunc = featureFunc, use_csp=False, use_median = False ) C = 1 clf = LinearSVC(C=C,random_state=40) K_CV = KFold(n=len(X_train), n_folds=len(X_train), random_state=17, shuffle=False) #leave out one validation predictions, truths = [], [] for train_index, CV_index in K_CV: #train clf.fit(X_train[train_index], y_train[train_index]) #predict pred = clf.predict(X_train[CV_index]) #save for metric calculations predictions.extend(pred) truths.extend(y_train[CV_index]) #optimization metric: best_metric = UT.auc(predictions, truths) best_C = C #try other C values for C in [0.01,0.03,0.1,0.3,3,10]: clf = LinearSVC(C=C,random_state=40) K_CV = KFold(n=len(X_train), n_folds=len(X_train), random_state=17, shuffle=True) #leave out one validation predictions, truths = [], [] for train_index, CV_index in K_CV: #train clf.fit(X_train[train_index], y_train[train_index]) #predict pred = clf.predict(X_train[CV_index]) #save for metric calculations predictions.extend(pred) truths.extend(y_train[CV_index]) #optimization metric: metric = UT.auc(predictions, truths) if metric > best_metric: best_metric = metric best_C = C #C param is now optimized, its value is stored in best_C #calculate all performance metrics on testset, using the optimal classifier clf = LinearSVC(C=C,random_state=40) clf.fit(X_train,y_train) #fit all training data #print("coef ", clf.coef_) predictions = clf.predict(X_test) acc = UT.accuracy(predictions, y_test) (tpr,tnr,fpr,fnr) = UT.tprtnrfprfnr(predictions, y_test) auc = UT.auc(predictions, y_test) print('person: ', person, ' - acc: ', str(acc), ' - tpr: ' , str(tpr), ' - tnr: ' , str(tnr), ' - auc: ', str(auc) ) return [acc,tpr,tnr,fpr,fnr,auc]
def PersonWorker(person): print('starting on person: ', str(person)) #data = 40 videos x 32 alpha(csp channel) X_train, y_train, X_test, y_test, csp = DL.loadPerson(person=person, featureFunc = featureFunc, use_median=False, use_csp=True, prefilter=False ) #store weights of upper CSP channel for topoplots csp.write_filters() #optimize channelPairs with leave-one out validation #prior probabilities pos_prior = np.sum(y_train) neg_prior = 40 - pos_prior pos_prior /= float(40) neg_prior /= float(40) #academic loop start with 1 channelPair channelPairs = 1 #filter out the channel pairs X = np.zeros((len(X_train),channelPairs * 2,)) top_offset = channelPairs * 2 - 1 for j, k in zip(range(channelPairs), range(31,31-channelPairs,-1)): X[:,j] = X_train[:,j] X[:,top_offset -j] = X_train[:,k] #LDA lda = LinearDiscriminantAnalysis(priors=[neg_prior, pos_prior]) K_CV = KFold(n=len(X), n_folds=len(X), random_state=17, shuffle=False) #leave out one validation predictions, truths = [], [] for train_index, CV_index in K_CV: #train lda = lda.fit(X[train_index], y_train[train_index]) #predict pred = lda.predict(X[CV_index]) #save for metric calculations predictions.extend(pred) truths.extend(y_train[CV_index]) #optimization metric: best_metric = UT.accuracy(predictions, truths) best_channelPairs = channelPairs #try other channel pairs for channelPairs in range(2,17): #filter out the channel pairs X = np.zeros((len(X_train),channelPairs * 2,)) top_offset = channelPairs * 2 - 1 for j, k in zip(range(channelPairs), range(31,31-channelPairs,-1)): X[:,j] = X_train[:,j] X[:,top_offset -j] = X_train[:,k] #LDA lda = LinearDiscriminantAnalysis(priors=[neg_prior, pos_prior]) K_CV = KFold(n=len(X), n_folds=len(X), random_state=17, shuffle=True) #leave out one validation predictions, truths = [], [] for train_index, CV_index in K_CV: #train lda = lda.fit(X[train_index], y_train[train_index]) #predict pred = lda.predict(X[CV_index]) #save for metric calculations predictions.extend(pred) truths.extend(y_train[CV_index]) #optimization metric: metric = UT.accuracy(predictions, truths) if metric > best_metric: best_metric = metric best_channelPairs = channelPairs #channel pairs are now optimized, its value is stored in best_channelPairs #calculate all performance metrics on testset, using the optimal classifier lda = LinearDiscriminantAnalysis(priors=[neg_prior, pos_prior]) lda = lda.fit(X_train,y_train) #fit all training data predictions = lda.predict(X_test) acc = UT.accuracy(predictions, y_test) (tpr,tnr,fpr,fnr) = UT.tprtnrfprfnr(predictions, y_test) auc = UT.auc(predictions, y_test) print('person: ', person, ' - channelPairs: ', str(best_channelPairs), ' - acc: ', str(acc), ' - tpr: ' , str(tpr), ' - tnr: ' , str(tnr), ' - auc: ', str(auc) ) return [best_channelPairs, acc,tpr,tnr,fpr,fnr,auc]