scikit-learn-将管道预测转换为原始值/比例 [英] scikit-learn - Convert pipeline prediction to original value/scale

查看:75
本文介绍了scikit-learn-将管道预测转换为原始值/比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了如下管道(使用 Keras Scikit-Learn API )

I've create a pipeline as follows (using the Keras Scikit-Learn API)

estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, nb_epoch=50, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)

并适合

pipeline.fit(trainX,trainY)

如果我使用pipline.predict(testX)进行预测,则(相信)我得到了标准化的预测.

If I predict with pipline.predict(testX), I (believe) I get standardised predictions.

如何在testX上进行预测,以使predictedY与实际的(未触及的)testY具有相同的比例(即,不是标准化的预测,而是实际值)?我看到有一个 Pipeline的方法,但是似乎仅用于还原转换后的X.

How do I predict on testX so that predictedY it at the same scale as the actual (untouched) testY (i.e. NOT standardised prediction, but instead the actual values)? I see there is an inverse_transform method for Pipeline, however appears to be for only reverting a transformed X.

推荐答案

完全正确.管道中的StandardScaler()仅映射管道的输入(trainX).fit(trainX,trainY).

Exactly. The StandardScaler() in a pipeline is only mapping the inputs (trainX) of pipeline.fit(trainX,trainY).

因此,如果您将模型拟合为近似trainY,并且也需要对其进行标准化,则应将trainY映射为

So, if you fit your model to approximate trainY and you need it to be standardized as well, you should map your trainY as

scalerY = StandardScaler().fit(trainY)  # fit y scaler
pipeline.fit(trainX, scalerY.transform(trainY))  # fit your pipeline to scaled Y
testY = scalerY.inverse_transform(pipeline.predict(testX))  # predict and rescale

inverse_transform()函数考虑在StandardScaler().fit()中计算出的标准差和均值来映射其值.

The inverse_transform() function maps its values considering the standard deviation and mean calculated in StandardScaler().fit().

您总是可以在不缩放Y的情况下拟合模型,但是根据数据的不同,这可能很危险,因为它可能导致模型过度拟合.您必须对其进行测试;)

You can always fit your model without scaling Y, as you mentioned, but this can be dangerous depending on your data since it can lead your model to overfit. You have to test it ;)

这篇关于scikit-learn-将管道预测转换为原始值/比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆