1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
-
-
-
-
-
-
-
- import numpy
-
- def pca(A, remove=[]):
- ''' The input matrix A should be a 2D numpy array in column-major
- order. Each column is a dataset and PCA will be applied across
- columns
- '''
-
-
- nrow,ncol = numpy.shape(A)
-
-
- A = numpy.matrix(A)
-
-
- covMatrix = numpy.matrix(numpy.zeros((nrow, nrow)))
-
-
- meanArray = []
- for i in range(nrow):
- meanArray.append(numpy.mean(A[i].tolist()[0]) )
- A[i] -= meanArray[i]
- meanArray = numpy.array(meanArray)
-
-
- covMatrix = numpy.cov(A)
-
-
- eigs = numpy.linalg.eig(covMatrix)
- K = eigs[1].T
-
-
-
-
-
-
- for i in remove:
- K[i] = numpy.zeros(len(K))
-
-
- transMatrix = K*A
-
-
- return numpy.array(transMatrix), K, meanArray
-
-
-
- def invpca(transMatrix, K, means):
- '''Converts a PCA rotated dataset back to normal. Input parameters
- are the components to discard in re-creation.
- '''
- K = numpy.matrix(K)
-
-
- untransMatrix = K.T*transMatrix
-
-
- for i in range(len(transMatrix)):
- untransMatrix[i] += means[i]
-
- return untransMatrix
|