scikit-learn:如何缩减"y"预测结果 [英] scikit-learn: how to scale back the 'y' predicted result

查看:139
本文介绍了scikit-learn:如何缩减"y"预测结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用Boston Housing数据集学习scikit-learn和机器学习.

I'm trying to learn scikit-learn and Machine Learning by using the Boston Housing Data Set.

# I splitted the initial dataset ('housing_X' and 'housing_y')
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(housing_X, housing_y, test_size=0.25, random_state=33)

# I scaled those two datasets
from sklearn.preprocessing import StandardScaler
scalerX = StandardScaler().fit(X_train)
scalery = StandardScaler().fit(y_train)
X_train = scalerX.transform(X_train)
y_train = scalery.transform(y_train)
X_test = scalerX.transform(X_test)
y_test = scalery.transform(y_test)

# I created the model
from sklearn import linear_model
clf_sgd = linear_model.SGDRegressor(loss='squared_loss', penalty=None, random_state=42) 
train_and_evaluate(clf_sgd,X_train,y_train)

基于这个新模型clf_sgd,我试图基于X_train的第一个实例来预测y.

Based on this new model clf_sgd, I am trying to predict the y based on the first instance of X_train.

X_new_scaled = X_train[0]
print (X_new_scaled)
y_new = clf_sgd.predict(X_new_scaled)
print (y_new)

但是,结果对我来说很奇怪(1.34032174,而不是20-30,是房屋价格的范围)

However, the result is quite odd for me (1.34032174, instead of 20-30, the range of the price of the houses)

[-0.32076092  0.35553428 -1.00966618 -0.28784917  0.87716097  1.28834383
  0.4759489  -0.83034371 -0.47659648 -0.81061061 -2.49222645  0.35062335
 -0.39859013]
[ 1.34032174]

我想应该缩小此1.34032174值,但是我试图弄清楚如何成功.欢迎任何提示.非常感谢.

I guess that this 1.34032174 value should be scaled back, but I am trying to figure out how to do it with no success. Any tip is welcome. Thank you very much.

推荐答案

您可以通过scalery对象使用inverse_transform:

y_new_inverse = scalery.inverse_transform(y_new)

这篇关于scikit-learn:如何缩减"y"预测结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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