I’m trying to compute PCA with 95% variance on an epochs object.
I’ve written the below function to do so:
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 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.