PCA Explained with Dynamic Plotly Visualizations | by Soner Yıldırım | Towards Data Science

PCA (Principal component analysis) is an unsupervised learning algorithm that finds the relations among features within a dataset. It is also widely used as a preprocessing step for supervised learning algorithms.
The aim of using PCA is to reduce the number of features by finding principal components that explain the variance within the dataset as much as possible. The principal components are linear combinations of the features of original dataset.
The advantage of PCA is that a significant amount of variance of the original dataset is retained using much smaller number of features than the original dataset. Principal components are ordered according to the amount of variance they represent.
In this post, we will first implement a PCA algorithm and then create dynamic visualizations with Plotly to explain the idea behind the PCA more clearly.
This post is more of a practical one. If you want to go deeper on how PCA actually works, here is more detailed post on the theoretical side.
Plotly Python (plotly.py) is an open-source plotting library built on plotly javascript (plotly.js) and it offers a high-level API (plotly express) and a low level API (graph objects) to create dynamic and interactive visualizations. With plotly express, we can create a nice plot with very few lines of code. On the other hand, we need to write more code with graph objects but have more control on what we create.
Let’s start with importing the related libraries:
import numpy as npimport pandas as pdfrom sklearn.decomposition import PCAfrom sklearn.datasets import load_breast_cancer
I will use the breast cancer dataset which contains 30 features and a target variable indicating whether the cell is malignant or benign. To simplify the visualizations, I will randomly select 5 features.
dataset = load_breast_cancer()X, y = load_breast_cancer(return_X_y = True)df = pd.DataFrame(X, columns=dataset.feature_names)df = df.sample(n=5, axis=1)df['target'] = ydf.head()
Let’s create a scatter_matrix that displays scatter plots of feature pairs. We get an overview of how successful pairs of features are at separating the target class.
import plotly.express as pxfig = px.scatter_matrix(df, dimensions=df.columns[:-1], color='target', title='Scatter Matrix of Features', height=800)
Some features are better at differentiating the target class.
Let’s apply PCA to represent these 5 features with 2 principal components.
X = df.drop(['target'], axis=1)#Normalizefrom sklearn.preprocessing import StandardScalersc = StandardScaler()X_normalized = sc.fit_transform(X)#Principal componentspca = PCA(n_components=2)components = pca.fit_transform(X_normalized)
One attribute of the PCA class is the explained_variance_ratio_ which, as the name suggests, tells us how much of the total variance is explained by each principal component.
pca.explained_variance_ratio_array([0.69369623, 0.20978844])
%90 of the variance in the original dataset is explained by 2 principal components.
Let’s create a scatter matrix with the principal components.
labels = {str(i): f"PC {i+1} ({var:.1f}%)" for i, var in enumerate(pca.explained_variance_ratio_ * 100)}fig = px.scatter_matrix(components, labels=labels, dimensions=range(2), color=df['target'])fig.show()
Plotly also provides 3D scatter plots which can be useful when we have 3 principal components. To experiment 3D plots, we first need to apply a PCA to our dataset again to create 3 principal components.
pca = PCA(n_components=3)components = pca.fit_transform(X_normalized)
We can now create a 3D scatter plot.
var = pca.explained_variance_ratio_.sum()fig = px.scatter_3d(components, x=0, y=1, z=2, color=df['target'],title=f'Total Explained Variance: {var}',labels={'0':'PC1', '1':'PC2', '2':'PC3'})fig.show()
The total explained variance with two principal components was %90. It has increased by %7 when the third principal component is added.
We have used a subset of the breast cancer dataset for demonstration purposes. Feel free to use the entire set and see how much of the total variance is explained by the principal components.
Plotting the principal components will provide insight into the structure within the dataset and the relationships among features.
Thank you for reading. Please let me know if you have any feedback.