Reputation: 38155
For the code below:
def makePrediction(mytheta, myx):
# -----------------------------------------------------------------
pr = sigmoid(np.dot(myx, mytheta))
pr[pr < 0.5] =0
pr[pr >= 0.5] = 1
return pr
# -----------------------------------------------------------------
# Compute the percentage of samples I got correct:
pos_correct = float(np.sum(makePrediction(theta,pos)))
neg_correct = float(np.sum(np.invert(makePrediction(theta,neg))))
tot = len(pos)+len(neg)
prcnt_correct = float(pos_correct+neg_correct)/tot
print("Fraction of training samples correctly predicted: %f." % prcnt_correct)
I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-f0c91286cd02> in <module>()
13 # Compute the percentage of samples I got correct:
14 pos_correct = float(np.sum(makePrediction(theta,pos)))
---> 15 neg_correct = float(np.sum(np.invert(makePrediction(theta,neg))))
16 tot = len(pos)+len(neg)
17 prcnt_correct = float(pos_correct+neg_correct)/tot
TypeError: ufunc 'invert' not supported for the input types, and the inputs
Why is it happening and how can I fix it?
Upvotes: 6
Views: 45497
Reputation: 499
np.invert requires ints or bools, use the method np.linalg.inv instead.
Upvotes: 8
Reputation:
From the documentation:
Parameters:
x : array_like.
Only integer and boolean types are handled."
Your original array is floating point type (the return value of sigmoid()
); setting values in it to 0 and 1 won't change the type. You need to use astype(np.int)
:
neg_correct = float(np.sum(np.invert(makePrediction(theta,neg).astype(np.int))))
should do it (untested).
float()
cast you have also makes more sense. Though I would just remove the cast, and rely on Python doing the right thing. from __future__ import division
to let Python do the right thing (it won't hurt if you do it in Python 3; it just doesn't do anything). With that (or in Python 3 anyway), you can remove numerous other float()
casts you have elsewhere in your code, improving readability.
Upvotes: 5