DrRonne
DrRonne

Reputation: 103

How would I change the content of an array using another array?

First off, I know there is a pretty easy way doing this using a for-loop, but this is a calculation that needs to be done a lot of times every second, and for-loops in python are just way too inefficient to do this. So I am looking for something in numpy that could fix this.

I have a 2D array(arr1) with values between 0 and 256 and a 1D array(arr2) that has the size 256 and contains information of how frequently a number in that 2D array is present. So for exemple if arr1[0, 0] = 1 and arr1[0, 1] = 1 and those are the only ones that have value 1, then arr2[1] = 2, because the value 1 exists twice in arr1.

What I want to do now, is mapping that frequency number onto arr1, so as in the previous exemple arr1[0,0] and arr1[0,1] would both become 2, because that's the frequency of their previous value.

In short this would look something like this, but obviously this can't just work outside a for-loop:

arr1[i,j] = arr2[arr1[i,j]]

Is there an easy, fast and efficient way to do this using numpy?

Upvotes: 1

Views: 178

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

Given arr2 is an 1-D array, you can write this as:

arr1 = arr2[arr1]

Here we thus do an elementwise "mapping" where the new arr1[i, j] is the value that corresponds with the value at the index in arr2 stored for the old value of arr1[i, j].

For example:

>>> a
array([[ 3, 15,  3, 15,  5],
       [ 8, 17, 14, 10,  1],
       [ 3,  8,  9,  0,  1],
       [10,  3, 10,  9,  1]])
>>> b
array([17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,
        0])
>>> b[a]
array([[14,  2, 14,  2, 12],
       [ 9,  0,  3,  7, 16],
       [14,  9,  8, 17, 16],
       [ 7, 14,  7,  8, 16]])

Here for index i, b[i] = 17 - i, we see that this mapping is performed when we calculate b[a].

In case arr1 contains a value that is not a valid index, this will however result - logically - in an IndexError.

Upvotes: 1

Related Questions