MinMaxScaler 用于训练后的预测

问题描述

有人能告诉我在使用 MLP 神经网络进行预测时如何使用 Scikit MinMaxScaler 的技巧吗?

我知道最后这部分代码是不正确的,用于 #make single prediction 的数据没有像训练数据那样缩放。我对数据如何缩放以进行训练、数据如何缩放以进行预测感到困惑,但是模型输出 y_pred 是否会未缩放所以有意义?假设如果模型输入数据被缩放,模型输出数据需要被取消缩放......

#make single prediction
vals = np.array([55,55])

if (vals.ndim == 1):
    vals = np.array([vals])

y_pred = model.predict(vals)
print('prediction is ',y_pred)

下面的整个脚本,任何其他使脚本更好的提示也非常感谢这里没有很多智慧。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import backend

from sklearn.preprocessing import MinMaxScaler

min_max_scaler = MinMaxScaler()


# This function keeps the learning rate at 0.001
# and decreases it exponentially after that.
def scheduler(epoch):
  if epoch < 1:
    return 0.001
  else:
    return 0.001 * tf.math.exp(0.01 * (1 - epoch))

callback = tf.keras.callbacks.LearningRateScheduler(scheduler)


#function to calculate RSME
def rmse(y_true,y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true),axis=-1))
 

df = pd.read_csv('https://raw.githubusercontent.com/bbartling/Data/master/colabData.csv',index_col='Date',parse_dates=True)
print(df.info())

df[['kW','OAT','AHU_01_discharge_Fan_01_Speed']] = min_max_scaler.fit_transform(df[['kW','AHU_01_discharge_Fan_01_Speed']])


# split into input (X) and output (Y) variables
X = df.drop(['kW'],1)
Y = df['kW']

#define training & testing data set
offset = int(X.shape[0] * 0.8)
X_train,Y_train = X[:offset],Y[:offset]
X_test,Y_test = X[offset:],Y[offset:]


#define model
model = Sequential()
model.add(Dense(11,input_dim=2,kernel_initializer='normal',activation='relu'))
model.add(Dense(5,activation='relu'))
model.add(Dense(3,activation='relu'))
model.add(Dense(1,kernel_initializer='normal'))
model.summary()
model.compile(loss='mse',optimizer='adam',metrics=[rmse])


#train model,test callback option
history = model.fit(X_train,Y_train,epochs=2,batch_size=1,verbose=2,callbacks=[callback])


#make single prediction
vals = np.array([55,55])
if (vals.ndim == 1):
    vals = np.array([vals])

y_pred = model.predict(vals)

print('prediction is ',y_pred)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)