0

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)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
bbartling
  • 3,288
  • 9
  • 43
  • 88
  • Not exactly clear what the *question* is. Care to elaborate? – desertnaut Mar 18 '21 at 21:24
  • Any chance I could get a tip on how to incorporate MinMaxScaler and make a prediction? I have heard people should scale data for NN but I dont know how to make a prediction with a model that is trained on scaled data – bbartling Mar 19 '21 at 13:13
  • All preprocessing made for your training data should also be applied to any data subsequently fed to the model; here something like `min_max_scaler.transform(vals)`. Not much else we can say without further details. If you face issues, please post a [mcve] focused on that part (the details of the NN model are largely irrelevant as long as the type & format of the data is clear). – desertnaut Mar 19 '21 at 13:28
  • To understand the modeled results `y_pred` would someone need to un-transform the data back to its original scale? I think I need to apply `inverse_transform` right? https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html#sklearn.preprocessing.MinMaxScaler.inverse_transform – bbartling Mar 19 '21 at 13:34
  • It always depends on what exactly you are after, i.e. the specific (business) case. For an example when you want to "translate" the error to the scale (and units) of your target output, see own answer here: https://stackoverflow.com/questions/48973140/how-to-interpret-mse-in-keras-regressor/49009442#49009442 – desertnaut Mar 19 '21 at 13:37
  • In any case, and since this is not a *programming* question but a methodology one, if you still have issues I suggest you post at Data Science SE instead. – desertnaut Mar 19 '21 at 13:42
  • Okay thanks for the help – bbartling Mar 19 '21 at 13:44
  • No problem. And just in case you find the linked answer useful, upvotes are always welcome ;) – desertnaut Mar 19 '21 at 13:45
  • Yeah absolutely my use case is similar not in dollars $ but in units of electricity kW – bbartling Mar 19 '21 at 13:46
  • oh yeah..... ;) – desertnaut Mar 19 '21 at 13:52
  • Would recommend using `StandardScaler` versus `MinMax` for NN? I noticed that on the link you sent – bbartling Mar 19 '21 at 14:23
  • I used Standard Scaler there only because this is what OP asked about, not because I recommend it. Such choices always depend on the problem details, and I do not have an opinion. – desertnaut Mar 19 '21 at 14:32

0 Answers0