[Python] sklearn's model.predict_proba() does not seem to make the same prediction as...

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    From my research (see e.g. here), model.predict_proba(X) should return class probabilities under the same order as the classes listed with model.classes. However, when I test this by comparing the outputs of model.predict(X) and model.predict_proba(X), I become confused.

    Here is an example:

    from sklearn.tree import DecisionTreeClassifier
    model = sklearn.tree.DecisionTreeClassifier(criterion='gini', max_depth=10)
    model.fit(X_train, y_train)
    print('Predicted class:', model.predict(X_test)) # X_test is single vector
    print('Predicted probabilities:', model.predict_proba(X_test))
    print('Corresponding classes:', model.classes_)


    The output is:

    Predicted class: ['Class B']
    Predicted probabilities: [0.33514444 0.28186712 0.38298844]
    Corresponding classes: ['Class B' 'Class C' 'Class A']


    Clearly, the predicted class should be Class A, since Class A was assigned the highest probability. However, the predicted class is Class B. Am I missing something? If I am wrong that the ordering of predicted probabilities should be the same as the ordering of model.classes_, how can I find the classes that correspond to each probabilities output by predict_proba?

    Continue reading...

Compartilhe esta Página