# decoding source space LDA patterns

**URL:** <https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977>\
**Category:** Support & Discussions\
**Tags:** machine-learning\
**Created:** [November 17, 2021, 3:07pm UTC](https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977 "2021-11-17T15:07:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magna-fede](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/magna-fede/32/967_2.png) [@magna-fede](https://mne.discourse.group/u/magna-fede)\
**Post date:** [November 17, 2021, 3:07pm UTC](https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977/1 "2021-11-17T15:07:11Z")

</div>

Hello,

I am learning how to decode brain activity in source space, based on this tutorial [Decoding source space data — MNE 0.24.0 documentation](https://mne.tools/stable/auto_examples/decoding/decoding_spatio_temporal_source.html#tut-dec-st-source) .

I can compute patterns on my data when using Logistic Regression model, as in the example. I would like however to compute patterns from an LDA model. I see that MNE creates the patterns\_ attribute only for logistic regressions [mne-python/base.py at main · mne-tools/mne-python · GitHub](https://github.com/mne-tools/mne-python/blob/main/mne/decoding/base.py). Are you are going to implement this feature also for LDA in future versions of MNE? Or is there a particular reason why this is available for Logisitc Regression and not for LDA?

I decided to give it a try and calculate the patterns myself, by using the coef\_ covariance\_ attributes stored in sklearn.discriminant\_analysis.LinearDiscriminantAnalysis:

coef = get\_coef(time\_decod, ‘coef\_’, inverse\_transform=True)  
cov = get\_coef(time\_decod, ‘covariance\_’, inverse\_transform=True)

for i in range(timepoints):  
patterns.append(cov[:,:,i].dot(coef[:,i]))

patterns = np.array(patterns).T

This runs okay, but when I then plot the resulting patterns, they do not look nice and smooth as in the logistic regressions, but are “spiky” and do not make much sense. So there is definitely something I am doing wrong. Could you help me with this?

Thanks,

Federica

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [November 17, 2021, 8:18pm UTC](https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977/2 "2021-11-17T20:18:12Z")

</div>

are you sure it does not work with LDA?

if the model has a .coef\_ attribute after fit it should work.

Alex

---

<div class="post-metadata">

**Author:** ![magna-fede](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/magna-fede/32/967_2.png) [@magna-fede](https://mne.discourse.group/u/magna-fede)\
**Post date:** [November 18, 2021, 10:24am UTC](https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977/3 "2021-11-18T10:24:32Z")

</div>

Hi Alex, thanks a lot for your reply.

I had the feeling that the reason why get\_coef was not working for in LDA (neither filters\_ nor patterns\_, while I could get attributes such as coef\_ and covariance\_) is that logistic regression is part of sklearn.linear\_model, while LDA is part of sklearn.discriminant\_analysis and somehow MNE was including just the first when transforming coef\_. However I might be completely wrong.

Just for clarity:

clf = make\_pipeline(StandardScaler(),  
SelectKBest(f\_classif, k=‘all’),  
LinearDiscriminantAnalysis(solver=“svd”,  
store\_covariance=True))

time\_decod = SlidingEstimator(clf, scoring=‘roc\_auc’)

time\_decod.fit(X, y)

####### this works  
coef = get\_coef(time\_decod, ‘coef\_’, inverse\_transform=True)

####### these don’t  
patterns = get\_coef(time\_decod, ‘patterns\_’, inverse\_transform=True)  
filters = get\_coef(time\_decod, ‘filters\_’, inverse\_transform=True)

####### ValueError: This estimator does not have a filters\_(/patterns\_) attribute: LinearDiscriminantAnalysis(store\_covariance=True)

Thanks,

Federica

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [November 18, 2021, 2:16pm UTC](https://mne.discourse.group/t/decoding-source-space-lda-patterns/3977/4 "2021-11-18T14:16:21Z")

</div>

to have filters and patterns attributes you need to wrap your scikit-learn model  
with LinearModel

see the:

csp = CSP(n\_components=3, norm\_trace=False)  
clf\_csp = make\_pipeline(csp, LinearModel(LogisticRegression(solver=‘lbfgs’)))  
scores = cross\_val\_multiscore(clf\_csp, X, y, cv=5, n\_jobs=1)  
print(‘CSP: %0.1f%%’ % (100 \* scores.mean(),))

in

[https://mne.tools/stable/auto\_tutorials/machine-learning/50\_decoding.html#source-power-comodulation-spoc](https://mne.tools/stable/auto_tutorials/machine-learning/50_decoding.html#source-power-comodulation-spoc)

Alex

in
