Could someone give me a tip on how to use Scikit MinMaxScaler
when predicting with an MLP neural network?
I know this part of the code at the very end isn't right, the data used to #make single prediction
is not getting scaled like training data did. I get confused on how data gets scaled to train, data gets scaled to predict, but then would the model output y_pred
get unscaled so it makes sense? Assuming that model output data needs to get unscaled if model input data gets scaled...
#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)
Entire script below, any other tips to make this better also greatly appreciated not alot of wisdom here.
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', 'OAT', '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, kernel_initializer='normal', activation='relu'))
model.add(Dense(3, kernel_initializer='normal', 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)