Using mne.decoding.GeneralizingEstimator fit() to predict categorical variables

  • MNE version: 1.1.1
  • operating system: macOS 11.6.2

Hi all,

For example, in a vision experiment where subjects are viewing different colors in different trials, epoch1 is for the red color, epoch2 is for the blue color, epoch3 is for the green color, epoch4 is for the yellow color. The goal is to do “Decoding sensor space data with generalization across time and conditions” as in here (Decoding sensor space data with generalization across time and conditions — MNE 1.2.dev0 documentation). In this toy example, I will train and test on the same set, about the color the subject is seeing (of course one should not do it in real data analysis, but just to keep things simple here). What shall I put in the ??? below? I should not put np.concatenate((np.full(50, 1),np.full(50, 2),np.full(50, 3),np.full(50, 4))) there because red, blue, green and yellow are categorical. So I need some way to e.g., turn them into something like one-hot vectors. However, I tried but haven’t figured out how to do it. Could anyone kindly help or suggest how I could do it?

clf = make_pipeline(
StandardScaler(),
LogisticRegression(solver=‘liblinear’) # liblinear is faster than lbfgs
)
time_gen = GeneralizingEstimator(clf, scoring=‘roc_auc’, n_jobs=None,
verbose=True)
epochs1234 = mne.concatenate_epochs([epochs1,epochs2,epochs3,epochs4])
time_gen.fit(X=epochs1234.get_data(),
y=???
)
scores = time_gen.score(X=epochs1234.get_data(),
y=???)

Sincerely,
Xinchi Yu
University of Maryland

yes np.concatenate((np.full(50, 1),np.full(50, 2),np.full(50, 3),np.full(50, 4))) should work

LogisticRegression does some kind of one-hot encoding internally.

Alex

Thank you so much!! :slight_smile:

It worked when putting np.concatenate((np.full(50, 1),np.full(50, 2),np.full(50, 3),np.full(50, 4))) in the fit() function, but putting it in the score() function resulted in an error “ValueError: roc_auc scoring can only be computed for two-class problems.” May I ask if there is a way to evaluate more-than-two-class problems?

roc_auc only applies to binary classification. You need to use another metric like accuracy.

Alex

Got it! Thank you so much!

Xinchi