将Keras模型的输出重新缩放为原始比例 [英] Re-scaling outputs from a Keras model back to original scale
问题描述
我是神经网络的新手(只是免责声明).
I'm new to neural nets (just a disclaimer).
基于8个特征,我有一个预测混凝土强度的回归问题.我首先要做的是使用最小-最大归一化来重新缩放数据:
I have a regression problem of predicting the strength of concrete, based on 8 features. What I've done first, is rescaled the data using min-max normalization:
# 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)
然后将数据帧转换为numpy数组,并将其拆分为X_train,y_train,X_test,y_test. 现在,这是网络本身的Keras代码:
then converted the dataframe into numpy array and split it into X_train, y_train, X_test, y_test. Now here is the Keras code for the network itself:
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)
问题:
-
预测数组将具有缩放格式的所有值(0到1之间),但是显然我需要将预测值设为真实值.如何将这些输出重新缩放为实际值?
predictions array will have all the values in the scaled format (between 0 and 1), but obviously I would need the predictions to be in their real values. How can I rescale those outputs back to the real values?
Min-Max或Z-Score标准化是否更适合回归问题?那这个批处理规范化"呢?
Is Min-Max or Z-Score standardization more appropriate for regression problems? What about this 'Batch-Normalization'?
谢谢
推荐答案
按照文档,MinMaxScaler
类具有inverse_transform
方法,该方法可以执行您想要的操作:
As per the doc, the MinMaxScaler
class has an inverse_transform
method which does what you want:
inverse_transform(X):根据feature_range撤消X的缩放比例.
inverse_transform(X): Undo the scaling of X according to feature_range.
这篇关于将Keras模型的输出重新缩放为原始比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!