Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

back to course overview

PCA (Principal Component Analysis) in Python - ML From Scratch 11

30 Nov 2019

In this Machine Learning from Scratch Tutorial, we are going to implement a PCA algorithm using only built-in Python modules and numpy. We will also learn about the concept and the math behind this popular ML algorithm.

All algorithms from this course can be found on GitHub together with example tests.

Implementation

import numpy as np class PCA: def __init__(self, n_components): self.n_components = n_components self.components = None self.mean = None def fit(self, X): # Mean centering self.mean = np.mean(X, axis=0) X = X - self.mean # covariance, function needs samples as columns cov = np.cov(X.T) # eigenvalues, eigenvectors eigenvalues, eigenvectors = np.linalg.eig(cov) # -> eigenvector v = [:,i] column vector, transpose for easier calculations # sort eigenvectors eigenvectors = eigenvectors.T idxs = np.argsort(eigenvalues)[::-1] eigenvalues = eigenvalues[idxs] eigenvectors = eigenvectors[idxs] # store first n eigenvectors self.components = eigenvectors[0:self.n_components] def transform(self, X): # project data X = X - self.mean return np.dot(X, self.components.T)