尺寸问题线性回归Python scikit学习

时间:2018-11-16 11:36:57

标签: 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)

然后

lm.fit(X_train,Y_train)
Y_pred = lm.predict(X_test)

但是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)

在此先感谢任何人可以带给我的帮助:)

亚历克斯

1 个答案:

答案 0 :(得分:1)

要使用lm.score(),您需要传递X_testy_test

dico['R2 value']=lm.score(X_test, Y_test)

请参见documentation here

  

得分(X,y,sample_weight =无)

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.

您正在尝试将score方法用作度量标准方法,这是错误的。任何估算器上的score()方法本身都会计算出预测,然后将其发送给适当的指标得分器。

如果您想自己使用Y_testY_pred,则可以执行以下操作:

from sklearn.metrics import r2_score
dico['R2 value'] = r2_score(Y_test, Y_pred)
相关问题