# Scree Plot for PCA

**URL:** <https://mne.discourse.group/t/scree-plot-for-pca/3783>\
**Category:** Support & Discussions\
**Tags:** meg\
**Created:** [October 6, 2021, 11:02am UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783 "2021-10-06T11:02:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tempMEG](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@tempMEG](https://mne.discourse.group/u/tempMEG)\
**Post date:** [October 6, 2021, 11:02am UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/1 "2021-10-06T11:02:25Z")

</div>

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](https://www.datasklr.com/principal-component-analysis-and-factor-analysis/principal-component-analysis) tutorial. For this, I need to run the follwing line of code to access the % of variance explained by each component:

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

```

This returns the following error:

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

```

How can I access `explained_variance_ratio` instead?

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 6, 2021, 11:43am UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/2 "2021-10-06T11:43:24Z")

</div>

Hello @tempMEG,

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

Thanks,  
Richard

---

<div class="post-metadata">

**Author:** ![tempMEG](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@tempMEG](https://mne.discourse.group/u/tempMEG)\
**Post date:** [October 6, 2021, 11:48am UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/3 "2021-10-06T11:48:48Z")

</div>

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

---

<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:** [October 7, 2021, 7:29am UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/4 "2021-10-07T07:29:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [October 7, 2021, 1:54pm UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/5 "2021-10-07T13:54:57Z")

</div>

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](https://mne.tools/stable/generated/mne.decoding.UnsupervisedSpatialFilter.html) I would suggest to use `.get_params()` method to get the attributes of the PCA object.

---

<div class="post-metadata">

**Author:** ![ali44](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@ali44](https://mne.discourse.group/u/ali44)\
**Post date:** [August 21, 2022, 2:01pm UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/6 "2022-08-21T14:01:06Z")

</div>

Hi @mmagnuski ,

I am having similar issue. But when I use ._.get\_params()_ I just obtain the followings:

```python
{'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_

---

<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:** [August 21, 2022, 6:36pm UTC](https://mne.discourse.group/t/scree-plot-for-pca/3783/7 "2022-08-21T18:36:56Z")

</div>

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
