<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import numpy as np <EOL> try : <EOL> from sklearn . discriminant_analysis import LinearDiscriminantAnalysis as LDA <EOL> except ImportError : <EOL> from sklearn . lda import LDA <EOL> from sklearn . cross_validation import KFold <EOL> from sklearn . metrics import confusion_matrix <EOL> import scot <EOL> import scot . xvschema <EOL> import scotdata . motorimagery as midata <EOL> raweeg = midata . eeg . T <EOL> triggers = np . asarray ( midata . triggers , dtype = int ) <EOL> classes = midata . classes <EOL> fs = midata . samplerate <EOL> locs = midata . locations <EOL> np . random . seed ( <NUM_LIT> ) <EOL> scot . backend . activate ( '<STR_LIT>' ) <EOL> ws = scot . Workspace ( { '<STR_LIT>' : <NUM_LIT:30> } , reducedim = <NUM_LIT:4> , fs = fs ) <EOL> freq = np . linspace ( <NUM_LIT:0> , fs , ws . nfft_ ) <EOL> data = scot . datatools . cut_segments ( raweeg , triggers , <NUM_LIT:3> * fs , <NUM_LIT:4> * fs ) <EOL> nfolds = <NUM_LIT:10> <EOL> kf = KFold ( len ( triggers ) , n_folds = nfolds ) <EOL> cl = np . unique ( midata . classes ) <EOL> classids = np . array ( [ dict ( zip ( cl , range ( len ( cl ) ) ) ) [ c ] for c in midata . classes ] ) <EOL> lda = LDA ( ) <EOL> cm = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> fold = <NUM_LIT:0> <EOL> for train , test in kf : <EOL> fold += <NUM_LIT:1> <EOL> ws . set_data ( data [ train , : , : ] , classes [ train ] ) <EOL> ws . do_cspvarica ( ) <EOL> ws . var_ . delta = <NUM_LIT:1> <EOL> features = np . zeros ( ( len ( triggers ) , <NUM_LIT:32> ) ) <EOL> for t in range ( len ( triggers ) ) : <EOL> print ( '<STR_LIT>' . format ( fold , nfolds , t ) , <EOL> end = '<STR_LIT:\r>' ) <EOL> ws . set_data ( data [ t , : , : ] ) <EOL> ws . fit_var ( ) <EOL> con = ws . get_connectivity ( '<STR_LIT>' ) <EOL> alpha = np . mean ( con [ : , : , np . logical_and ( <NUM_LIT:7> < freq , freq < <NUM_LIT> ) ] , axis = <NUM_LIT:2> ) <EOL> beta = np . mean ( con [ : , : , np . logical_and ( <NUM_LIT:15> < freq , freq < <NUM_LIT> ) ] , axis = <NUM_LIT:2> ) <EOL> features [ t , : ] = np . array ( [ alpha , beta ] ) . flatten ( ) <EOL> lda . fit ( features [ train , : ] , classids [ train ] ) <EOL> acc_train = lda . score ( features [ train , : ] , classids [ train ] ) <EOL> acc_test = lda . score ( features [ test , : ] , classids [ test ] ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( fold , nfolds , acc_train , acc_test ) ) <EOL> pred = lda . predict ( features [ test , : ] ) <EOL> cm += confusion_matrix ( classids [ test ] , pred ) <EOL> print ( '<STR_LIT>' , cm ) <EOL> print ( '<STR_LIT>' . format ( np . sum ( np . diag ( cm ) ) / np . sum ( cm ) ) ) </s>
