You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

回归任务中如何将模型预测值还原为真实温度值?

将模型预测的缩放温度值还原为真实温度值

我用One-hot编码器处理类别特征,MinMaxScaler处理数值特征,训练了一个回归模型预测温度。现在模型输出的是缩放后的预测值,想把它转成真实温度值。我的特征工程和数据划分代码如下:

# Feature Engineering steps (scaling and encoding)
numerical_cols = data.select_dtypes(include=['number']).columns
categorical_cols = data.select_dtypes(exclude=['number']).columns

scaler = MinMaxScaler()
data[numerical_cols] = scaler.fit_transform(data[numerical_cols])

data = pd.get_dummies(data, columns=categorical_cols, drop_first=True)

# Reverse the temperature scaling to get original values
temperature_scaler = MinMaxScaler()
temperature = data['Temperature set at home '].values.reshape(-1, 1)
data['Temperature set at home '] = temperature_scaler.fit_transform(temperature)

# Split the data into features (X) and target variable (y)
X = data.drop(columns=['Temperature set at home '])
y = data['Temperature set at home ']

# Split the data into training (X_train) and testing sets (X_test, y_train, y_test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

解决方案

你已经为目标变量单独训练了temperature_scaler,直接用它的inverse_transform()方法就能还原缩放后的预测值,步骤如下:

  1. 获取模型输出的缩放后预测值
  2. 将预测值转换为MinMaxScaler要求的二维数组格式
  3. 调用inverse_transform()得到真实温度值

示例代码:

# 假设model是你训练完成的回归模型
y_pred_scaled = model.predict(X_test)

# 转换为二维数组(MinMaxScaler的输入要求)
y_pred_scaled_2d = y_pred_scaled.reshape(-1, 1)

# 还原为真实温度值
y_pred_actual = temperature_scaler.inverse_transform(y_pred_scaled_2d)

注意事项

  • 必须使用训练目标变量时的同一个temperature_scaler,不能重新拟合新的scaler,否则还原结果会失真
  • 对新数据预测前,要确保新数据的特征处理(数值缩放、类别编码逻辑)和训练集完全一致,再用模型预测,最后用同一temperature_scaler还原

内容的提问来源于stack exchange,提问作者Ahmad Aladawi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:07:20