Keras inverse scaling prediction from model causes problems with broadcasting with shapes

Question:

I
have built multi classification model with Keras and after model is finished I would like to predict value for one of my test input.

This is the part where I scaled features:

x = dataframe.drop("workTime", axis = 1)
x = dataframe.drop("creation", axis = 1)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x = pd.DataFrame(sc.fit_transform(x))
y = dataframe["workTime"]

import seaborn as sb
corr = dataframe.corr()
sb.heatmap(corr, cmap="Blues", annot=True)

print("Scaled features:", x.head(3))

Then I did:

y_cat = to_categorical(y)
x_train, x_test, y_train, y_test = train_test_split(x.values, y_cat, test_size=0.2)

And built model:

model = Sequential()

model.add(Dense(16, input_shape = (9,), activation = "relu"))
model.add(Dense(8, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(6, activation = "softmax"))
model.compile(Adam(lr = 0.0001), "categorical_crossentropy", metrics = ["categorical_accuracy"])
model.summary()

model.fit(x_train, y_train, verbose=1, batch_size = 8, epochs=100, shuffle=True)

After my calculation finished, I wanted to take first element from test data and predict
value/classify it.

print(x_test.shape, x_train.shape) // (1550, 9) (6196, 9)
firstTest = x_test[:1]; // [[ 2.76473141  1.21064165  0.18816548 -0.94077449 -0.30981017 -0.37723917
  -0.44471711 -1.44141792  0.20222467]]

prediction = model.predict(firstTest)


print(prediction) // [[7.5265622e-01 2.4710520e-01 2.3643016e-04 2.1405797e-06 3.8411264e-19
  9.4137732e-23]]
print(prediction[0]) // [7.5265622e-01 2.4710520e-01 2.3643016e-04 2.1405797e-06 3.8411264e-19
 9.4137732e-23]

unscaled = sc.inverse_transform(prediction)

print("prediction", unscaled)

During this I retrieve:

ValueError: operands could not be broadcast together with shapes (1,6) (9,) (1,6) 

I think it may be related to my scalers.
And please correct me if I wrong, but what I want to achieve here is to either have one output value which points me how this entry was classified or array of possibilities for each classification label.

Thank you for hints

Asked By: Ernesto

||

Answers:

Your StandardScaler was used to scale the input features, you can’t apply it (or its inverse) on the outputs!

If you are looking for the probabilities of the test sample being in each class, you already have it in prediction[0].

If you want the final class predicted, just take the one with the largest probability with argmax: tf.math.argmax(prediction[0]).

Answered By: ErwanF