如何在scikit学习回归中不标准化目标数据

2024-09-24 00:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图预测未来的利润数据在一个铜矿企业数据集的csv格式。在

我看了数据:

data = pd.read_csv('data.csv')

我把数据分开:

^{pr2}$

创建svr预测值:

clf_svr= svm.SVR(kernel='rbf')

数据标准化:

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)

print np.max(x_train), np.min(x_train), np.mean(x_train), np.max(y_train), np.min(y_train), np.mean(y_train)

然后预测:

y_pred=clf.predict(x_test)

并对预测数据进行了标准化处理。我希望预测的数据是原始格式,我该怎么做?在


Tags: csv数据testdata格式nptransformtrain
3条回答

正如其他人已经提到的,您应该使用inverse_transform()方法从之前应用的相应转换中检索原始数据。 另一个需要思考的问题是,如果我们的目的是预测实际的目标'y'值,为什么需要转换目标y_test, y_train?在预测过程中,我们也可以保持它的原始状态。在

另外(在python3.7.3,sklearn0.20.3)中,当您像上面所做的那样标准化单列行时,您会无意中收到一个numpy数组的输出,这对数据帧操作没有帮助

例如:

enter image description here

当您指定输出应该类似于一个单列数据帧时,您可能会遇到更多问题

例如:

enter image description hereenter image description here

解决方案:必须使用适当的子集选择运算符(.loc/.iloc)在列表中显式声明目标列名/索引。在

例如:

enter image description here

注意:在live-ML项目中,测试数据是指当您的模型准备好在产品化阶段进行调优时,将来到达或实时收集的数据。在

一个标准化的训练测试特征集,如X_train, X_test有助于比较特征变量与平均值之间的差异,也有助于对特征变量进行标准化的正则化主成分分析技术。在

您应该使用y-scaler的inverse_transform方法。请注意,您可以使用管道更简洁地完成所有这些,如下所示

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR

pipeline = Pipeline([('scaler', StandardScaler()), ('estimator', SVR(kernel="rbf"))])

y_scaler = StandardScaler()
y_train = y_scaler.fit_transform(y_train)
pipeline.fit(x_train, y_train)
y_pred = y_scaler.inverse_transform(pipeline.predict(x_test))

许多人只会在全球范围内扩大目标,而不会太过适应。但你不上当是好事。AFAIK对y数据使用单独的定标器(如代码中所示)是唯一的方法。在

我知道这个问题很古老,当时的答案是正确的,但是现在有一种scikit-learn方法来做这个。在

http://scikit-learn.org/dev/modules/compose.html#transforming-target-in-regression

相关问题 更多 >