Home > Enterprise >  Replace values in Python numpy array based on value from dictionary
Replace values in Python numpy array based on value from dictionary

Time:03-03

I have the following array that I want to replace values in based on what it corresponds with in a dictionary.

Array:

[[ 0. -1.  1.  1.]
 [ 0.  1. -2. -3.]
 [-1.  1.  1. -5.]
 [-3. -1. -1.  2.]
 [-5.  2. -4. -2.]
 [-1. -3. -1.  2.]
 [ 0.  1. -3.  1.]
 [-2. -3.  0. -2.]
 [-2. -2.  1. -6.]
 [-0. -2.  2. -0.]]

Dictionary:

dict = {-13: 13.0,
      -12: 9.375,
      -11: 9.4,
      -10: 8.6,
      -9: 8.3,
      -8: 7.8,
      -7: 7.1,
      -6: 6.4,
      -5: 5.8,
      -4: 5.2,
      -3: 4.6,
      -2: 4.0,
      -1: 3.6,
      0: 3.2,
      1: 2.8,
      2: 2.5,
      3: 2.2,
      4: 2.0,
      5: 1.8,
      6: 1.6}

As an example, any 0 in the array would be replaced with 3.2, any -1 in the array would be replaced with a 3.6, so on and so forth. The original array is 120x10000x4 so any speed optimization would be ideal.

Thanks in advance for any help!

CodePudding user response:

I think this answers your question. You can check out this for more info.

import numpy as np
from numpy import copy

a = np.array([[ 0., -1.,  1.,  1.],[ 0.,  1., -2., -3.],[-1.,  1.,  1., -5.],[-3., -1., -1.,  2.],[-5.,  2., -4., -2.],[-1., -3., -1.,  2.],[ 0.,  1., -3.,  1.],[-2., -3.,  0., -2.],[-2., -2.,  1., -6.],[-0., -2.,  2., -0.]]) # You need to save this as `np.array`. 
d = {-13: 13.0,-12: 9.375,-11: 9.4,-10: 8.6,-9: 8.3,-8: 7.8,-7: 7.1,-6: 6.4,-5: 5.8,-4: 5.2,-3: 4.6,-2: 4.0,-1: 3.6,0: 3.2,1: 2.8,2: 2.5,3: 2.2,4: 2.0,5: 1.8,6: 1.6}

new_a = copy(a) # This will create a copy of the `a` array. So, you can apply operations on it and not on original data.
for key, value in d.items(): # Taking key and values from dictionary.
    new_a[a==key] = value # Matching the items where the item in array is same as in the dictionary. Setting it's value to the value of dictionary
print(new_a)

Output:

[[3.2 3.6 2.8 2.8]
 [3.2 2.8 4.  4.6]
 [3.6 2.8 2.8 5.8]
 [4.6 3.6 3.6 2.5]
 [5.8 2.5 5.2 4. ]
 [3.6 4.6 3.6 2.5]
 [3.2 2.8 4.6 2.8]
 [4.  4.6 3.2 4. ]
 [4.  4.  2.8 6.4]
 [3.2 4.  2.5 3.2]]

CodePudding user response:

Here is code that does what you've asked:

        import numpy as np
        a = [[ 0., -1.,  1.,  1.],
             [ 0.,  1., -2., -3.],
             [-1.,  1.,  1., -5.],
             [-3., -1., -1.,  2.],
             [-5.,  2., -4., -2.],
             [-1., -3., -1.,  2.],
             [ 0.,  1., -3.,  1.],
             [-2., -3.,  0., -2.],
             [-2., -2.,  1., -6.],
             [-0., -2.,  2., -0.]]
        d = {-13: 13.0,
              -12: 9.375,
              -11: 9.4,
              -10: 8.6,
              -9: 8.3,
              -8: 7.8,
              -7: 7.1,
              -6: 6.4,
              -5: 5.8,
              -4: 5.2,
              -3: 4.6,
              -2: 4.0,
              -1: 3.6,
              0: 3.2,
              1: 2.8,
              2: 2.5,
              3: 2.2,
              4: 2.0,
              5: 1.8,
              6: 1.6}
        x = np.array(a)
        y = np.copy(x)
        for k, v in d.items():
            x[y == k] = v
        print(x)

I have replaced dict from the question with d to avoid using the name of the dict built-in datatype as a variable name, which can cause problems elsewhere in the same module.

Here is sample output:

[[3.2 3.6 2.8 2.8]
 [3.2 2.8 4.  4.6]
 [3.6 2.8 2.8 5.8]
 [4.6 3.6 3.6 2.5]
 [5.8 2.5 5.2 4. ]
 [3.6 4.6 3.6 2.5]
 [3.2 2.8 4.6 2.8]
 [4.  4.6 3.2 4. ]
 [4.  4.  2.8 6.4]
 [3.2 4.  2.5 3.2]]
  • Related