# PCA with 95% Variance

**URL:** <https://mne.discourse.group/t/pca-with-95-variance/4253>\
**Category:** Support & Discussions\
**Created:** [January 12, 2022, 1:03am UTC](https://mne.discourse.group/t/pca-with-95-variance/4253 "2022-01-12T01:03:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![julesneuro](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/julesneuro/32/795_2.png) [@julesneuro](https://mne.discourse.group/u/julesneuro)\
**Post date:** [January 12, 2022, 1:03am UTC](https://mne.discourse.group/t/pca-with-95-variance/4253/1 "2022-01-12T01:03:45Z")

</div>

Hi there,

I’m trying to compute PCA with 95% variance on an epochs object.

I’ve written the below function to do so:

```python
def run_PCA(epochs):
    
    m = epochs.metadata
    pca = UnsupervisedSpatialFilter(PCA(0.95), average=False)
    X = epochs.get_data()
    pca_data = pca.fit_transform(X)
    
    events = epochs.events
    pca_components = pca_data.shape[1]
    
    info = mne.create_info(pca_components, epochs.info['sfreq'], ch_types='eeg')
    
    epochs_pca = mne.EpochsArray(pca_data, info, events)
    epochs_pca.metadata = m
    
    return epochs_pca

```

This function has then be saved in a separate .py file and I’m calling the function from an .ipynb notebook. When initially tested in a notebook the code worked fine, but once called in a separate notebook it will only return an epochs object with a single channel. Does anyone know why this is? I’ve repeatedly had the same problem where computing PCA using the UnsupervisedSpatialFilter function, where n\_components is a percentage of variance instead of a selected number of components.

---

<div class="post-metadata">

**Author:** ![julesneuro](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/julesneuro/32/795_2.png) [@julesneuro](https://mne.discourse.group/u/julesneuro)\
**Post date:** [January 13, 2022, 1:43pm UTC](https://mne.discourse.group/t/pca-with-95-variance/4253/2 "2022-01-13T13:43:51Z")

</div>

Update - worked out the problem. Accidently parsed other types of channel into the PCA which were not EEG.
