Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 管道mlp回归器评分_Python_Scikit Learn_Pipeline - Fatal编程技术网

Python 管道mlp回归器评分

Python 管道mlp回归器评分,python,scikit-learn,pipeline,Python,Scikit Learn,Pipeline,我正在努力弄清楚当使用mlprepressor时,什么是cross\u val\u score和pipeline.score返回。我的印象是它返回了r2score,但是当我运行sklearn.metrics.r2_score时,我收到了一个完全不同的值。 这是我的密码 x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2, random_state=i) scaler=StandardScaler() x_trai

我正在努力弄清楚当使用
mlprepressor
时,什么是
cross\u val\u score
和pipeline.score返回。我的印象是它返回了
r2
score,但是当我运行
sklearn.metrics.r2_score
时,我收到了一个完全不同的值。 这是我的密码

x_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2, random_state=i)
scaler=StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test=scaler.transform(x_test)


mlp=MLPRegressor(activation='relu', solver='lbfgs', hidden_layer_sizes=(6,6,6), max_iter=10000, learning_rate_init=0.001)
pipeline=Pipeline([('transformer',scaler),('estimator',mlp)])
pipeline.fit(x,y)
cv=KFold(n_splits=3)
scorescrossval=cross_val_score(pipeline,x_test,y_test,cv=cv)
print('scores cross val',scorescrossval)
print('accuracy: %0.2f (+/- %0.2f)'%(scorescrossval.mean(),scorescrossval.std()*2))
print('pipeline score',pipeline.score(x_test,y_test))

mlp.fit(x_train,y_train)
predictions=mlp.predict(x_test)
RTWO=sklearn.metrics.r2_score(y_test,predictions)
这就给了,

scores cross val [0.97435245 0.96969591 0.98333161]
accuracy: 0.98 (+/- 0.01)
pipeline score -0.7028296099215846
RTWO -5.735668230366273

我努力想知道应该信任哪种价值观,并确定它们的含义。感谢您的帮助

这些分数不具有可比性,因为每次使用不同的数据学习模型

让我们将代码分为三部分:

(一)

(二)

(三)

代码3很容易分离

在我看来,有一件事是你误解了
cross\u val\u分数
这只是测试,而不是从数据中学习,但事实并非如此

cross_val_score
将根据提供的
cv
对提供的数据进行分割,并对模型进行多次训练,并报告剩余数据的分数。因此,您的
x_测试
y_测试
将分为3个折叠,每个折叠将根据其他折叠的学习测试一次。所以你会得到3分

另外,对于代码1和代码2,我可以像上面一样更改顺序,因为
cross_val_score
将使用
pipeline
对象的克隆来学习数据并报告分数。因此,原始的
管道
未被触动

现在你能理解这些不同分数背后的原因吗

更新以获取评论

在代码1中,对整个未缩放数据拟合管道,然后对相同数据的缩放部分进行测试。所以分数应该更高一些。但你得到的是-0.7028

这是因为,
x_test
x
的一部分,但是已经进行了缩放,并且
管道。score
将再次缩放它,这是错误的

代码2接近正确。但它只使用了0.2%的数据,而且已经超出了导致数据泄漏的
交叉评分。所以分数很高


代码3似乎也正确。在这种情况下,
RTWO
应该与
mlp.score(x\u测试,y\u测试)
的输出相匹配。现在,为什么它这么低是另一个问题。
(x\u火车,y\u火车)

谢谢你的回答。我了解如何使用不同的数据,以及如何将数据分割为交叉分数,但我不明白为什么它们如此不同。cross_val的准确度为0.98,这表明模型非常好,但r2_得分为-5,表明模型很糟糕。我如何知道如何给模型打分?谢谢你,你的评论帮了大忙。
mlp.score(x\u-train,y\u-train)
上的分数为0.992,这与
sklearn.metrics.r2\u分数(x\u-train,y\u-train)
at-899978上的分数截然不同!!!
pipeline.fit(x,y)
print('pipeline score',pipeline.score(x_test,y_test))
cv=KFold(n_splits=3)
scorescrossval=cross_val_score(pipeline,x_test,y_test,cv=cv)
print('scores cross val',scorescrossval)
print('accuracy: %0.2f (+/- %0.2f)'%(scorescrossval.mean(),scorescrossval.std()*2))
mlp.fit(x_train,y_train)
predictions=mlp.predict(x_test)
RTWO=sklearn.metrics.r2_score(y_test,predictions)