Skip to content

Instantly share code, notes, and snippets.

View RyotaBannai's full-sized avatar
🛴
Man's soil is still rich enough to direct his own life.

Ryota Bannai RyotaBannai

🛴
Man's soil is still rich enough to direct his own life.
View GitHub Profile
View matrix_x.py
import numpy as np
X = np.random.rand(5,10)
View select_2_pcs.py
n = 2;
n_PC = v[:, 0:n]
#transform matrix X to two dimension matrix
T = np.dot(X, n_PC)
View plot_matrix_n_evectors_w_eig.py
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):
View rotate.py
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)
View projecting_samples_2.py
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]
View plot_matrix_n_evectors_w_svd.py
ax = plt.gca()
ax.set_xlabel('X'); ax.set_ylabel('Y')
plt.scatter(X_raw[:, 0], X_raw[:, 1], c='#B8860B', alpha=0.5)
plt.scatter(X_mean[0], X_mean[1], c='red', s=50)
plt.axis('equal')
for length, vector in zip(e_values, V):
dir_ = -vector * 3 * np.sqrt(length) # Tweak the sign
start = X_mean; end = start + dir_
arrowprops = dict(arrowstyle='->',linewidth=2,
View 2x200_matrix_random_gdist.py
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
View scilearn_pca.py
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(X) # Apply PCA
View plot_matrix_n_evectors_w_scilearn.py
ax = plt.gca(); ax.set_xlabel('X'); ax.set_ylabel('Y')
plt.scatter(X[:, 0], X[:, 1], alpha=0.3, color="#191970")
plt.scatter(pca.mean_[0], pca.mean_[1], c='red', s=50)
plt.axis('equal')
for length, vector in zip(pca.explained_variance_, pca.components_):
dir_ = vector * 3 * np.sqrt(length)
start = pca.mean_; end = start + dir_
arrowprops = dict(arrowstyle='->',linewidth=2,
shrinkA=0, shrinkB=0, color='red', alpha=0.5)