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 | |
X = np.random.rand(5,10) |
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
X -= X.mean(axis=0) | |
C = np.cov(X,rowvar=False) |
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
from numpy import linalg as LA | |
w, v = LA.eig(C) | |
idx = w.argsort()[::-1] # In order that bigger Eigenvalue comes first | |
w, v = w[idx], v[:, idx] |
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
n = 2; | |
n_PC = v[:, 0:n] | |
#transform matrix X to two dimension matrix | |
T = np.dot(X, n_PC) |
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
rng = np.random.RandomState(1) | |
X_raw = np.dot(rng.rand(2, 2), rng.randn(2, 200)).T | |
X_mean = X_raw.mean(axis=0) | |
X -= X_mean |
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
C = np.cov(X.T) # Now shape is 2 *2 | |
w, v = LA.eig(C) | |
inx = w.argsort()[::-1] | |
w, v = w[inx], v[:, inx] | |
w_12 = w[:2] | |
v_12 = v[:, :2] |
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 seaborn as sns; sns.set() | |
ax = plt.gca() | |
ax.set_xlabel('Principal component 1') | |
ax.set_ylabel('Principal component 2') | |
plt.scatter(X_raw[:, 0], X_raw[:, 1], c='#663399', alpha=0.5) | |
plt.scatter(X_mean[0], X_mean[1], c='red', s=50) | |
plt.axis('equal') | |
for length, vector in zip(w_12, v_12): |
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
T = X.dot(v) # Projecting sample data | |
T = T[:, 0:2] | |
# Or simply, T = X.dot(v_12) | |
T_mean = T.mean(axis=0) | |
C_T = np.cov(T.T) # Shape 2 *2 | |
wT, vT = LA.eig(C_T) | |
inxT = wT.argsort()[::-1] | |
wT, v = wT[inxT], vT[:, inxT] |
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
ax = plt.gca() | |
ax.set_xlabel('Principal component 1') | |
ax.set_ylabel('Principal component 2') | |
plt.scatter(T[:, 0], T[:, 1], c='#663399', alpha=0.5) | |
plt.scatter(T_mean[0], T_mean[1], c='red', s=50) | |
for length, vector in zip(w_12_T, v_12_T): | |
dir_ = vector * 3 * np.sqrt(length) | |
arrowprops = dict(arrowstyle='->', linewidth=2, shrinkA=0, | |
shrinkB=0, color='red', alpha=0.5) |
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
rng = np.random.RandomState(1) | |
X_raw = np.dot(rng.rand(2, 2), rng.randn(2, 200)).T | |
X_mean = X_raw.mean(axis=0) | |
X -= X_mean | |
U, s, Vt = LA.svd(X, full_matrices=False) | |
V = Vt.T | |
S = np.diag(s) | |
e_values = (s ** 2) / X.shape[0] |
OlderNewer