CSP returning LinAlgError

Hi there :)!

  • MNE-Python version: 0.23.0
  • os: Windows 10
  • Misc: Working in Spyder, Python 3.8

I am currently working with the MEG dataset provided by Rathee et al. (2021).
Data: Link
Publication describing the data: Link

My goal is to replicate their results (i.e., binary classification of the events). To this end, I am using their original scripts (Script2 and Script3 from their git repo), which were written in Matlab using fieldtrip. I am rewriting the code in Python using MNE.

I have gotten to the point in Script2 where they apply CSP (line 191) for feature selection. This is where I am struggling.
My first attempt to perform the CSP in MNE looks as follows:

csp = CSP()
X = data_tr_toi_comb_bp.get_data()
y = data_tr_toi_comb_bp.events[:,2]
X_train = csp.fit(X,y)

Running the last line returns the following error:
LinAlgError: The leading minor of order 200 of B is not positive definite. The factorization of B could not be completed and no eigenvalues or eigenvectors were computed.

At the end of this post you can find the full error traceback if it’s of interest for you.
I found these two post (Post 1, Post 2) which addressed the same error messages, however, none of the advice given on them solved the problem for me.
Has anyone an idea what is causing this issue and what might be a possible work around?


  1. Full error traceback
Traceback (most recent call last):

  File "C:\Users\vof7rng\Documents\FVA\LocalFiles\MNE\MatlabScripts_PythonVersions\Script2.py", line 156, in <module>
    X_train = csp.fit(X,y)

  File "C:\Users\vof7rng\AppData\Roaming\Python\Python38\site-packages\mne\decoding\csp.py", line 176, in fit
    eigen_vectors, eigen_values = self._decompose_covs(covs,

  File "C:\Users\vof7rng\AppData\Roaming\Python\Python38\site-packages\mne\decoding\csp.py", line 538, in _decompose_covs
    eigen_values, eigen_vectors = linalg.eigh(covs[0], covs.sum(0))

  File "C:\Program Files\Anaconda3\lib\site-packages\scipy\linalg\decomp.py", line 575, in eigh
    raise LinAlgError('The leading minor of order {} of B is not '

LinAlgError: The leading minor of order 200 of B is not positive definite. The factorization of B could not be completed and no eigenvalues or eigenvectors were computed.
  1. Some additional info about the data that might help:
  • data_tr_toi_comb_bp is the data, which
    1. tr: is from the first recording session. The data from the first recording session is being used as training data.
    2. toi: is from a specific time interval of interest, in this case from 0.5 - 3.5 s
    3. comb: is from a specific binary combination, in this case Both Hand Imagery (event id: 1) & Both Feet Imagery (event id: 2)
    4. bp: has been bandpassfiltered, in this case at 8-12 Hz

Note: I cropped the data at tmax=200 to allow faster computational times while only figuring out the code.

  1. data_tr_toi_comb_bp.info
<Info | 19 non-empty values
 acq_pars: ACQactiveGround 0 ACQch.BIO001.gain 2000 ACQch.BIO001.highpass ...
 bads: []
 ch_names: MEG0112, MEG0113, MEG0122, MEG0123, MEG0132, MEG0133, MEG0142, ...
 chs: 204 GRAD
 custom_ref_applied: False
 description: Anonymized using a time shift to preserve age at acquisition
 dev_head_t: MEG device -> head transform
 dig: 266 items (3 Cardinal, 5 HPI, 258 Extra)
 experimenter: mne_anonymize
 file_id: 4 items (dict)
 highpass: 8.0 Hz
 hpi_meas: 1 item (list)
 hpi_results: 1 item (list)
 line_freq: 50
 lowpass: 12.0 Hz
 meas_date: 2017-07-06 12:57:17 UTC
 meas_id: 4 items (dict)
 nchan: 204
 projs: generated with autossp-1.0.1: on, generated with autossp-1.0.1: ...
 sfreq: 250.0 Hz
 subject_info: 9 items (dict)

Hello @tempMEG,

have you tried this with a fresh conda environment that was created following our official installation instructions? Tagging @larsoner to catch his attention :wave:

Is the data rank deficient? Either way, you could probably apply PCA first (in a pipeline for example) or regularize the covariance(s) to avoid this issue

Hi @richard & @larsoner, thanks so much for getting back to me!
Removing some noisy channels and applying PCA solved the issue.