27

What is the difference between predict and predict_class functions in keras?

Why does Model object don't have predict_class function?

nbro
  • 15,395
  • 32
  • 113
  • 196
hyqdvd
  • 281
  • 1
  • 3
  • 3

3 Answers3

47

predict will return the scores of the regression and predict_class will return the class of your prediction. Although it seems similar, there are some differences:

Imagine you are trying to predict if the picture is a dog or a cat (you have a classifier):

  • predict will return you: 0.6 cat and 0.4 dog (for example).
  • predict_class will return the index of the class having maximum value. For example, if cat is 0.6 and dog is 0.4, it will return 0 if the class cat is at index 0)

Now, imagine you are trying to predict house prices (you have a regressor):

  • predict will return the predicted price
  • predict_class will not make sense here since you do not have a classifier

TL:DR: use predict_class for classifiers (outputs are labels) and use predict for regressions (outputs are non-discrete)

Hope it helps!

For your second question, the answer is here

Rahul
  • 5
  • 5
Ignacio Peletier
  • 2,056
  • 11
  • 24
  • 4
    if 'predict_class' function is used for classification task, why does Model object don't have this function? – hyqdvd Jul 18 '18 at 13:49
  • I encountered this problem when I was browsing the stackoverflow, not when I was coding. I found it a bit strange because the task types handled by the two models should be similar. Since the Model object can deal with classification task, it should have 'predict_classes' function. – hyqdvd Jul 18 '18 at 14:41
  • can you supply more info? links maybe? – Ignacio Peletier Jul 19 '18 at 11:33
  • https://stackoverflow.com/questions/44806125/attributeerror-model-object-has-no-attribute-predict-classes – hyqdvd Jul 20 '18 at 05:47
  • This is the link. It just said there is no 'predict_classes' function in Model object, but it didn't say why. Anyway, thank you all the same. – hyqdvd Jul 20 '18 at 05:50
  • 1
    The anwser of your question is the answer of the post you gave me, what is it you dont understand? (Trying to help here). This is the answer: `The predict_classes method is only available for the Sequential class (which is the class of your first model) but not for the Model class (the class of your second model). With the Model class, you can use the predict method which will give you a vector of probabilities and then get the argmax of this vector (with np.argmax(y_pred1,axis=1)).` – Ignacio Peletier Jul 20 '18 at 06:28
8

predict_classes method is only available for the Sequential class but not for the Model class You can check this answer

Harsh Gupta
  • 135
  • 2
  • 11
1

Why does Model object don't have predict_class function?

The answer was given here in this github issue. (Nevertheless, this is still a very complex explanation. Any help is welcomed)

For models that have more than one output, these concepts are ill-defined. And it would be a bad idea to make available something in the one-output case but not in other cases (inconsistent API).

For the Sequential model, the reason this is supported is for backwards compatibility only.

Predict_class is missing from the functional API.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
pyjavo
  • 1,598
  • 2
  • 23
  • 41