Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Sat Mar 24 09:57:46 2018
- @author: username
- """
- import numpy as np
- X = np.array([[1, 2, 3, 1, 2],
- [3, 1, 5, 7, 9],
- [2, 2, 7, 1, 6],
- [8, 9, 2, 6, 2]])
- n_sample = X.shape[0]
- dim = X.shape[1]
- print ('The number of data is %d and the original dimensionality of data is %d.' % (n_sample, dim))
- X_mean = np.mean(X, 0)
- X_hat = X - X_mean
- X_hat_transpose = X_hat.T
- covariance_matrix = np.cov(X_hat_transpose)
- print ('The covariance matrix is \n%s' % covariance_matrix)
- EigenValues, EigenVectors = np.linalg.eig(covariance_matrix)
- index = EigenValues.argsort()[-1]
- EigenValues_maximum = EigenValues[index]
- EigenVectors_maximum = EigenVectors[:, index]
- print ('The eigenvector of the maximum eigenvalue is \n%s' % EigenVectors_maximum)
- X_projected_1 = np.dot(X_hat, EigenVectors_maximum)
- print (X_projected_1)
- U, D, V = np.linalg.svd(X_hat)
- print (D)
- print (V)
- print (V[0, :])
- X_projected_2 = np.dot(X_hat, V[0, :])
- print (X_projected_2)
- ----
- Outputs:
- The number of data is 4 and the original dimensionality of data is 5.
- The covariance matrix is
- [[ 9.66666667 10.66666667 -3.83333333 6.5 -3.16666667]
- [ 10.66666667 13.66666667 -5.5 4.16666667 -7.83333333]
- [ -3.83333333 -5.5 4.91666667 -2.25 5.41666667]
- [ 6.5 4.16666667 -2.25 10.25 3.91666667]
- [ -3.16666667 -7.83333333 5.41666667 3.91666667 11.58333333]]
- The eigenvector of the maximum eigenvalue is
- [-0.51593529+0.j -0.65096754+0.j 0.31439697+0.j -0.25398825+0.j
- 0.38300986+0.j]
- X_projected_1
- [ 1.51848390+0.j 2.92351431+0.j 3.79217593+0.j -8.23417413+0.j]
- Print (D)
- [ 9.64546273e+00 6.88907160e+00 3.12341821e+00 6.66788455e-16]
- Print (V)
- [[-0.51593529 -0.65096754 0.31439697 -0.25398825 0.38300986]
- [ 0.26157469 -0.03763102 0.11075017 0.69562888 0.65878483]
- [-0.3182144 -0.39321581 -0.71388251 0.42748234 -0.22748964]
- [ 0.5031028 -0.22133546 -0.53150908 -0.5007761 0.40573338]
- [-0.55768864 0.60927589 -0.31106077 -0.1344343 0.45048294]]
- Print (V[0, :])
- [-0.51593529 -0.65096754 0.31439697 -0.25398825 0.38300986]
- X_projected_2
- [ 1.5184839 2.92351431 3.79217593 -8.23417413]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement