微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MinMaxScaler 用于训练后的预测

如何解决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 举报,一经查实,本站将立刻删除。