I have a model what looks like this

Product_names= [
    'Dress white',
    'Pullover shirt',


category_labels = [

I use the MultinomialNB classifier to predict a new product name into a category. Before it gets predicted i want to get the probability of the prediction.

So in psuedo it looks likes this:

clf = MultinomialNB()  
clf.predict_proba('White Pullover shirt')

What I get is this:

[4.18600796e-03 7.46021220e-04 4.14456233e-05 4.14456233e-05
 1.16047745e-03 6.92141910e-03 3.70938329e-03 1.78216180e-03
 1.49204244e-03 2.15517241e-03 1.03614058e-04 3.48143236e-03
 3.27420424e-03 7.66744032e-04 1.03614058e-03 8.91080902e-04
 8.49635279e-04 2.07228117e-04 4.14456233e-05 4.14456233e-05
 2.44529178e-03 1.84433024e-03 1.67854775e-03 1.01541777e-03
 5.28431698e-03 1.03614058e-03 1.45059682e-04 8.28912467e-05
 9.53249337e-04 1.86505305e-03 2.59035146e-03 2.32509947e-02
 1.24336870e-04 6.21684350e-05 2.69396552e-04 1.90028183e-02
 6.83852785e-04 8.28912467e-05 2.07228117e-05 8.91080902e-04
 5.80238727e-03 3.39854111e-03 1.11488727e-02 6.21684350e-05
 3.31564987e-04 8.18551061e-03 7.46021220e-04 3.52287798e-04
 6.21684350e-05 1.50862069e-02 2.48673740e-04 1.53141578e-02
 4.64190981e-03 4.14456233e-05 2.27950928e-04 1.73242706e-02
 8.89008621e-03 4.14456233e-04 1.28481432e-03 1.65782493e-04
 3.99950265e-03 7.41876658e-03 3.31564987e-04 1.90649867e-03
 1.24336870e-04 7.39804377e-03 1.07758621e-03 6.21684350e-05
 3.39854111e-03 2.19661804e-03 3.85444297e-03 1.88577586e-03
 3.56432361e-03 1.03614058e-03 2.07228117e-05 4.35179045e-04
 6.90069629e-03 1.86505305e-04 2.27950928e-03 2.90119363e-04
 4.39323607e-03 4.14456233e-05 5.18070292e-04 1.80288462e-03
 4.14456233e-05 4.10311671e-03 3.93733422e-04 4.53829576e-03
 6.21684350e-05 1.80288462e-03 5.38793103e-04 2.01011273e-03
 3.68037135e-02 3.50008289e-02 2.63386936e-02 9.82261273e-03
 1.75729443e-02 2.89497679e-02 1.78423408e-02 2.69396552e-03
 3.04418103e-02 4.35179045e-03 3.29492706e-03 1.59565650e-03
 1.67854775e-03 1.58115053e-02 1.83604111e-02 2.34375000e-02
 1.50033156e-02 1.38221154e-02 4.66263263e-03 1.92722149e-03
 1.59565650e-03 1.09830902e-03 3.43998674e-03 2.17589523e-03
 3.81299735e-03 1.11281499e-02 1.45059682e-04 1.91271552e-02
 1.96866711e-03 4.55901857e-04 5.80238727e-04 6.21684350e-05
 1.86505305e-04 6.15467507e-03 8.84864058e-03 3.73010610e-04
 1.24336870e-03 7.04575597e-04 1.03614058e-04 7.66744032e-04
 1.24336870e-04 4.14456233e-04 2.07228117e-04 4.97347480e-04
 1.61637931e-03 1.45059682e-04 1.20192308e-03 3.43998674e-03
 1.24336870e-04 3.00480769e-03 1.71999337e-03 1.03614058e-04
 2.07228117e-03 3.33637268e-03 1.69927056e-03 2.56962865e-03
 3.21203581e-03 5.38793103e-04 2.92191645e-03 4.24817639e-03
 4.90508952e-02 3.35709549e-03 6.00961538e-04 2.27950928e-04
 6.19612069e-03 1.59565650e-02 4.14456233e-03 1.52934350e-02
 8.70358090e-04 8.28912467e-05 1.24336870e-04 1.86505305e-03
 8.28912467e-05 1.80288462e-03 1.99767905e-02 2.63179708e-03
 2.69396552e-04 8.35129310e-03 7.08720159e-03 3.10842175e-04
 2.96336207e-03 3.46070955e-03 1.13975464e-03 3.58504642e-03
 5.59515915e-04 2.23806366e-03 2.07228117e-04 4.43468170e-03
 1.40915119e-02 1.15011605e-02 4.18600796e-03 4.20673077e-03
 7.41876658e-03 5.22214854e-03 2.07228117e-05 1.09001989e-02
 1.69927056e-03 1.45059682e-02 6.77635942e-03 1.46095822e-02
 3.25969828e-02 2.21734085e-03 9.94694960e-04 6.21684350e-05
 1.94794430e-03 3.62649204e-03 4.31034483e-03 3.25348143e-03
 1.28481432e-03 3.31564987e-04 3.93733422e-04 1.03614058e-04
 1.84433024e-03 1.71999337e-03 1.45059682e-04 2.73541114e-03
 2.25878647e-03 2.92191645e-03 3.31564987e-04 1.07758621e-03
 2.27950928e-04 1.65782493e-04 4.35179045e-04 1.26409151e-03
 1.51276525e-03 2.48673740e-04 4.14456233e-05 2.07228117e-04
 2.79757958e-03 8.28912467e-05 2.30023210e-03 1.24336870e-03
 3.31564987e-04 6.21684350e-05 9.11803714e-04 2.07228117e-05
 4.35179045e-04 2.07228117e-05 2.27950928e-04 1.16047745e-03
 9.73972149e-04 3.31564987e-04 3.64721485e-03]

Its an array of 235 items, It makes sense, because if i group my dataset on category it contains 235 categories. But its missing the Category label with the probability. Does someone know why? Or can someone give me the right directions?

I want to create a flask web application where i can see the probability of the prediction, if its greater then 0.80% for example i can assign it.

Can someone help me with this? I am struggling a couple of days with this issue now :(.

My complete code looks like this

import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import string
from nltk.corpus import stopwords

#open file
data = pd.read_csv('cats.csv',sep=';')
data['length'] = data['Product Name'].str.len()

#remove all puncs
def text_process(mess):
    # Check characters to see if they are in punctuation
    nopunc = [char for char in mess if char not in string.punctuation]
    # Join the characters again to form the string.
    nopunc = ''.join(nopunc)
    # Now just remove any stopwords
    return [word for word in nopunc.split() if word.lower() not in stopwords.words('english') if word.lower() not in stopwords.words('dutch')]

# Might take awhile...
bow_transformer = CountVectorizer(analyzer=text_process).fit(data['Product Name'])

# Print total number of vocab words
messages_bow = bow_transformer.transform(data['Product Name'])
tfidf_transformer = TfidfTransformer().fit(messages_bow)
messages_tfidf = tfidf_transformer.transform(messages_bow)

from sklearn.naive_bayes import MultinomialNB
spam_detect_model = MultinomialNB().fit(messages_tfidf, data['Category Path'])

message4 = "Some input data from flask web app "
bow4 = bow_transformer.transform([message4])
tfidf4 = tfidf_transformer.transform(bow4)

predicted =  spam_detect_model.predict_proba(tfidf4)[0]


  • $\begingroup$ I think the labels are not stored in the model. You may need to re-attach names to the predicted classes. $\endgroup$ – Peter Jan 2 at 19:35

Found the solution, with the .classes_ attribute you can find the predicted classes. Then you have to concatenate the predict_proba together with the .classes_.

In Python it looks like this

message4 = "Jeans model Ornella met iets kortere pijpen Van"
bow4 = bow_transformer.transform([message4])
tfidf4 = tfidf_transformer.transform(bow4)
counter = 0
predicted = spam_detect_model.predict_proba(tfidf4)
for x in spam_detect_model.classes_:
  proba  = round(predicted[0][counter],2)
  if proba > 0.01:
      print(x + ' probility '+ str(proba) + '%')
  counter +=1

What returns, depending on the if statement:

Dames%26gt%3BKleding%26gt%3BJeans%26gt%3BBoyfriend+Jeans probility 0.04%
Dames%26gt%3BKleding%26gt%3BJeans%26gt%3BMom+Jeans probility 0.12%
Dames%26gt%3BKleding%26gt%3BJeans%26gt%3BSkinny+Fit+Jeans probility 0.26%
Dames%26gt%3BKleding%26gt%3BJeans%26gt%3BStraight+Fit+Jeans probility 0.02%
Dames%26gt%3BKleding%26gt%3BPlus+Size%26gt%3BJeans probility 0.03%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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