I have some probability output of 5 models, and I sum of the probabilities element by element like below:
probs = [None] * 5
for i in range(0,5):
probs[i] = models[i].predict_proba(X)
probs[0] + probs[1] + probs[2] + probs[3] + probs[4]
This works fine.
I then tried to simplified the above code a bit by doing below:
probs = [None] * 5
results = [None]
for i in range(0,5):
probs[i] = models[i].predict_proba(X)
results += probs[i]
results
But got the following error:
TypeError unsupported operand type(s) for +: 'NoneType' and 'float'
TypeErrorTraceback (most recent call last)
<ipython-input-20-8d4d443a7428> in <module>()
4 for i in range(0,5):
5 probs[i] = models[i].predict_proba(X)
----> 6 results += probs[i]
7
8 results
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
How could I fix such error? Thanks.
Note:
probs[i] is of format:
array([[ 9.99877759e-01, 1.22241455e-04],
[ 9.99694005e-01, 3.05994629e-04],
[ 9.47546608e-01, 5.24533925e-02],
[ 1.83994998e-01, 8.16005002e-01],
[ 9.66928729e-01, 3.30712706e-02],
[ 9.99487283e-01, 5.12717255e-04],
[ 2.85824823e-03, 9.97141752e-01],
[ 9.97979081e-01, 2.02091861e-03],
[ 9.99744813e-01, 2.55186665e-04]])
results = 0and your code will run fine. However, you should really consider the answers below as they are more pythonic.