# GeneralizingEstimator with incremental learning

**URL:** <https://mne.discourse.group/t/generalizingestimator-with-incremental-learning/2282>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [August 6, 2020, 3:11pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning/2282 "2020-08-06T15:11:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 6, 2020, 3:11pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning/2282/1 "2020-08-06T15:11:25Z")

</div>

External Email - Use Caution

Hi Giulia,

I recently needed to incorporate SGDClassifier into some MNE pipeline and  
ran into the same problem when working with the LinearModel class. I ended  
up subclassing the MNE class and overloaded / wrote my own "fit" method  
with my own, then called that one instead.

If you write your own class and inherit from GeneralizingEstimator, give it  
its own fit function (in which you write your code that calls partial\_fit  
on the SGDClassifier instance until convergence or whatever your criteria  
are), then it should work.

Best  
Alex Murphy

> Send Mne\_analysis mailing list submissions to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mne\_analysis at nmr.mgh.harvard.edu
> 
> To subscribe or unsubscribe via the World Wide Web, visit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne\_analysis](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)  
> or, via email, send a message with subject or body 'help' to  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mne\_analysis-request at nmr.mgh.harvard.edu
> 
> You can reach the person managing the list at  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mne\_analysis-owner at nmr.mgh.harvard.edu
> 
> When replying, please edit your Subject line so it is more specific  
> than "Re: Contents of Mne\_analysis digest..."
> 
> Today's Topics:
> 
> &nbsp;&nbsp;&nbsp;1. GeneralizingEstimator with incremental learning /  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.partial\_fit (Giulia Gennari)  
> &nbsp;&nbsp;&nbsp;2. Re: GeneralizingEstimator with incremental learning /  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.partial\_fit (Jean-R?mi KING)  
> &nbsp;&nbsp;&nbsp;3. Re: GeneralizingEstimator with incremental learning /  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.partial\_fit (Giulia Gennari)
> 
> ----------------------------------------------------------------------
> 
> Message: 1  
> Date: Wed, 5 Aug 2020 18:38:35 +0200  
> From: Giulia Gennari \<giulia.gennari1991 at gmail.com\>  
> Subject: [Mne\_analysis] GeneralizingEstimator with incremental  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning / .partial\_fit  
> To: mne\_analysis at nmr.mgh.harvard.edu  
> Message-ID:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<CAMn-mVzF+1URRkCr3jf=  
> 9rwneju9Nh\_LUJzvgAEFYc0DtG\_vSA at mail.gmail.com\>  
> Content-Type: text/plain; charset="utf-8"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution
> 
> Hi!
> 
> I would need to try decoding with incremental learning (EEG data).  
> I was planning to use logistic regression by means of the SGDClassifier  
> \<  
> [https://scikit-learn.org/stable/modules/generated/sklearn.linear\_model.SGDClassifier.html](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html)  
> \>  
> .  
> I would then need to call .partial\_fit to make my estimator learn on each  
> of my training sets.  
> However:
> 
> 'GeneralizingEstimator' object has no attribute 'partial\_fit'
> 
> Same issue for SlidingEstimator.  
> Is there a way to work around this limitation?
> 
> Thank you so so much in advance!
> 
> Giulia Gennari  
> -------------- next part --------------  
> An HTML attachment was scrubbed...  
> URL:  
> [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200805/2ceed796/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200805/2ceed796/attachment-0001.html)
> 
> ------------------------------
> 
> Message: 2  
> Date: Wed, 5 Aug 2020 19:17:24 +0200  
> From: Jean-R?mi KING \<jeanremi.king at gmail.com\>  
> Subject: Re: [Mne\_analysis] GeneralizingEstimator with incremental  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning / .partial\_fit  
> To: Discussion and support forum for the users of MNE Software  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<mne\_analysis at nmr.mgh.harvard.edu\>  
> Message-ID:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<  
> CAOcgdchCrC+ibarOSLBmaEr7tSgVFk71dxj1s5zAMekBZnvgCA at mail.gmail.com\>  
> Content-Type: text/plain; charset="utf-8"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution
> 
> Hi Giulia,
> 
> I think you should be able to change the method:
> 
> model = sklearn.linear\_model.SGDClassifier()  
> model.fit = model.partial\_fit  
> slider = mne.decoding.SlidingEstimator(model)  
> for X, y in train\_batches:  
> &nbsp;&nbsp;&nbsp;&nbsp;slider.fit(X, y)
> 
> Best
> 
> JR
> 
> \> External Email - Use Caution  
> \>  
> \> Hi!  
> \>  
> \> I would need to try decoding with incremental learning (EEG data).  
> \> I was planning to use logistic regression by means of the SGDClassifier  
> \> \<  
> [https://scikit-learn.org/stable/modules/generated/sklearn.linear\_model.SGDClassifier.html](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html)  
> \>  
> \> .  
> \> I would then need to call .partial\_fit to make my estimator learn on each  
> \> of my training sets.  
> \> However:  
> \>  
> \> 'GeneralizingEstimator' object has no attribute 'partial\_fit'  
> \>  
> \> Same issue for SlidingEstimator.  
> \> Is there a way to work around this limitation?  
> \>  
> \> Thank you so so much in advance!  
> \>  
> \> Giulia Gennari  
> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> Mne\_analysis mailing list  
> \> Mne\_analysis at nmr.mgh.harvard.edu  
> \> [https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne\_analysis](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)  
> -------------- next part --------------  
> An HTML attachment was scrubbed...  
> URL:  
> [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200805/f78eff5e/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200805/f78eff5e/attachment-0001.html)
> 
> ------------------------------
> 
> Message: 3  
> Date: Thu, 6 Aug 2020 15:54:37 +0200  
> From: Giulia Gennari \<giulia.gennari1991 at gmail.com\>  
> Subject: Re: [Mne\_analysis] GeneralizingEstimator with incremental  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;learning / .partial\_fit  
> To: Discussion and support forum for the users of MNE Software  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<mne\_analysis at nmr.mgh.harvard.edu\>  
> Message-ID:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<CAMn-mVydRwoZTBtuTj\_UhQNX7TV=  
> ufd99xsTfSmFymF+1ZaAWA at mail.gmail.com\>  
> Content-Type: text/plain; charset="utf-8"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution
> 
> Dear Jean-R?mi,
> 
> Thank you for the nice suggestion!
> 
> Just to make sure that this is working (I apologize for my ignorance):
> 
> When I run:  
> model = SGDClassifier(loss='log', class\_weight='balanced')  
> model.fit = model.partial\_fit  
> slider1 = SlidingEstimator(model, scoring='roc\_auc')  
> slider1.fit(X\_train, y\_train)
> 
> or
> 
> clf = make\_pipeline(Vectorizer(), StandardScaler(), model)  
> slider2 = SlidingEstimator(clf, scoring='roc\_auc')  
> slider2.fit(X\_train, y\_train)
> 
> I do not get any error, while I would expect:
> 
> 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?
> 
> Thank you again!
> 
> Giulia
> 
> \> External Email - Use Caution  
> \>  
> \> Hi Giulia,  
> \>  
> \> I think you should be able to change the method:  
> \>  
> \> 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)  
> \>  
> \> Best  
> \>  
> \> JR  
> \>  
> \>  
> \>\> External Email - Use Caution  
> \>\>  
> \>\> Hi!  
> \>\>  
> \>\> I would need to try decoding with incremental learning (EEG data).  
> \>\> I was planning to use logistic regression by means of the SGDClassifier  
> \>\> \<  
> [https://scikit-learn.org/stable/modules/generated/sklearn.linear\_model.SGDClassifier.html](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html)  
> \>  
> \>\> .  
> \>\> I would then need to call .partial\_fit to make my estimator learn on  
> each  
> \>\> of my training sets.  
> \>\> However:  
> \>\>  
> \>\> 'GeneralizingEstimator' object has no attribute 'partial\_fit'  
> \>\>  
> \>\> Same issue for SlidingEstimator.  
> \>\> Is there a way to work around this limitation?  
> \>\>  
> \>\> Thank you so so much in advance!  
> \>\>  
> \>\> Giulia Gennari  
> \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \>\> Mne\_analysis mailing list  
> \>\> Mne\_analysis at nmr.mgh.harvard.edu  
> \>\> [https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne\_analysis](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)  
> \>  
> \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> \> Mne\_analysis mailing list  
> \> Mne\_analysis at nmr.mgh.harvard.edu  
> \> [https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne\_analysis](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)  
> -------------- next part --------------  
> An HTML attachment was scrubbed...  
> URL:  
> [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200806/3f539252/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200806/3f539252/attachment.html)
> 
> ------------------------------
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mne\_analysis mailing list  
> Mne\_analysis at nmr.mgh.harvard.edu  
> [https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne\_analysis](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> 
> End of Mne\_analysis Digest, Vol 151, Issue 4  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<[http://www.avg.com/email-signature?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail&gt](http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt);  
Virus-free.  
www.avg.com  
\<[http://www.avg.com/email-signature?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail&gt](http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt);  
\<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2\>  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200806/c10bff67/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200806/c10bff67/attachment-0001.html)
