scikit-learn:如何缩减'y'预测结果

时间:2016-06-27 16:19:38

标签: python machine-learning scikit-learn scale

我正在尝试使用波士顿住房数据集学习scikit-learn和机器学习。

# 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,我尝试根据y的第一个实例预测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,房屋的价格范围)

[-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值应该缩减,但我试图弄清楚如何做到这一点却没有成功。欢迎任何提示。非常感谢你。

2 个答案:

答案 0 :(得分:22)

您可以使用inverse_transform对象使用scalery

y_new_inverse = scalery.inverse_transform(y_new)

答案 1 :(得分:1)

游戏迟到: 只是不要缩放您的y。通过缩放y,您实际上会松动单位。回归或损失优化实际上是由要素之间的相对差异确定的。对于房价(或任何其他货币价值)的顺便说一句,通常采用对数。然后您显然需要执行numpy.exp()才能返回实际的美元/欧元/日元...