Created
June 30, 2014 13:47
-
-
Save EnsekiTT/d47226f7af0db8ae6c91 to your computer and use it in GitHub Desktop.
PCA(二次元なのにPCAしちゃって可視化するだけ)
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
| # -*- coding: utf-8 -*- | |
| import numpy as np | |
| import scipy as sp | |
| import scipy.linalg as spla | |
| import matplotlib.pyplot as plt | |
| def pca(data, base_num = 1): | |
| n, d = data.shape #n:データ数 d:次元数 n>dじゃないとダメです。 | |
| data_mean = data.mean(0) | |
| data_norm = data - data_mean | |
| cov = np.dot(data_norm.T, data_norm) / float(n) | |
| w, vl = spla.eig(cov) | |
| index = w.argsort()[-min(base_num, d) :] | |
| t = vl[:, index[:: -1]].T | |
| return t | |
| if __name__ == "__main__": | |
| data = np.random.multivariate_normal([0, 0], [[1, 2], [3, 4]], 100) | |
| base = pca(data)[0] | |
| #ここから可視化 | |
| plt.scatter(data[:, 0], data[:, 1]) | |
| leng = (data.max()-data.min())/2 | |
| pc_line = np.array([-leng, leng]) * (base[1] / base[0]) | |
| plt.plot([-leng, leng], pc_line, "r") | |
| plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment