在线性回归上使用PCA [英] Using PCA on linear regression
本文介绍了在线性回归上使用PCA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在应用线性回归之前使用主成分分析来减少一些噪声.
I want to use principal component analysis to reduce some noise before applying linear regression.
我有1000个样本和200个特征
I have 1000 samples and 200 features
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.decomposition import PCA
X = np.random.rand(1000,200)
y = np.random.rand(1000,1)
有了这些数据,我可以训练我的模型:
With this data I can train my model:
model.fit(X,y)
但是如果我在应用PCA之后尝试相同的方法
But if I try the same after applying PCA
pca = PCA(n_components=8)
pca.fit(X)
PCA(copy=True, iterated_power='auto', n_components=3, random_state=None,
svd_solver='auto', tol=0.0, whiten=False)
principal_components = pca.components_
model.fit(principal_components,y)
我收到此错误:
ValueError: Found input variables with inconsistent numbers of samples: [8, 1000]
推荐答案
尝试一下:
pca = PCA(n_components=8)
X_pca = pca.fit_transform(X)
model.fit(X_pca,y)
也就是说,您同时将PCA拟合到X并将其转换为名为X_pca的(1000,8)数组.那就是您应该使用的而不是pca.components _
That is, you simultaneously fit PCA to X and transform it into (1000, 8) array named X_pca. That's what you should use instead of the pca.components_
这篇关于在线性回归上使用PCA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文