scikits-learn pca dimension reduction issue

 ̄綄美尐妖づ 提交于 2019-12-06 08:45:24

问题


I have a problem with reduction dimension using scikit-learn and PCA.

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)

回答1:


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)


来源:https://stackoverflow.com/questions/15422487/scikits-learn-pca-dimension-reduction-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!