@@ -0,0 +1,1405 @@
+import cv2%0Aimport PIL%0Afrom numpy import *%0A%0A%22%22%22%0APCA (Principal Component Analysis) -- Preprocessing technique%0A%0AUsed for:%0A1) Dimensionality reduction%0A%0ARequires:%0A1) Matrix with training data stored as flattened %5Bnumpy%5D arrays in rows%0A%0AReturns:%0A1) Projection matrix (with important dimensions first)%0A2) Variance%0A3) Mean%0A%22%22%22%0A%0A%0Adef pca(input_matrix):%0A    %22%22%22%0A    Principal Component Analysis for dimensionality reduction%0A    %22%22%22%0A    # Get dimensions%0A    num_data, dim = input_matrix.shape%0A%0A    # Center data%0A    mean_input_matrix = input_matrix.mean(axis=0)%0A    input_matrix = input_matrix - mean_input_matrix%0A%0A    if dim %3E num_data:%0A        # PCA - compact trick used%0A        M = dot(input_matrix, input_matrix.T) # Convariance matrix%0A        e, EV = linalg.eigh(M) # eigenvalues and eigenvectors%0A        tmp = dot(input_matrix.T, EV) # this is the compact trick%0A        V = tmp%5B::-1%5D # reverse since the last eigenvectors are the ones we want%0A        S = sqrt(e)%5B::-1%5D # reverse since eigenvalues are in increasing order%0A        for i in range(V.shape%5B1%5D):%0A            V%5B:,i%5D /= S%0A    else:%0A        # PCA - SVD used%0A        U, S, V = linalg.sv(input_matrix)%0A        V = V%5B:num_data%5D # only makes sense to return the first num_data%0A%0A    # Return the projection matrix, the variance, and the mean%0A    return V, S, mean_input_matrix%0A%0Aif __name__ == '__main__':%0A    # @TODO add example code to test here%0A    pass%0A

Add PCA