# Linear classifier on sensor data with plot patterns	and filters

**URL:** <https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [February 6, 2019, 6:12am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733 "2019-02-06T06:12:46Z")\
**Posts on this page:** 14\
**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:** [February 6, 2019, 6:12am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/1 "2019-02-06T06:12:46Z")

</div>

External Email - Use Caution

Hello

I was thing to do this example with my data, but in the end, I only get this error:  
&nbsp;&nbsp;File "C:\Anaconda3\envs\mne\lib\site-packages\numpy\lib\function\_base.py", line 2325, in cov  
&nbsp;&nbsp;&nbsp;&nbsp;c = dot(X, X\_T.conj())

MemoryError

What can cause it?

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20190206/71c6c057/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20190206/71c6c057/attachment.html)

---

<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:** [February 6, 2019, 8:56am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/2 "2019-02-06T08:56:55Z")

</div>

External Email - Use Caution

hi,

I don't think your error is related to MNE.

I suspect you want to compute cov(X.T) and not cov(X) ie  
you swapped rows and columns

HTH  
Alex

---

<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:** [February 6, 2019, 9:10am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/3 "2019-02-06T09:10:19Z")

</div>

External Email - Use Caution

Well, I didn't change code much, only add my data.  
I'm really confused where I made a mistake.

Now it looks this way:

data\_path = 'C:/Users/Public/MEG/asami\_ryo/170809/Asami1\_raw.fif'

#print(raw\_data)  
#print(raw\_data.ch\_names[:204])  
tmin, tmax = 0, 10.26  
event\_id = dict(l=1, r=2, n=4)

raw\_data = mne.io.read\_raw\_fif(data\_path, preload=True)  
raw\_data.filter(1.0, None, fir\_design='firwin')  
#raw\_data.crop(tmin, tmax).load\_data()  
events = mne.find\_events (raw\_data)  
picks = mne.pick\_types(raw\_data.info, meg=True, eeg=True, stim=False, eog=False)  
epochs = mne.Epochs(raw\_data, events, event\_id=event\_id, tmin=tmin, tmax=tmax, proj=False,baseline=(None, 0), preload=True,verbose=False)  
labels = epochs.events[:, -1]

# 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)

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\_)):  
&nbsp;&nbsp;&nbsp;&nbsp;# We fitted the linear model onto Z-scored data. To make the filters  
&nbsp;&nbsp;&nbsp;&nbsp;# interpretable, we must reverse this normalization step  
&nbsp;&nbsp;&nbsp;&nbsp;coef = scaler.inverse\_transform([coef])[0]

&nbsp;&nbsp;&nbsp;&nbsp;# The data was vectorized to fit a single model across all time points and  
&nbsp;&nbsp;&nbsp;&nbsp;# all channels. We thus reshape it:  
&nbsp;&nbsp;&nbsp;&nbsp;coef = coef.reshape(len(meg\_epochs.ch\_names), -1)

&nbsp;&nbsp;&nbsp;&nbsp;# Plot  
&nbsp;&nbsp;&nbsp;&nbsp;evoked = EvokedArray(coef, meg\_epochs.info, tmin=epochs.tmin)  
&nbsp;&nbsp;&nbsp;&nbsp;evoked.plot\_topomap(title='MEG %s' % name, time\_unit='s')

---

<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:** [February 6, 2019, 9:19am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/4 "2019-02-06T09:19:29Z")

</div>

External Email - Use Caution

what is the size of meg\_data for you? how does it compare with what is  
used in mne example?

Alex

---

<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:** [February 6, 2019, 10:16am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/5 "2019-02-06T10:16:16Z")

</div>

External Email - Use Caution

??? ??? ??? ??? ??? ??? ? ??? OneDrive. ??? ??? ???, ??? ??? ???.

\<[https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU](https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU)\>  
[[https://r1.res.office365.com/owa/prem/images/dc-generic\_20.png](https://r1.res.office365.com/owa/prem/images/dc-generic_20.png)]\<[https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU](https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU)\>

Asami1\_raw.fif\<[https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU](https://1drv.ms/u/s!AsTbwfRXq04Aa0PWbR2gdb0UdLU)\>

I didn' checked the sample data of this example, just add mine raw data. It's one session with one person, so I assumed I can't it worse.

---

<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:** [February 6, 2019, 10:29am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/6 "2019-02-06T10:29:19Z")

</div>

External Email - Use Caution

please check first with sample data. If you have a pb with sample you have  
a setup / hardware limitation.

Alex  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20190206/effbe745/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20190206/effbe745/attachment-0001.html)

---

<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:** [February 7, 2019, 8:47am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/7 "2019-02-07T08:47:15Z")

</div>

External Email - Use Caution

Tried, but it works well with a sample data.

So it's be because I'm using a to big raw file? I need crop it? Or it's because of different filter's settings?

---

<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:** [February 7, 2019, 8:56am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/8 "2019-02-07T08:56:47Z")

</div>

External Email - Use Caution

I cannot guess.

I need to know the size of the new data.

Alex

---

<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:** [February 7, 2019, 9:03am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/9 "2019-02-07T09:03:07Z")

</div>

External Email - Use Caution

I think it s because you reshape your meg data (n\_trials, n\_sensors,  
n\_times) into a matrix (n\_trials, n\_sensors \* n\_times)

meg\_data = meg\_epochs.get\_data().reshape(len(labels), -1)

The covariance of this matrix will be pretty heavy.

JR

---

<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:** [February 7, 2019, 9:22am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/10 "2019-02-07T09:22:57Z")

</div>

External Email - Use Caution

It's 278 mb heavy, around 6 minutes.

---

<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:** [February 7, 2019, 9:24am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/11 "2019-02-07T09:24:44Z")

</div>

External Email - Use Caution

I did reshape because it's in example, if it's too heavy I should refuse this conversion?

---

<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:** [February 7, 2019, 10:04am UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/12 "2019-02-07T10:04:49Z")

</div>

External Email - Use Caution

what is n\_times for you?  
basically how many samples and features do you have?

A

---

<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:** [February 7, 2019, 6:49pm UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/13 "2019-02-07T18:49:25Z")

</div>

External Email - Use Caution

n\_times is 348000 and total projection items are 10.

Probably there's a very simple solution, but I'm quite new for all this.

---

<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:** [February 7, 2019, 8:16pm UTC](https://mne.discourse.group/t/linear-classifier-on-sensor-data-with-plot-patterns-and-filters/1733/14 "2019-02-07T20:16:52Z")

</div>

External Email - Use Caution

learning in dimension 348000 with 10 samples is not realistic.  
You need more sample and to reduce the feature space / dimension.

Alex
