model = sklearn.linear_model.SGDClassifier()
model.fit = model.partial_fit
slider = mne.decoding.SlidingEstimator(model)
for X, y in train_batches:
slider.fit(X, y)
ValueError: class_weight 'balanced' is not supported for partial_fit.
In order to use 'balanced' weights, use
compute_class_weight('balanced', classes, y). Pass the resulting
weights as the class_weight parameter.
Since this is what I get with:
model.fit(X_train[:,:,single_time_point], y_train)
Is there a good reason for that? E.g. class weights are computed internally
beforehand by SlidingEstimator?
A solution based on this:
class MyModel(SGDClassifier):
def fit(self, X, y):
super().partial_fit(X, y)
return self
..works fine!
Except for the crucial fact that parallel processing (n_jobs>1) seems not
feasible.
This is what I get when I try to score the slider (apologies for the
ugliness, I copy-paste everything since it might be meaningful to catch
what is wrong):
In the long run, for batch optimization of parallel tasks (here each slided
time sample), I would encourage you to have a look at pytorch; sklearn is
not really optimal for this because it can't make use of gpu.
In the meantime, here is a solution to your problem: simply put your new
class in a separate script e.g.
# in mymodel.py
import numpy as np
from sklearn.linear_model import SGDClassifier
class MyModel(SGDClassifier):
def fit(self, X, y):
if not hasattr(self, 'classes_'):
self.classes_ = np.unique(y)
super().partial_fit(X, y, self.classes_)
return self
# main script
import numpy as np
from mne.decoding import SlidingEstimator
from mymodel import MyModel
model = MyModel()
slider = SlidingEstimator(model, scoring='roc_auc', n_jobs=2)
X = np.random.randn(100, 10, 3)
y = np.random.randint(0, 2, 100)
slider.fit(X, y)
slider.score(X, y)
Thank you for the suggestion and, above all, thank you so much for your
help and assistance!
My scripts have been working just fine while I would have never been able
to implement my current analysis without your prompts.