I am working with scikit-learn library in python. In the code below, I am predicting probability but I don't know how to read the output.

Testing data

from sklearn.ensemble import RandomForestClassifier as RF
from sklearn import cross_validation

X = np.array([[5,5,5,5],[10,10,10,10],[1,1,1,1],[6,6,6,6],[13,13,13,13],[2,2,2,2]])
y = np.array([0,1,1,0,1,2])

Split the dataset

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.5, random_state=0) 

Calculate the probability

clf = RF()
pred_pro = clf.predict_proba(X_test)
print pred_pro

The output

[[ 1.  0.]
 [ 1.  0.]
 [ 0.  1.]]

The X_test list contains 3 arrays (I have 6 samples and test_size=0,5) so output has 3 too.

But I am predicting 3 values (0,1,2) so why I am getting only 2 elements in each array?

How should I read the output?

I also noticed, when I modify the number of distinct values in y, number of columns in output is always distinct count of y -1.

  • $\begingroup$ Welcome to CrossValidated. Did you see my answer below? If it solved your question, go ahead and mark it as the correct answer. Otherwise, let me know what's missing and I'll try to clear it up for you. $\endgroup$
    – Ben
    Nov 3, 2015 at 21:09

1 Answer 1


Take a look at y_train. It is array([0, 0, 1]). This means your split didn't pick up the sample where y=2. So, your model has no idea that the class y=2 exists.

You need more samples for this to return something meaningful.

Also check out the docs to understand how to interpret the output.

  • 1
    $\begingroup$ This is correct. If you set y = np.array([0,2,1,0,1,2]) and random_state=2 you'll now see 3 columns of output $\endgroup$
    – tdc
    Nov 3, 2015 at 16:35
  • $\begingroup$ The answer solved my question. Thank you very much. And in which order are the columns please? Its always ascending? $\endgroup$
    – HonzaB
    Nov 4, 2015 at 7:43
  • $\begingroup$ Run clf.classes_. Columns will be in that order. $\endgroup$
    – Ben
    Nov 4, 2015 at 14:26
  • $\begingroup$ Just like this: clf.fit(X_train,y_train).classes_? $\endgroup$
    – HonzaB
    Nov 5, 2015 at 12:45
  • 1
    $\begingroup$ I think that'll work but you can just run clf.classes_ after you run clf.fit(X_train,y_train) $\endgroup$
    – Ben
    Nov 5, 2015 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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