Welcome, guest | Sign In | My Account | Store | Cart

simple pca

Python, 11 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from numpy import array, mat, shape, transpose
from scipy import cov, linalg
from pylab import load, arange

data2 = mat(array(load('raw3.dat', delimiter='\t',usecols=arange(0,13,1), unpack=True)))
time_series = mat(cov(data2, rowvar=1))
print 'covariance matrix : ', shape(time_series)
eval, evec = linalg.eig(mat(time_series))
print shape(eval), shape(evec)
print abs(evec)
print abs(eval)

2 comments

Keith Briggs 13 years, 3 months ago  # | flag

That should be "principal component analysis".

James Stroud 11 years, 7 months ago  # | flag

Very cryptic! Let me help. Here is 3D (x,y,z) data (observations are by COLUMN).

data = array([[ -3.936,  -1.429,   2.245,   4.082,   7.557,   9.266],
              [ 19.913,  20.915,  21.745,  22.307,  23.814,  23.628],
              [  1.839,   4.516,   4.847,   8.128,   7.809,  11.176]])
eval, evec = linalg.eig(cov(data))

Note that the eigenvectors are organized like data where evec[:,0] is the first eigenvector, etc. See the numpy docs.