本文介绍了scikits-learn pca降维问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用scikit-learn和PCA缩小尺寸时遇到问题.
I have a problem with reduction dimension using scikit-learn and PCA.
我有两个numpy矩阵,一个具有大小(1050,4096),另一个具有大小(50,4096).我试图减小两者的尺寸以产生(1050,399)和(50,399),但是在执行pca之后,我得到了(1050,399)和(50,50)矩阵.一个矩阵用于knn训练,另一个矩阵用于knn测试.我的下面的代码有什么问题?
I have two numpy matrices, one has size (1050,4096) and another has size (50,4096). I tried to reduce the dimensions of both to yield (1050, 399) and (50,399) but, after doing the pca I got (1050,399) and (50,50) matrices. One matrix is for knn training and another for knn test. What's wrong with my code below?
pca = decomposition.PCA()
pca.fit(train)
pca.n_components = 399
train_reduced = pca.fit_transform(train)
pca.n_components = 399
pca.fit(test)
test_reduced = pca.fit_transform(test)
推荐答案
在火车上致电fit_transform()
,在测试中致电transform()
:
Call fit_transform()
on train, transform()
on test:
from sklearn import decomposition
train = np.random.rand(1050, 4096)
test = np.random.rand(50, 4096)
pca = decomposition.PCA()
pca.n_components = 399
train_reduced = pca.fit_transform(train)
test_reduced = pca.transform(test)
这篇关于scikits-learn pca降维问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!