Python 2.7 维度问题线性回归Python scikit学习
我正在实现一个函数,其中我必须使用scikit learn执行线性回归 用一个例子来运行它时,我得到了什么:Python 2.7 维度问题线性回归Python scikit学习,python-2.7,scikit-learn,linear-regression,Python 2.7,Scikit Learn,Linear Regression,我正在实现一个函数,其中我必须使用scikit learn执行线性回归 用一个例子来运行它时,我得到了什么: X_train.shape=(34,3) X_test.shape=(12,3) Y_train.shape=(34,1) Y_test.shape=(12,1) 然后 然而Python告诉我这一行有一个错误 dico['R2 value']=lm.score(Y_test, Y_pred) Python告诉我: ValueError: shapes (12,1) and (3,
X_train.shape=(34,3)
X_test.shape=(12,3)
Y_train.shape=(34,1)
Y_test.shape=(12,1)
然后
然而Python告诉我这一行有一个错误
dico['R2 value']=lm.score(Y_test, Y_pred)
Python告诉我:
ValueError: shapes (12,1) and (3,1) not aligned: 1 (dim 1) != 3 (dim 0)
提前感谢任何人能给我带来的帮助:)
Alex使用lm.score()
您需要通过X\u测试
,y\u测试
dico['R2 value']=lm.score(X_test, Y_test)
见:
得分(X,y,样本重量=无)
您试图将分数方法用作度量方法,这是错误的。任何估计器上的score()
方法本身都会计算预测,然后将它们发送给相应的度量记分器
如果您想自己使用Y_test
和Y_pred
,则可以执行以下操作:
from sklearn.metrics import r2_score
dico['R2 value'] = r2_score(Y_test, Y_pred)
非常感谢你的帮助!似乎我有点困惑:)但是现在我不明白为什么r2分数真的很低(0.11),而我使用的数据集是iris数据集…@Alex iris是一个分类数据集,你使用的是回归模型(R平方的线性回归),因此不起作用。使用名称中有
分类器的模型我不明白为什么,因为我只保留了setosa类型的虹膜,这样回归就有意义了。我的特征是separalengthcm,separalwidthcm,petalalengthcm,我想预测PetalWidthCm。那么为什么线性回归不合法呢?@Alex那么,在这种情况下,回归是有意义的。但是,你需要考虑从其他特征预测花瓣宽度是否有意义。只有当因变量(本例中为petalwidth)实际上是其他变量的因变量时,回归才会表现良好。最后一个问题:如果还有一些名词性/序数特征,我还能使用sklearn的线性回归吗?(显然,我会在执行回归之前对它们进行编码)
X : array-like, shape = (n_samples, n_features) Test samples.
For some estimators this may be a precomputed kernel matrix instead,
shape = (n_samples, n_samples_fitted], where n_samples_fitted is the
number of samples used in the fitting for the estimator.
y : array-like, shape = (n_samples) or (n_samples, n_outputs) True values for X.
sample_weight : array-like, shape = [n_samples], optional Sample weights.
from sklearn.metrics import r2_score
dico['R2 value'] = r2_score(Y_test, Y_pred)