我正在尝试在python上实现PCA。我在mnist数据集上使用KNN分类器来检查我的实现成功但是成功率太低了%10%。你能检查我的代码并说明我做错了吗?
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
def PCA(data, ndimension):
x , y = data.shape
mean_vec = np.mean(data, axis=0)
mean_data = data - mean_vec
cov_mat = mean_data.T.dot(mean_data) / (x-1)
eig_vals, eig_vecs = np.linalg.eig(cov_mat)
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]
eig_pairs.sort(key=lambda x: x[0], reverse=True)
matrix_w = eig_pairs[0][1].reshape(y,1)
for ar in range(1, ndimension):
matrix_w = np.hstack((matrix_w, eig_pairs[ar][1].reshape(y,1)))
FinalData = (mean_data.dot(matrix_w))
return FinalData
xtrain = PCA(train_images,40)
xtest = PCA(test_images, 40)
r=0
w=0
num = len(xtest)
for i in range(num):
t = xtest[i]
j = getNearestSampleIndex(t, xtrain)
if (np.all(train_labels[j] == test_labels[i])):
r+=1
else:
w+=1
print ("tested ", num, " digits")
print ("correct: ", r, "wrong: ", w, "error rate: ", float(w)*100/(r+w), "%")
print ("got correctly ", float(r)*100/(r+w), "%")
答案 0 :(得分:0)
我知道你为什么用x-1划分cp.cov()而你使用np.hstack,这里我认为是一个非常简单的PCA版本(测试并比较了女巫scikit)每个数据都是一列矢量形状(N,1)
def myPCA(data, n_comp=3):
d= data - data.mean(axis = 1).reshape(data.shape[0], 1)
c = np.cov(d)
eigvals, eigvect = np.linalg.eigh(c)
ind = np.argsort(eigvals)
ind = ind[::-1]
eigvals = eigvals[ind]
eigvect = eigvect[:,ind]
data_projected = (d.T @ eigvect[:,:n_comp]).T
return eigvals[:n_comp], eigvect[:,:n_comp], data_projected