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

Keras中是否需对Y_train缩放?回归神经网络标签处理问询

Should Y_train Be Scaled or Kept as Original Values in Regression Neural Networks?

Great question—this is one of those practical details that trips up a lot of folks when building regression models with neural networks. Let’s break this down clearly:

Short Answer

It depends on the range and distribution of your Y values, but in most cases, scaling Y_train will lead to better, more stable model training. Only skip scaling if your Y values are already in a narrow, manageable range (like 0 to 1 or 0 to 5).

Why Scaling Y Matters

Neural networks rely on gradient descent to learn, and large or wildly varying Y values can throw off this process:

  • If Y is in a huge range (e.g., house prices from $10k to $1M), the model’s loss function will prioritize large Y values, making it harder to learn patterns in smaller ones.
  • Extreme Y values can cause unstable gradients—either vanishing (too small to update weights) or exploding (updates are too big, leading to erratic training).
  • Most activation functions (like sigmoid, which caps at 0-1) have limited output ranges; scaling Y to match or align with these ranges helps the model map inputs to outputs more effectively.

When to Scale Y_train

  • Y has a large range: E.g., sales figures from $100 to $100k, temperature readings from -50°C to 50°C. Use scalers like MinMaxScaler (to 0-1) or StandardScaler (to mean 0, std 1).
  • Y is skewed: If your target variable follows a non-normal distribution (like right-skewed sales data), scaling (or even log-transforming first, then scaling) can help the model learn more evenly.

When You Might Skip Scaling

  • Y is already in a narrow range: If your target is, say, a satisfaction score from 0 to 1, or a percentage from 0% to 100%, scaling may not provide much benefit. That said, it rarely hurts to scale anyway—many practitioners still do it for consistency with X data.

Example: Scaling Y with MinMaxScaler

Here’s how you’d implement this in Python (note the critical step of inverse-scaling predictions to get back to original values):

from sklearn.preprocessing import MinMaxScaler
import numpy as np

# Assume Y_train is a 1D numpy array
Y_train = np.array([10000, 50000, 150000, 80000])

# Initialize scaler for 0-1 range
scaler_y = MinMaxScaler(feature_range=(0, 1))
# Reshape Y to 2D (required by scikit-learn scalers)
Y_train_scaled = scaler_y.fit_transform(Y_train.reshape(-1, 1))

# After training, predict and inverse-scale to get original values
Y_pred_scaled = model.predict(X_test_scaled)
Y_pred_original = scaler_y.inverse_transform(Y_pred_scaled)

Critical Notes

  • Never fit the scaler on test data: Always use only the training data to fit your scaler—using test data would introduce data leakage, which ruins your model’s generalization ability.
  • Evaluate with original values: When calculating metrics like MAE or RMSE, use the inverse-scaled predictions and original Y_test values—metrics on scaled Y won’t reflect real-world performance.

内容的提问来源于stack exchange,提问作者Per favore

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:19:56