With Keras Sequential Model Prediction To get Class Labels we can do

yhat_classes1 = Keras_model.predict_classes(predictors)[:, 0] #this shows deprecated warning in tf==2.3.0

WARNING:tensorflow:From <ipython-input-54-226ad21ffae4>:1: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).


yhat_classes2 = np.argmax(Keras_model.predict(predictors), axis=1)

With the first class labels if i create confusion matrix, i get

matrix = confusion_matrix(actual_y, yhat_classes1)
 [[108579   8674]
 [  1205  24086]]

But with the second class labels with the confusion matrix, i get 0 for True Positive and False Positive

matrix = confusion_matrix(actual_y, yhat_classes2)
 [[117253      0]
 [ 25291      0]]

May I know whats the issue here?

  • $\begingroup$ Can you use np.argmax() method to get both yhat_classes1 and yhat_classes2. $\endgroup$ Aug 4 '20 at 10:10
  • $\begingroup$ (model.predict(x) > 0.5).astype("int32") this works, but the warning message information doesn't work $\endgroup$
    – hanzgs
    Aug 5 '20 at 0:40

Model.predict_classes gives you the most likely class only (highest probability value), therefore it is of dimension (samples,), for every input in sample there is one output - the class.

To be more precise Model.predict_classes calls argmax on the output of predict. See the code (of predict_classes below)

def predict_classes(self, X, batch_size=128, verbose=1):
  proba = self.predict(X, batch_size=batch_size, verbose=verbose)
        if self.class_mode == 'categorical':
            return proba.argmax(axis=-1)
            return (proba > 0.5).astype('int32')

As the warning suggest, Please use instead:

  • np.argmax(model.predict(x), axis=-1), if your model does multi-class classification (e.g. if it uses a softmax last-layer activation).
  • (model.predict(x) > 0.5).astype("int32"), if your model does binary classification (e.g. if it uses a sigmoid last-layer activation).
  • 1
    $\begingroup$ This answer should be accepted. $\endgroup$
    – Juneyee
    Oct 7 '20 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.