Advertisement
Mhazard

Anaconda Spyder - Principle Coponent Analysis (PCA)

Mar 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Mar 24 09:57:46 2018
  4.  
  5. @author: username
  6. """
  7. import numpy as np
  8.  
  9.  
  10. X = np.array([[1, 2, 3, 1, 2],
  11.              [3, 1, 5, 7, 9],
  12.              [2, 2, 7, 1, 6],
  13.              [8, 9, 2, 6, 2]])
  14.  
  15. n_sample = X.shape[0]
  16. dim = X.shape[1]
  17.  
  18. print ('The number of data is %d and the original dimensionality of data is %d.' % (n_sample, dim))
  19.  
  20. X_mean = np.mean(X, 0)
  21. X_hat = X - X_mean
  22.  
  23. X_hat_transpose = X_hat.T
  24. covariance_matrix = np.cov(X_hat_transpose)
  25. print ('The covariance matrix is \n%s' % covariance_matrix)
  26.  
  27. EigenValues, EigenVectors = np.linalg.eig(covariance_matrix)
  28. index = EigenValues.argsort()[-1]
  29. EigenValues_maximum = EigenValues[index]
  30. EigenVectors_maximum = EigenVectors[:, index]
  31.  
  32. print ('The eigenvector of the maximum eigenvalue is \n%s' % EigenVectors_maximum)
  33.  
  34. X_projected_1 = np.dot(X_hat, EigenVectors_maximum)
  35. print (X_projected_1)
  36.  
  37. U, D, V = np.linalg.svd(X_hat)
  38.  
  39. print (D)
  40. print (V)
  41.  
  42. print (V[0, :])
  43.  
  44. X_projected_2 = np.dot(X_hat, V[0, :])
  45.  
  46. print (X_projected_2)
  47.  
  48. ----
  49.  
  50. Outputs:
  51.  
  52. The number of data is 4 and the original dimensionality of data is 5.
  53.  
  54. The covariance matrix is
  55. [[  9.66666667  10.66666667  -3.83333333   6.5         -3.16666667]
  56.  [ 10.66666667  13.66666667  -5.5          4.16666667  -7.83333333]
  57.  [ -3.83333333  -5.5          4.91666667  -2.25         5.41666667]
  58.  [  6.5          4.16666667  -2.25        10.25         3.91666667]
  59.  [ -3.16666667  -7.83333333   5.41666667   3.91666667  11.58333333]]
  60.  
  61. The eigenvector of the maximum eigenvalue is
  62. [-0.51593529+0.j -0.65096754+0.j  0.31439697+0.j -0.25398825+0.j
  63.   0.38300986+0.j]
  64.  
  65. X_projected_1
  66. [ 1.51848390+0.j  2.92351431+0.j  3.79217593+0.j -8.23417413+0.j]
  67.  
  68. Print (D)
  69. [  9.64546273e+00   6.88907160e+00   3.12341821e+00   6.66788455e-16]
  70.  
  71. Print (V)
  72. [[-0.51593529 -0.65096754  0.31439697 -0.25398825  0.38300986]
  73.  [ 0.26157469 -0.03763102  0.11075017  0.69562888  0.65878483]
  74.  [-0.3182144  -0.39321581 -0.71388251  0.42748234 -0.22748964]
  75.  [ 0.5031028  -0.22133546 -0.53150908 -0.5007761   0.40573338]
  76.  [-0.55768864  0.60927589 -0.31106077 -0.1344343   0.45048294]]
  77.  
  78. Print (V[0, :])
  79. [-0.51593529 -0.65096754  0.31439697 -0.25398825  0.38300986]
  80.  
  81. X_projected_2
  82. [ 1.5184839   2.92351431  3.79217593 -8.23417413]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement