def nf_select(nf): fselector = mvpa2.FixedNElementTailSelector(np.round(nf), tail='upper', mode='select', sort=False) sbfs = mvpa2.SensitivityBasedFeatureSelection( mvpa2.OneWayAnova(), fselector, enable_ca=['sensitivities'], auto_train=True) if (optimize == 1): not_test_ds = ds[ds.chunks != chunk] train_ds = not_test_ds[not_test_ds.chunks != val_chunk] sbfs.train(train_ds) ds2 = sbfs( not_test_ds ) #optimize nf & include validation set for computing dists elif (optimize == 0): train_ds = ds[ ds.chunks != chunk] #retrain with all data if not optimizing sbfs.train(train_ds) ds2 = sbfs( ds ) #pick top features with training & use whole dataset for computing dists return ds2
def nf_select(nf): fselector = mvpa2.FixedNElementTailSelector(np.round(nf), tail='upper', mode='select', sort=False) sbfs = mvpa2.SensitivityBasedFeatureSelection( mvpa2.OneWayAnova(), fselector, enable_ca=['sensitivities'], auto_train=True) if (optimize >= 1): not_test_ds = ds[ds.chunks != chunk] val_ds = not_test_ds[not_test_ds.chunks == val_chunk] train_ds = not_test_ds[not_test_ds.chunks != val_chunk] sbfs.train(train_ds) train_ds = sbfs(train_ds) val_ds = sbfs(val_ds) return train_ds, val_ds elif (optimize == 0): train_ds = ds[ds.chunks != chunk] test_ds = ds[ds.chunks == chunk] sbfs.train(train_ds) train_ds = sbfs(train_ds) test_ds = sbfs(test_ds) return train_ds, test_ds
def nf_select(nf): #this function is for selecting the number of voxels (i.e., voxels = features) #probably overkill to wrap twice fselector = mvpa2.FixedNElementTailSelector(np.round(nf), tail='upper',mode='select', sort=False) sbfs = mvpa2.SensitivityBasedFeatureSelection(mvpa2.OneWayAnova(), fselector, enable_ca=['sensitivities'], auto_train=True) if(optimize>=1): not_test_ds = ds[ds.chunks!=chunk] val_ds = not_test_ds[not_test_ds.chunks==val_chunk] train_ds = not_test_ds[not_test_ds.chunks!=val_chunk] sbfs.train(train_ds) train_ds = sbfs(train_ds) val_ds = sbfs(val_ds) return train_ds, val_ds; elif(optimize==0): train_ds = ds[ds.chunks!=chunk] test_ds = ds[ds.chunks==chunk] sbfs.train(train_ds) train_ds = sbfs(train_ds) test_ds = sbfs(test_ds) return train_ds, test_ds;
# REDUCE TO CLASS LABELS, AND ONLY KEEP CONDITIONS OF INTEREST (KEEP VS SWITCH) dataset.targets = [t[0] for t in dataset.targets] dataset = dataset[N.array([l in ['k', 's'] for l in dataset.sa.targets], dtype='bool')] print '... and only', dataset.shape[ 0], 'cases of interest (Keep vs Switch Language)' dataset = M.datasets.miscfx.remove_invariant_features(dataset) print 'saving as compressed file', trimmedCache pickleFile = gzip.open(trimmedCache, 'wb', 5) pickle.dump(dataset, pickleFile) anovaSelectedSMLR = M.FeatureSelectionClassifier( M.PLR(), M.SensitivityBasedFeatureSelection( M.OneWayAnova(), M.FixedNElementTailSelector(500, mode='select', tail='upper')), ) foldwiseCvedAnovaSelectedSMLR = M.CrossValidation( anovaSelectedSMLR, M.NFoldPartitioner(), enable_ca=['samples_error', 'stats', 'calling_time', 'confusion']) # run classifier print 'learning on detrended, normalised, averaged, Keep vs Switch ...', datetime.datetime.now( ) results = foldwiseCvedAnovaSelectedSMLR(dataset) print '... done', datetime.datetime.now() print 'accuracy', N.round(100 - N.mean(results) * 100, 1), '%', datetime.datetime.now() #New lines for out putting the result into a csv file.