For my project, I work with three dimensional MRI data, where the fourth dimension represents different subjects (I use the package nilearn for this). I am using sklearn.decomposition.PCA
to extract a given number of principal components from my data. Now I would like to plot the components separately on a brain image, that is, I would like to show a brain image with my extracted components (in this case, 2) in different colors.
Here’s an example code using the OASIS dataset, which can be downloaded via the nilearn API:
- masking using
nilearn.input_data.NiftiMasker
, which converts my 4 dimensional data into a 2 dimesional array (n_subjects x n_voxels). - standardizing the data matrix using
StandardScaler
- running the PCA using
sklearn.decomposition.PCA
:
## set workspace
import numpy as np
from nilearn.datasets import fetch_oasis_vbm
from nilearn.input_data import NiftiMasker
from nilearn.image import index_img
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from nilearn import plotting
## Load Data #################################################################
# take only first 30 subjects as example
oasis_dataset = fetch_oasis_vbm(n_subjects=30)
imgs = np.array(oasis_dataset['gray_matter_maps'])
## PIPELINE ###################################################################
# create a random number generator
rng = np.random.RandomState(42)
# Convert Images to 2D Data Array
niftimasker = NiftiMasker(mask_strategy='template')
# z-standardize images
scaler = StandardScaler()
# Extract 2 Components
pca = PCA(n_components=2,
svd_solver='full',
random_state=rng)
# create pipeline
pipe = Pipeline([('niftimasker',niftimasker),
('scaler',scaler),
('pca',pca)])
# call fit_transform on pipeline
X = pipe.fit_transform(imgs)
As far as I understand what I obtain after running the PCA are the PCA loadings? Unfortunately, I don't understand how to get from this to two images, each containing one PCA component.