将 Keras 模型的输出重新缩放回原始比例
Re-scaling outputs from a Keras model back to original scale
我是神经网络的新手(只是免责声明)。
我有一个基于 8 个特征预测混凝土强度的回归问题。我首先做的是使用最小-最大归一化重新缩放数据:
# Normalize data between 0 and 1
from sklearn.preprocessing import MinMaxScaler
min_max = MinMaxScaler()
dataframe2 = pd.DataFrame(min_max.fit_transform(dataframe), columns = dataframe.columns)
然后将dataframe转换为numpy数组并将其拆分为X_train、y_train、X_test、y_test。
现在这里是网络本身的 Keras 代码:
from keras.models import Sequential
from keras.layers import Dense, Activation
#Set the params of the Neural Network
batch_size = 64
num_of_epochs = 40
hidden_layer_size = 256
model = Sequential()
model.add(Dense(hidden_layer_size, input_shape=(8, )))
model.add(Activation('relu'))
model.add(Dense(hidden_layer_size))
model.add(Activation('relu'))
model.add(Dense(hidden_layer_size))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', # using the mean squared error function
optimizer='adam', # using the Adam optimiser
metrics=['mae', 'mse']) # reporting the accuracy with mean absolute error and mean squared error
model.fit(X_train, y_train, # Train the model using the training set...
batch_size=batch_size, epochs=num_of_epochs,
verbose=0, validation_split=0.1)
# All predictions in one array
predictions = model.predict(X_test)
问题:
predictions 数组将具有缩放格式的所有值(在 0 和 1 之间),但显然我需要预测是真实的值。如何将这些输出重新调整为实际值?
Min-Max 或 Z-Score 标准化更适合回归问题吗?这个'Batch-Normalization'怎么样?
谢谢,
根据 doc,MinMaxScaler
class 有一个 inverse_transform
方法可以满足您的需求:
inverse_transform(X):根据feature_range.
撤销X的缩放
对于 1.:将 inverse_transform()
与您拥有的相同 MinMaxScaler 使用 fit_transformed 您的原始数据:
我是神经网络的新手(只是免责声明)。
我有一个基于 8 个特征预测混凝土强度的回归问题。我首先做的是使用最小-最大归一化重新缩放数据:
# Normalize data between 0 and 1
from sklearn.preprocessing import MinMaxScaler
min_max = MinMaxScaler()
dataframe2 = pd.DataFrame(min_max.fit_transform(dataframe), columns = dataframe.columns)
然后将dataframe转换为numpy数组并将其拆分为X_train、y_train、X_test、y_test。 现在这里是网络本身的 Keras 代码:
from keras.models import Sequential
from keras.layers import Dense, Activation
#Set the params of the Neural Network
batch_size = 64
num_of_epochs = 40
hidden_layer_size = 256
model = Sequential()
model.add(Dense(hidden_layer_size, input_shape=(8, )))
model.add(Activation('relu'))
model.add(Dense(hidden_layer_size))
model.add(Activation('relu'))
model.add(Dense(hidden_layer_size))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', # using the mean squared error function
optimizer='adam', # using the Adam optimiser
metrics=['mae', 'mse']) # reporting the accuracy with mean absolute error and mean squared error
model.fit(X_train, y_train, # Train the model using the training set...
batch_size=batch_size, epochs=num_of_epochs,
verbose=0, validation_split=0.1)
# All predictions in one array
predictions = model.predict(X_test)
问题:
predictions 数组将具有缩放格式的所有值(在 0 和 1 之间),但显然我需要预测是真实的值。如何将这些输出重新调整为实际值?
Min-Max 或 Z-Score 标准化更适合回归问题吗?这个'Batch-Normalization'怎么样?
谢谢,
根据 doc,MinMaxScaler
class 有一个 inverse_transform
方法可以满足您的需求:
inverse_transform(X):根据feature_range.
撤销X的缩放对于 1.:将 inverse_transform()
与您拥有的相同 MinMaxScaler 使用 fit_transformed 您的原始数据: