Scree Plot for PCA

Hi,

I performed a PCA on my epoched data and now would like to create a scree plot from the components. For this, I am trying to follow this tutorial. For this, I need to run the follwing line of code to access the % of variance explained by each component:

pca = UnsupervisedSpatialFilter(PCA(.99))
X_pca = pca.fit_transform(epochs_filt_data)
var_explained = pca.explained_variance_ratio_

This returns the following error:

AttributeError: 'UnsupervisedSpatialFilter' object has no attribute 'explained_variance_ratio_'

How can I access explained_variance_ratio instead?

Hello @tempMEG,

can you please provide a minimal working example so others have something to work with?

Thanks,
Richard

Yes ofc! I just added two more lines to give the code more context. I sadly can’t provide the data though.

hi @tempMEG ,

no need to share the data. Just use a public dataset from mne.datasets and share a full script one can just execute. By doing this effort you allow us to answer more user support questions.

thanks
Alex

1 Like

Hi @tempMEG
The error tells you that .explained_variance_ratio_ is not available for UnsupervisedSpatialFilter class. This is because it is an attribute of sklearn PCA object that is stored somewhere inside the UnsupervisedSpatialFilter class.
Based on the docs I would suggest to use .get_params() method to get the attributes of the PCA object.

Hi @mmagnuski ,

I am having similar issue. But when I use ..get_params() I just obtain the followings:

{'average': False,
 'estimator__copy': True,
 'estimator__iterated_power': 'auto',
 'estimator__n_components': 0.9,
 'estimator__random_state': None,
 'estimator__svd_solver': 'auto',
 'estimator__tol': 0.0,
 'estimator__whiten': False,
 'estimator': PCA(n_components=0.9)}

There is nothing about explained_variance_ratio

get_params() gives you the hyperparameters of a sklearn model as passed in the init constructor.
It does not return the fit attributes that end with a trailing _

Alex

1 Like