Tampa
Tampa

Reputation: 78244

Python and numpy - converting multiple values in an array to binary

I have a numpy array that is rather large, about 1mill. The distinct number of numbers is about 8 numbered 1-8.

Lets say I want given the number 2, I would like to recode all 2's to 1 and the rest to 0's.

i.e. 
2==>1
1345678==0

Is there a pythonic way to do this with numpy?


[1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8]=> [0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0]

Thanks

Upvotes: 1

Views: 4110

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 601411

That's the result of a == 2 for a NumPy array a:

>>> a = numpy.random.randint(1, 9, size=20)
>>> a
array([4, 5, 1, 2, 5, 7, 2, 5, 8, 2, 4, 6, 6, 1, 8, 7, 1, 7, 8, 7])
>>> a == 2
array([False, False, False,  True, False, False,  True, False, False,
        True, False, False, False, False, False, False, False, False,
       False, False], dtype=bool)
>>> (a == 2).astype(int)
array([0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

If you want to change a in place, the most efficient way to do so is to use numpy.equal():

>>> numpy.equal(a, 2, out=a)
array([0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Upvotes: 5

DSM
DSM

Reputation: 352979

I'd probably use np.where for this:

>>> import numpy as np
>>> a = np.array([[1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8]])
>>> np.where(a==2, 1, 0)
array([[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]])

Upvotes: 4

Related Questions