sklearn - Predict each class's probability

bmc picture bmc · Nov 6, 2017 · Viewed 10k times · Source

So far I have resourced another post and sklearn documentation

So in general I want to produce the following example:

X = np.matrix([[1,2],[2,3],[3,4],[4,5]])
y = np.array(['A', 'B', 'B', 'C', 'D'])
Xt = np.matrix([[11,22],[22,33],[33,44],[44,55]])
model = model.fit(X, y)
pred = model.predict(Xt)

However for output, I would like to see 3 columns per observation as output from pred:

 A  |  B  |  C
.5  | .2  | .3
.25 | .25 | .5
...

and a different probability for each class showing up in my prediction.

I believe that the best approach would be Multilabel classification from the second link I provided above. Additionally, I think it might be a good idea to hop into one of the multi-label or multi-output models listed below:

Support multilabel:

    sklearn.tree.DecisionTreeClassifier
    sklearn.tree.ExtraTreeClassifier
    sklearn.ensemble.ExtraTreesClassifier
    sklearn.neighbors.KNeighborsClassifier
    sklearn.neural_network.MLPClassifier
    sklearn.neighbors.RadiusNeighborsClassifier
    sklearn.ensemble.RandomForestClassifier
    sklearn.linear_model.RidgeClassifierCV

Support multiclass-multioutput:

    sklearn.tree.DecisionTreeClassifier
    sklearn.tree.ExtraTreeClassifier
    sklearn.ensemble.ExtraTreesClassifier
    sklearn.neighbors.KNeighborsClassifier
    sklearn.neighbors.RadiusNeighborsClassifier
    sklearn.ensemble.RandomForestClassifier

However, I am looking for someone who is has more confidence and experience at doing this the right way. All feedback is appreciated.

-bmc

Answer

Maksim Khaitovich picture Maksim Khaitovich · Nov 7, 2017

From what I understand you want to obtain probabilities for each of the potential classes for multi-class classifier.

In Scikit-Learn it can be done by generic function predict_proba. It is implemented for most of the classifiers in scikit-learn. You basically call:

clf.predict_proba(X)

Where clf is the trained classifier. As output you will get a decimal array of probabilities for each class for each input value.

One word of caution - not all classifiers naturally evaluate class probabilities. For instance, SVM doesn't do that. You still can obtain the class probabilities though, but to do that upon constructing such classifiers you need to instruct it to perform probability estimation. For SVM it would look like:

SVC(Probability=True)

After you fit it you will be able to use predict_proba as before.

I need to warn you that if classifier doesn't naturally evaluate probabilities that means that the probabilities will be evaluated using rather expansive computational methods which may significantly increase training time. So I advice you to use classifiers which naturally evaluate class probabilities (neural networks with softmax output, logistic regression, gradient boosting etc)