Ayush Agrawal
Ayush Agrawal

Reputation: 75

Random forest classifier result from Predict_proba() does not match with predict()?

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer

from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
('features', FeatureUnion([
    ('Comments',Pipeline([
        ('selector',ItemSelector(column = "Comments")),
        ('tfidf',TfidfVectorizer(use_idf=False,ngram_range=(1,2),max_df=0.95, min_df=0,sublinear_tf=True)),
    ])),
    ('Vendor', Pipeline([
        ('selector',ItemSelector(column = "Vendor Name")),
        ('tfidf',TfidfVectorizer(use_idf=False)),

    ]))
])),
('clf',RandomForestClassifier(n_estimators =200, max_features='log2',criterion = 'entropy',random_state = 45))
 #('clf',LogisticRegression())
 ])


X_train, X_test, y_train, y_test = train_test_split(X,
                                df['code Description'],
                                test_size = 0.3, 
                                train_size = 0.7,
                                random_state = 100)
model = pipeline.fit(X_train, y_train)
s = pipeline.score(X_test,y_test)
pred = model.predict(X_test)
predicted =model.predict_proba(X_test)

for some of classification my predict is matching with prediction score. but in some cases,

proba_predict = [0.3,0.18,0.155]

but instead of classifying it as class A, it is classifying as Class B.

Predict class: B

Actual Class : A

Right side column is my labels and left side column is my input text data:

enter image description here

Upvotes: 0

Views: 7019

Answers (1)

Merlin1896
Merlin1896

Reputation: 1821

I think that you state the following situation: For a test vector X_test you find a predicted probability distribution y=[p1, p2, p3] from the predict_proba() method with p1>p2 and p1>p3 but the predict() method does not output class 0 for this vector.

If you inspect the source code of the predict function of sklearn's RandomForestClassifier, you will see that the predict_proba() method of the RandomForest is called there:

proba = self.predict_proba(X)

From these probabilities, the argmax is used to output the class.

Hence, the prediction step uses the predict_proba method for its output. For me it seems impossible that anything goes wrong there.

I would assume that you mixed up some class names in your routine and got confused there. But it is not possible to give a more detailed answer based on the information you provided.

Upvotes: 1

Related Questions