通常,您会重新调整自变量(特征),因为比例差异会影响模型计算,但您尝试预测的因变量通常不会受到影响。通常没有理由重新缩放因变量,并且缩放它会使结果非常难以解释。
StandardScaler 类文档的第一行甚至指定了这么多:
通过去除均值并缩放到单位方差来标准化特征
您也可以选择缩放标签,但通常不需要这样做。
所以我会代替你做的是(假设你的原始数据框包含 3 个自变量和 1 个目标变量)是这样的:
X = some_df.iloc[:, :3].values
y = some_df.iloc[3].values
scaler = StandardScaler()
X = scaler.fit_transform(X)
# And then goes everything as usual
现在,当您要预测值时,您只需使用缩放器以与以前相同的方式转换输入。
不过,更好的方法是在模型中添加标准化层作为预处理步骤。这样,您只需将原始数据输入估算器,它就会为您处理所有细节。同样,您在生成预测时不需要对数据进行规范化,模型将为您完成所有工作。您可以添加如下内容:
from tensorflow.keras.layers.experimental.preprocessing import Normalization
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras import Model
# this is your default batch_size
BATCH_SIZE = 128
# Here's your raw (non-normalized) X data
X = some_df.iloc[:, :3].values
norm = Normalization()
norm.adapt(X)
preprocess = Sequential([
Input(shape=(BATCH_SIZE, 3)),
norm
])
# Now finally, when you build your actual model you add
# pre-processing step in the beginning
inp = preprocess()
x = Dense(64)(input)
x = Dense(128)(x)
x = Dense(1)(x)
model = Model(inputs=inp, outputs=x)
这里的预处理步骤是模型本身的一部分,所以一旦你这样做了,你可以直接提供原始数据而无需任何额外的转换。
这就是它的作用:
# Skipping the imports as they are the same as above + numpy
X = np.array([[1, 2, 3], [10, 20, 40], [100, 200, 400]])
norm = Normalization()
norm.adapt(X)
preprocess = Sequential([
Input(shape=(3, 3)),
norm
])
x_new = preprocess(X)
print(x_new)
Out: tf.Tensor(
[[-0.80538726 -0.80538726 -0.807901 ]
[-0.60404044 -0.60404044 -0.6012719 ]
[ 1.4094278 1.4094278 1.4091729 ]], shape=(3, 3), dtype=float32)