Issue
Is there a function in Numpy to invert 0 and 1 in a binary array? If
a = np.array([0, 1, 0, 1, 1])
I would like to get:
b = [1, 0, 1, 0, 0]
I use:
b[a==0] = 1
b[a==1] = 0
but maybe it already exist something in Numpy to do this.
Solution
you can simply do:
In[1]:b=1-a
In[2]:b
Out[2]: array([1, 0, 1, 0, 0])
or
In[22]:b=(~a.astype(bool)).astype(int)
Out[22]: array([1, 0, 1, 0, 0])
Answered By - shivsn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.