根据字典中的值替换 Python numpy 数组中的值

Replace values in Python numpy array based on value from dictionary

我有以下数组,我想根据它在字典中对应的内容替换其中的值。

数组:

[[ 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.]]

词典:

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}

例如,数组中的任何 0 将替换为 3.2,数组中的任何 -1 将替换为 3.6,依此类推。原始数组为 120x10000x4,因此任何速度优化都是理想的。

在此先感谢您的帮助!

我想这回答了你的问题。您可以查看 this 了解更多信息。

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)

输出:

[[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]]

这是执行您所要求的代码:

        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)

我已将问题中的 dict 替换为 d 以避免使用 dict built-in 数据类型的名称作为变量名称,这可能会在其他地方引起问题在同一模块中。

这是示例输出:

[[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]]