Skip to content

Instantly share code, notes, and snippets.

@gnzsnz
Last active December 9, 2023 15:09
Show Gist options
  • Save gnzsnz/f8d37f59d14783599f67b03e36879f0e to your computer and use it in GitHub Desktop.
Save gnzsnz/f8d37f59d14783599f67b03e36879f0e to your computer and use it in GitHub Desktop.
Principal Component Analysis in pure Numpy
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 31 11:03:57 2013
@author: Sukhbinder
https://sukhbinder.wordpress.com/2022/08/21/principal-component-analysis-with-numpy/
"""
import numpy as np
def pca1(x):
"""Determine the principal components of a vector of measurements
Determine the principal components of a vector of measurements
x should be a M x N numpy array composed of M observations of n variables
PCA using covariance
The output is:
coeffs - the NxN correlation matrix that can be used to transform x into its components
signals is MxN of projected data
The code for this function is based on "A Tutorial on Principal Component
Analysis", Shlens, 2005 http://www.snl.salk.edu/~shlens/pub/notes/pca.pdf
(unpublished)
https://www.cs.cmu.edu/~elaw/papers/pca.pdf
"""
(M,N) = x.shape
Mean = x.mean(0)
y = x - Mean
cov = np.dot(y.T,y) / (M-1)
(V,PC) = np.linalg.eig(cov)
order = (-V).argsort()
V=V[order]
coeff = PC[:,order]
signals = np.dot(PC.T,y.T)
return coeff,signals,V
def pca2(x):
"""Determine the principal components of a vector of measurements
Determine the principal components of a vector of measurements
x should be a M x N numpy array composed of M observations of n variables
The output is:
coeffs - the NxN correlation matrix that can be used to transform x into its components
signals is MxN of projected data
The code for this function is based on "A Tutorial on Principal Component
Analysis", Shlens, 2005 http://www.snl.salk.edu/~shlens/pub/notes/pca.pdf
(unpublished)
https://www.cs.cmu.edu/~elaw/papers/pca.pdf
"""
(M,N) = x.shape
Mean = x.mean(0)
y = x - Mean
yy = y.T/np.sqrt(M-1)
u,s,pc = np.linalg.svd(yy)
s=np.diag(s)
v= np.dot(s,s)
v=diag(v)
signals = np.dot(pc.T,y)
return pc,signals,v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment