user9155773
user9155773

Reputation:

Substitute item in numpy array with list

I want to replace an item in a numpy array composed of 0 and 1. I want to replace 1 with the list [0,1] and the 0 with the list [1,0].

I found the function numpy.where(), but it doesn't work with lists. Is there a function or something similar to do the equivalent for numpy.where(vector==1,[0,1], [1,0]) ?

Upvotes: 0

Views: 177

Answers (5)

FatihAkici
FatihAkici

Reputation: 5109

hpaulj's answer would be the ideal way of doing this. An alternative is to reshape your original numpy array, and apply your standard np.where() operation.

import numpy as np

x = np.array([0,1,0,0,1,1,0])
x = np.reshape(x,(len(x),1))
# Equivalently, if you want to be explicit:
# x = np.array([[e] for e in x])
print np.where(x==1,[1,0],[0,1])
# Result:
array([[0, 1],
       [1, 0],
       [0, 1],
       [0, 1],
       [1, 0],
       [1, 0],
       [0, 1]])

Upvotes: 0

Paul Panzer
Paul Panzer

Reputation: 53089

You can actually use where for that if you wish:

>>> import numpy as np
>>> vector = np.random.randint(0, 2, (8, 8))
>>> vector
array([[1, 0, 0, 0, 0, 0, 1, 1],
       [1, 1, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 1, 1, 1, 0, 0],
       [1, 0, 1, 1, 0, 0, 0, 0],
       [0, 1, 0, 1, 1, 1, 1, 1],
       [1, 0, 1, 1, 0, 0, 0, 0],
       [0, 1, 1, 0, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1, 0, 0]])
>>> np.where(vector[..., None] == 1, [0,1], [1,0])
# btw. if vector has only 0 and 1 entries you can leave out the " == 1 "
array([[[0, 1],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [1, 0],
        [0, 1],
        [0, 1]],

       [[0, 1],
        [0, 1],
        [1, 0],
        [0, 1],
        [1, 0],
        [1, 0],
        [1, 0],

        etc.

Upvotes: 1

hpaulj
hpaulj

Reputation: 231605

Simple indexing should do the job:

In [156]: x = np.array([0,1,0,0,1,1,0])
In [157]: y = np.array([[1,0],[0,1]])
In [158]: y[x]
Out[158]: 
array([[1, 0],
       [0, 1],
       [1, 0],
       [1, 0],
       [0, 1],
       [0, 1],
       [1, 0]])

And just to be sure this is a general solution, not some 'boolean' fluke

In [162]: x = np.array([0,1,0,0,1,2,2])
In [163]: y = np.array([[1,0],[0,1],[1,1]])
In [164]: y[x]
Out[164]: 
array([[1, 0],
       [0, 1],
       [1, 0],
       [1, 0],
       [0, 1],
       [1, 1],
       [1, 1]])

Upvotes: 2

kmario23
kmario23

Reputation: 61445

Since it's not possible to grow the array dynamically in NumPy, you've to convert the array to list, do the substitution as desired, and then convert the result back to NumPy array like:

In [21]: vector = np.array([0, 0, 1, 0, 1])

In [22]: vlist = vector.tolist()

In [23]: vlist
Out[23]: [0, 0, 1, 0, 1]

In [24]: new_list = [[1, 0] if el == 0 else [0, 1] for idx, el in enumerate(vlist)]

In [25]: result = np.array(new_list)

In [26]: result
Out[26]: 
array([[1, 0],
       [1, 0],
       [0, 1],
       [1, 0],
       [0, 1]])

If your 1D array is not that large enough, then list comprehension is not that bad of an approach.

Upvotes: 0

aiven
aiven

Reputation: 4323

Looks like you want to make one-hot vector encoding:

a = np.array([1, 0, 1, 1, 0])  # initial array
b = np.zeros((len(a), 2))  # where 2 is number of classes
b[np.arange(len(a)), a] = 1
print(b)

result:

array([[ 0.,  1.],
       [ 1.,  0.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 1.,  0.]])

Upvotes: 0

Related Questions