Search code examples
pythonscikit-learnmulticlass-classification

Retrieve non zero class probabilities from predict_proba()


I want to retrieve only the non zero class probabilities. My code below keeps generating the following error

enter image description here

print(clf.predict(xtest))
pp = clf.predict_proba(xtest)[0] 
pp[:] = ([ind,value] for ind,value in enumerate(pp) if value > 0)

for ind,val in enumerate(pp):
    print('\t',clf.classes_[pp[ind][0]],'->',pp[ind][1])
print('\n\n\n\n')

Solution

  • Try this!

    pp = clf.predict_proba(xtest)[0] 
    pp = [[ind,value] for ind,value in enumerate(pp) if value > 0]
    

    you were over writing elements of float array with a generator.

    If you remove [:], you can store it as a generator, but it will not allow indexing. Hence try using list (square braces).