def pcaTest(): """ Si = sqrtm( array([[3,2], [2,4]]) ) x = dot( randn(1000, 2), Si ) (P,Z,evals) = pca( x, 2 ) print "Z:", Z print "evals:", evals x0 = dot(dot(x, Z[0,:]).reshape(1000,1), Z[0,:].reshape(1,2)) x1 = dot(dot(x, Z[1,:]).reshape(1000,1), Z[1,:].reshape(1,2)) plot(x[:,0], x[:,1], 'b.', x0[:,0], x0[:,1], 'r.', x1[:,0], x1[:,1], 'g.') input() """ #(X, Y) = datasets.loadDigits() #(P,Z,evals) = pca( X, 784 ) """ esum = sum( evals ) print esum print "evals", evals[0:3] evals = evals / esum print "evals", evals[0:3] print "evals", evals plot( evals, zeros([evals.shape[0]]), 'b.') input() """ #print cumsum( evals ) """acum = 0 for i in xrange( len( evals ) ): acum = sum( evals[:i] ) print acum if acum >= 0.90: print "i:", i break """ # print Z #x0 = dot(dot(X, Z[0,:]).reshape(1000,1), Z[0,:].reshape(1,2)) #plot(X[:,0], X[:,1], 'b.', x0[:,0], x0[:,1], 'r.') #input() #drawDigits(Z[1:50], arange(50)) #input() (a,b) = datasets.makeKPCAdata() x = vstack((a,b)) (P,Z,evals) = pca(x,2) Pa = P[0:a.shape[0],:] Pb = P[a.shape[0]:-1,:] plot(Pa[:,0], randn(Pa.shape[0]), 'b.', Pb[:,0], randn(Pb.shape[0]), 'r.') input() return
#FOR WU3 Figure draw the top 50 eigvectors util.drawDigits(Z[:, 0:49].T, arange(50)) #FOR WU3b util.drawDigits(dot(P, Z.T), Y) #FOR WU3c (P, Z, evals) = dr.pca(X, 5) util.drawDigits(dot(P, Z.T), Y) # ---------- KPCA ---------- # Si = util.sqrtm(array([[3, 2], [2, 4]])) x = dot(randn(1000, 2), Si) (P, alpha, evals) = dr.kpca(x, 2, kernel.linear) evals # do with data where vanilla PCA fails (a, b) = datasets.makeKPCAdata() plot(a[:, 0], a[:, 1], 'b.', b[:, 0], b[:, 1], 'r.') x = vstack((a, b)) x_c = (x - mean(x)) / std(x) (P, Z, evals) = dr.pca(x, 2) Pa = P[0:a.shape[0], :] Pb = P[a.shape[0]:, :] plot(Pa[:, 0], randn(Pa.shape[0]), 'b.', Pb[:, 0], randn(Pb.shape[0]), 'r.') # projection of the data with each eig vectors... # x0 = dot(dot(x, Z[:, 0]).reshape(473,1), Z[:, 0].reshape(1,2)) # x1 = dot(dot(x, Z[:, 1]).reshape(473,1), Z[:, 1].reshape(1,2)) # pro0a = x0[0:a.shape[0],:] # pro0b = x0[a.shape[0]:,:]
#FOR WU3 Figure draw the top 50 eigvectors util.drawDigits(Z[:,0:49].T,arange(50)) #FOR WU3b util.drawDigits(dot(P,Z.T),Y) #FOR WU3c (P,Z,evals) = dr.pca(X, 5) util.drawDigits(dot(P,Z.T),Y) # ---------- KPCA ---------- # Si = util.sqrtm(array([[3,2],[2,4]])) x = dot(randn(1000,2), Si) (P, alpha, evals) = dr.kpca(x, 2, kernel.linear) evals # do with data where vanilla PCA fails (a,b) = datasets.makeKPCAdata() plot(a[:,0], a[:,1], 'b.', b[:,0], b[:,1], 'r.') x = vstack((a,b)) x_c = (x-mean(x))/std(x) (P,Z,evals) = dr.pca(x, 2) Pa = P[0:a.shape[0],:] Pb = P[a.shape[0]:,:] plot(Pa[:,0], randn(Pa.shape[0]), 'b.', Pb[:,0], randn(Pb.shape[0]), 'r.') # projection of the data with each eig vectors... # x0 = dot(dot(x, Z[:, 0]).reshape(473,1), Z[:, 0].reshape(1,2)) # x1 = dot(dot(x, Z[:, 1]).reshape(473,1), Z[:, 1].reshape(1,2)) # pro0a = x0[0:a.shape[0],:] # pro0b = x0[a.shape[0]:,:]