Principal Component Analysis

Required packages

The required packages for this section are pandas, seaborn and scikit-learn. These can be installed with the following command in the command window (Windows) / terminal (Mac).

pip install pandas seaborn scikit-learn

Loading the data

We will again use the demo lipidomics dataset:

Load the dataset into a Pandas DataFrame named df as described in the basic plotting section:

import pandas as pd
df = pd.read_excel("Lipidomics_dataset.xlsx", decimal=",")
df.set_index("Sample Name", inplace=True)

PCA

Data normalisation

The first step is to normalise the data such that the features (lipids) have zero mean and unit variance, this can easily be done with the StandardScaler from sklearn. By indexing the dataframe with df.iloc[:,1:] we'll select all the data in the dataframe, except for the first column, which contains the labels:

PCA

Next, we use the PCA sklearn from sklearn, we'll select the 10 first principal components and we'll apply the PCA algorithm to the normalised data:

The PCA function from sklearn return the results as a numpy ndarray, let's put these results in a Pandas DataFrame:

We can now visualise the projection of the samples to the new feature space with a scatterplot:

Explained variance

We can visualise the explained variance of the first n (we chose 10) principal components with:

PCA loadings

We'll extract the loadings from the PCA results and put them in a DataFrame:

Next we can visualise the loadings scores in a scatterplot, including the labels of the features for each datapoint:

PCA on data with missing values

The standard PCA algorithm does not handle datasets with missing values. As an alternative to missing value imputation prior to PCA, there exist PCA variants that can deal with missing values directly. This approach of avoiding imputation of missing values directly can reduce bias. A popular PCA implementation that can handle missing values is Probabilistic Principal Component Analysis (PPCA), first described by Michael E. Tipping and Christopher M. Bishop (Neural Computation (1999) 11 (2): 443–482.).

Required packages

The required packages for this section are pandas, seaborn and scikit-learn. These can be installed with the following command in the command window (Windows) / terminal (Mac).

In addition we need to install the pyppca package from github:

Loading the data

We will use the demo lipidomics dataset with missing values:

Data normalisation

The first step is to normalise the data such that the features (lipids) have zero mean and unit variance, this can easily be done with the StandardScaler from sklearn. By indexing the dataframe with df.iloc[:,1:] we'll select all the data in the dataframe, except for the first column, which contains the labels:

PPCA

Next, we use the PPCA function from pyppca, we'll select the 10 first principal components and we'll apply the PCA algorithm to the normalised data. The first argument of the ppca function is a numpy array that contains the data, the second argument is the number of principal components, and the final argument is a boolean that indicates if algorithmic run details should be printed:

The returned variables are:

The PPCA function from pyppca return the results as a numpy ndarray in the X variable, let's put these results in a Pandas DataFrame:

We can now visualise the projection of the samples to the new feature space with a scatterplot:

Explained variance

We can visualise the explained variance of the first n (we chose 10) principal components with:

PCA loadings

We'll extract the loadings from the PCA results and put them in a DataFrame:

Next we can visualise the loadings scores in a scatterplot, including the labels of the features for each datapoint:

Last updated