Created
August 4, 2020 10:05
-
-
Save jangsoopark/4282daaa4e210051a09ba481436903ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.5 2.4 | |
0.5 0.7 | |
2.2 2.9 | |
1.9 2.2 | |
3.1 3.0 | |
2.3 2.7 | |
2 1.6 | |
1 1.1 | |
1.5 1.6 | |
1.1 0.9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
data = np.loadtxt('data.txt', delimiter='\t') | |
# subtract mean | |
data_adjusted = data - data.mean(axis=0) | |
# covariance matrix | |
covariance = np.cov(data_adjusted.T) | |
# eigen decomposition | |
eigen_values, eigen_vectors = np.linalg.eig(covariance) | |
print(eigen_values.shape) | |
print(eigen_vectors.shape) | |
print('eigen value * I * eigen vector') | |
print(np.matmul(eigen_values * np.eye(2), eigen_vectors)) | |
print('covariance matrix * eigen vector') | |
print(np.matmul(covariance, eigen_vectors)) | |
ind = np.argsort(eigen_values)[::-1] | |
eigen_values = eigen_values[ind] | |
eigen_vectors = eigen_vectors[ind] | |
top_k = 1 | |
eigen_values_top_k = eigen_values[:top_k] | |
eigen_vectors_top_k = eigen_vectors[:top_k, :] | |
print(eigen_values_top_k) | |
print(eigen_vectors_top_k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment