# get MEG and EEG data meg_epochs = epochs.copy().pick_types(meg=True, eeg=False) meg_data = meg_epochs.get_data().reshape(len(labels), -1) ############################################################################### # Decoding in sensor space using a LogisticRegression classifier clf = LogisticRegression(solver='lbfgs') scaler = StandardScaler() # create a linear model with LogisticRegression model = LinearModel(clf) # fit the classifier on MEG data X = scaler.fit_transform(meg_data) model.fit(X, labels) # Extract and plot spatial filters and spatial patterns for name, coef in (('patterns', model.patterns_), ('filters', model.filters_)): # We fitted the linear model onto Z-scored data. To make the filters # interpretable, we must reverse this normalization step coef = scaler.inverse_transform([coef])[0] # The data was vectorized to fit a single model across all time points and # all channels. We thus reshape it: coef = coef.reshape(len(meg_epochs.ch_names), -1) # Plot evoked = EvokedArray(coef, meg_epochs.info, tmin=epochs.tmin) evoked.plot_topomap(title='MEG %s' % name, time_unit='s')
# get MEG and EEG data meg_epochs = epochs.copy().pick_types(meg=True, eeg=False) meg_data = meg_epochs.get_data().reshape(len(labels), -1) ############################################################################### # Decoding in sensor space using a LogisticRegression classifier clf = LogisticRegression() scaler = StandardScaler() # create a linear model with LogisticRegression model = LinearModel(clf) # fit the classifier on MEG data X = scaler.fit_transform(meg_data) model.fit(X, labels) # Extract and plot spatial filters and spatial patterns for name, coef in (('patterns', model.patterns_), ('filters', model.filters_)): # We fitted the linear model onto Z-scored data. To make the filters # interpretable, we must reverse this normalization step coef = scaler.inverse_transform([coef])[0] # The data was vectorized to fit a single model across all time points and # all channels. We thus reshape it: coef = coef.reshape(len(meg_epochs.ch_names), -1) # Plot evoked = EvokedArray(coef, meg_epochs.info, tmin=epochs.tmin) evoked.plot_topomap(title='MEG %s' % name)
for ti, (train_idx, test_idx) in enumerate(cv_obj): # Initialize classifier with params # XXX: kernel must be 'linear' clf = LinearModel( SVC(kernel=SVM_P['kernel'], C=hp_set[0], gamma=hp_set[1], cache_size=SVM_P['cache_size'])) X_train = ss.fit_transform(trial_data[train_idx]) X_test = ss.transform(trial_data[test_idx]) # Train and test classifier, save score clf.fit(X_train, y_labels[train_idx]) temp_score = clf.score(X_test, y_labels[test_idx]) class_scores[hpi[0], hpi[1], hpi[2], ti] = temp_score print 'Test accuracy on CV %i: %0.2f' % (ti, temp_score) # Save results if save_data: date_str = strftime('%Y_%m_%d__%H_%M') save_dict = dict(class_scores=class_scores, hyper_params=hyper_params, hyper_param_desc=hyper_param_desc, subj=s_num, time_finished=date_str) save_dir = op.join(hcp_path, 'classification_results',
# Get the epoched data (get only the data columns) eeg_data = epochs.get_data().reshape(len(labels), -1) eeg_data = eeg_data[:, 0:epochs.get_data().shape[2] * 1] #eeg_data[labels==2] = erptemplate1[:201,0] #eeg_data[labels==1] = erptemplate1[:201,0] #eeg_data[labels==2] = np.zeros((eeg_data.shape[1],)) #eeg_data[labels==1] = np.ones((eeg_data.shape[1],)) #labels = np.random.permutation(labels) # fit the classifier on MEG data X = scaler.fit_transform(eeg_data) model.fit(X[0:2800], labels[0:2800]) preds = model.predict(X[2800:]) # Classification report target_names = ['nohit', 'hit'] report = classification_report(labels[2800:], preds, target_names=target_names) print(report) cm = confusion_matrix(labels[2800:], preds) print(cm) cm_normalized = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis] acc = (cm[0, 0] + cm[1, 1]) * 1.0 / (np.sum(cm)) # %%
cv_obj = StratifiedKFold(y_labels, n_folds=SVM_P['n_folds'], shuffle=True) for ti, (train_idx, test_idx) in enumerate(cv_obj): # Initialize classifier with params # XXX: kernel must be 'linear' clf = LinearModel(SVC(kernel=SVM_P['kernel'], C=hp_set[0], gamma=hp_set[1], cache_size=SVM_P['cache_size'])) X_train = ss.fit_transform(trial_data[train_idx]) X_test = ss.transform(trial_data[test_idx]) # Train and test classifier, save score clf.fit(X_train, y_labels[train_idx]) temp_score = clf.score(X_test, y_labels[test_idx]) class_scores[hpi[0], hpi[1], hpi[2], ti] = temp_score print 'Test accuracy on CV %i: %0.2f' % (ti, temp_score) # Save results if save_data: date_str = strftime('%Y_%m_%d__%H_%M') save_dict = dict(class_scores=class_scores, hyper_params=hyper_params, hyper_param_desc=hyper_param_desc, subj=s_num, time_finished=date_str) save_dir = op.join(hcp_path, 'classification_results', 'sens_svm%s' % shuffled_add) fname_scores_pkl = op.join(save_dir, s_num + '.pkl')