1

*Question edited/updated to add an example

Hi all! I have this a np.array. Based on the reference values of it, I want to update array b, which is my matrix. The "1st column" of a represents a code and the "2nd column" is my reference value. The matrix is populated with codes and I must replace them. See below the example.

import numpy as np
a = np.asarray([[0, 11], [1, 22], [2, 33]])
b = np.asarray([[0, 14, 12, 2], [1, 1, 7, 0], [0, 0,3,5], [1, 2, 2, 6]])

In other words: I want to replace the 0, 1, 2 values in "b" by 11, 22, 33, respectively.

Which is the best way to do that, considering that my real a array has +- 50 codes and my real b matrices have a shape of (850,850).

Thanks in advance!

2
  • Could you add some example of expected output? Commented Dec 15, 2018 at 23:03
  • @RenardKorzeniowski question updated ! Thanks Commented Dec 16, 2018 at 10:10

2 Answers 2

1

If I understand the question correctly, this example should show what you're asking for?

Assuming a is the matrix as you've listed above, and b is the list you want to write to

import numpy as np
a = np.asarray([[0, 10], [2, 30], [1, 40]])
b = np.zeros(3)
b[a[:, 0]] = a[:, 1]

where the [:, 0] is the index to be changed, and [:, 1] is what to populate it with

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It works when arrays have same dimension, right? In my case the dimension differs so I'm still figuring out how to make it happen. I updated the question with one example. Thank you!
0

If codes are not too long integers, You just have to build the correct lookup table :

lut = np.arange(b.max()+1)
k,v = a.T
lut[k] = v

For :

>>> b
[[ 0 14 12  2]
 [ 1  1  7  0]
 [ 0  0  3  5]
 [ 1  2  2  6]]

>>> lut[b]
[[11 14 12 33]
 [22 22  7 11]
 [11 11  3  5]
 [22 33 33  6]]

undefined codes are mapped to themselves,code=value.

4 Comments

Great! It is working. You've said that the approach can be used for codes that are not too long integers. If they are, why is not a good idea? Is it because processing time or sth? In my case, the code range is 1-10000 but the values have around 12-16 digits
No 10000 << 850², so lut will be little in regard to your data.
One more question! My real data has is populated with a negative value that represents "no data" for my analysis . I've tested your solution with one example, but because of the inverted index my output matrix is not what I was expecting. Is there any way to determine that the negative value remains imutable? My example array: b = np.asarray([[0, 1, 2, 3, -9999], [0, 1, 2, 10000, -9999], [0, 1, 2, 3, -9999], [0, 1, 2, 3, -9999]])
Clearly this -9999 is a bad idea, there is always a day where it will be problematic. work around : lut = np.arange(b.max()+2); b[b==-9999]=b.max()+1; solve your problem;b[b==b.max()+1]=-9999

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.