Package pyvision :: Package ml :: Module pca
[hide private]
[frames] | no frames]

Source Code for Module pyvision.ml.pca

 1  ''' 
 2  Created on Jan 17, 2011 
 3   
 4  @author: bolme 
 5  ''' 
 6   
 7  import numpy as np 
 8  import scipy.linalg as la 
 9   
10 -def prcomp(data,center=True,scale=False):
11 ''' 12 Conduct a basic principal components analysis on the data. 13 14 This function has been compared to R to verify that it produces similar results. 15 16 @param data: a data matrix with vectors in rows 17 @param center: subtract the mean vector from the data 18 @param scale: scale the values to have unit variance 19 @returns: stdevs,rotation,[center],[scale] 20 ''' 21 data = data.copy() 22 r,c = data.shape 23 24 # center the data 25 if center: 26 ctr = data.mean(axis=0).reshape(1,c) 27 data = data - ctr 28 29 # scale the data 30 if scale: 31 scl = data.std(axis=0,ddof=1).reshape(1,c) 32 data = data/scl 33 34 # decompose the data using svd 35 _,val,vt = la.svd(data,full_matrices=False) 36 37 # compute the standard deviations from the singular values 38 standard_dev = val/np.sqrt(r-1) 39 40 # Vt.T are the basis vectors 41 result = [standard_dev,vt.T] 42 43 # format result 44 if center: 45 result.append(ctr) 46 47 if scale: 48 result.append(scl) 49 50 return result
51 52 53 # Alias the name to pca 54 pca = prcomp 55