将X中的每一行分别与 W k W_{k} Wk中的每一行相乘,就得到了k个数,k个数组成的向量就是该样本映射到 W k W_{k} Wk这个坐标系上得到的一个新的k维的向量,这样做 就可以把X中的m个样本全部映射到k维空间中
但是此时的 X m X_{m} Xm和原来的 X X X不一样
import numpy as np
class PCA:
def __init__(self, n_components):
"""初始化PCA"""
assert n_components >= 1, "n_components must be valid"
self.n_components = n_components
self.components_ = None
def fit(self, X, eta=0.01, n_iters=1e4):
"""获得数据集X的前n个主成分"""
assert self.n_components <= X.shape[1], \
"n_components must not be greater than the feature number of X"
def demean(X):
return X - np.mean(X, axis=0)
def f(w, X):
return np.sum((X.dot(w) ** 2)) / len(X)
def df(w, X):
return X.T.dot(X.dot(w)) * 2. / len(X)
def direction(w):
return w / np.linalg.norm(w)
def first_component(X, initial_w, eta=0.01, n_iters=1e4, epsilon=1e-8):
w = direction(initial_w)
cur_iter = 0
while cur_iter < n_iters:
gradient = df(w, X)
last_w = w
w = w + eta * gradient
w = direction(w)
if (abs(f(w, X) - f(last_w, X)) < epsilon):
break
cur_iter += 1
return w
X_pca = demean(X)
self.components_ = np.empty(shape=(self.n_components, X.shape[1]))
for i in range(self.n_components):
initial_w = np.random.random(X_pca.shape[1])
w = first_component(X_pca, initial_w, eta, n_iters)
self.components_[i,:] = w
X_pca = X_pca - X_pca.dot(w).reshape(-1, 1) * w
return self
def transform(self, X):
"""将给定的X,映射到各个主成分分量中"""
assert X.shape[1] == self.components_.shape[1]
return X.dot(self.components_.T)
def inverse_transform(self, X):
"""将给定的X,反向映射回原来的特征空间"""
assert X.shape[1] == self.components_.shape[0]
return X.dot(self.components_)
def __repr__(self):
return "PCA(n_components=%d)" % self.n_components
import numpy as np
import matplotlib .pyplot as plt
X = np.empty((100,2))
X[:,0] = np.random.uniform(0.,10.,size=100)
X[:,1] = 0.75 * X[:,0] + 3. + np.random.normal(0,10.,size=100)
from playML.PCA import PCA
pca = PCA(n_components=2)
pca.fit(X)
输出:PCA(n_components=2)
pca.components_
输出:array([[ 0.08690502, 0.9962166 ],
[ 0.99622176, -0.08684583]])
X_reduction = pca.transform(X)
X_reduction.shape
输出:(100, 2)这里输出(100,2)是因为2行2列的矩阵转置后仍为2行2列
X_restore = pca.inverse_transform(X_reduction)
X_restore.shape
输出结果:(100, 2)这里输出(100,2)是因为2行2列的矩阵转置后仍为2行2列
#下面我们用n_components=1来实验,就可以看出映射的效果
pca = PCA(n_components=1)
pca.fit(X)
X_reduction = pca.transform(X)
X_reduction.shape
输出:(100, 1)
X_restore = pca.inverse_transform(X_reduction)
X_restore.shape
输出:(100, 2)
plt.scatter(X[:,0],X[:,1],color='b',alpha=0.5)
plt.scatter(X_restore[:,0],X_restore[:,1],color='r',alpha=0.5)
plt.show()
由图片可以看出:映射回去后的 X m X_{m} Xm和降维之前的 X X X不一样,说明在降维时出现了不可找回的数据损失
from sklearn.decomposition import PCA
pca = PCA(n_components=1)
pca.fit(X)
输出:PCA(n_components=1)
pca.components_
输出:array([[-0.09418549, -0.99555467]])
X_redction = pca.transform(X)
X_reduction.shape
输出:(100, 1)
pca.inverse_transform(X_reduction)
X_restore.shape
(100, 2)
plt.scatter(X[:,0],X[:,1],color='b',alpha=0.5)
plt.scatter(X_restore[:,0],X_restore[:,1],color='r',alpha=0.5)
plt.show()