用多维数组中的条件替换值 python

replace values with condition in a multidimensional array python

我有一个多维数据:data(K,i,j)

我试过这个珠饰:

for i in range ():
   for j in range ():
      for k in range() :
          if data[k,i,j]<0.01:
             data[k,i,j]='nan'

但是要花很多时间

有最优解吗???

谢谢

你可以使用applmap,你可以试试这个代码。

data=data.applymap(lambda x:'nan' if x<0.01 else x)

使用 NumPy 时不需要循环:

data[data < 0.01] = np.nan
import numpy as np

data = np.array(data)
data[data<0.01] = "nan"

NumPy模块通常在执行时间的情况下更有用。让我们在 jupyter notebook 中举个例子:

import numpy as np
matrix = np.random.rand(400,400,400)

下一个单元格:

%%time # This is a magic command that can be used in a colab notebook in order to see the execution time. If you run it through Python command line, it probably results in a error.
matrix[matrix < 0.5] = np.nan

输出

CPU times: user 625 ms, sys: 0 ns, total: 625 ms
Wall time: 773 ms

如您所见,重新计算 400*400*400 个元素矩阵的元素只用了 773 ms 次。当然,它有效!现在让我们看看 matrix 输出:

array([[[0.58888172, 0.7849002 ,        nan, ...,        nan,
                nan,        nan],
        [       nan, 0.76474748,        nan, ...,        nan,
                nan,        nan],
        [0.60103106, 0.62287764,        nan, ..., 0.63933367,
                nan,        nan],
        ...,
        [       nan, 0.91627877, 0.52837604, ...,        nan,
                nan, 0.7755497 ],
        [0.52519088,        nan, 0.70463779, ..., 0.80754958,
         0.96804943, 0.66126769],
        [0.90220552, 0.97406624, 0.93333412, ...,        nan,
                nan,        nan]],

       [[       nan,        nan, 0.80116461, ..., 0.60552189,
                nan, 0.60547716],
        [       nan,        nan,        nan, ...,        nan,
         0.58920489, 0.64414314],
        [0.59112547, 0.58970573, 0.7817127 , ..., 0.92115189,
         0.67403891, 0.50897163],
        ...,
        [       nan, 0.73571716, 0.89320417, ...,        nan,
         0.64638843, 0.90098355],
        [       nan, 0.60734221, 0.54421301, ..., 0.67758928,
         0.91324091, 0.56821318],
        [       nan,        nan, 0.72628094, ..., 0.79698288,
                nan,        nan]],

       [[0.60481884, 0.87283859, 0.98231053, ...,        nan,
         0.90631025, 0.57994473],
        [       nan,        nan,        nan, ...,        nan,
         0.92896717,        nan],
        [       nan, 0.85387808,        nan, ...,        nan,
         0.82199092, 0.64892107],
        ...,
        [0.51940221,        nan, 0.86065574, ...,        nan,
         0.70869112, 0.60288317],
        [0.92355508,        nan,        nan, ..., 0.61121525,
         0.60234445, 0.88535996],
        [0.94738293,        nan, 0.7134501 , ..., 0.56876322,
                nan,        nan]],

       ...,

       [[       nan,        nan, 0.54247768, ...,        nan,
                nan, 0.94484817],
        [       nan,        nan, 0.6923426 , ...,        nan,
                nan, 0.72161344],
        [0.64403315,        nan,        nan, ...,        nan,
         0.87946106, 0.92479187],
        ...,
        [       nan, 0.62671375, 0.59782275, ...,        nan,
                nan,        nan],
        [       nan, 0.98113013,        nan, ...,        nan,
                nan,        nan],
        [0.64293314,        nan, 0.86640945, ..., 0.57533483,
                nan, 0.74854156]],

       [[0.97657601,        nan, 0.92713931, ..., 0.63876501,
         0.50303145, 0.55530041],
        [0.85351375,        nan, 0.65790311, ..., 0.9921048 ,
         0.67602624, 0.99773446],
        [       nan, 0.84887916, 0.7843213 , ...,        nan,
         0.91993127,        nan],
        ...,
        [0.53349779, 0.80875952,        nan, ...,        nan,
         0.69806386, 0.94379129],
        [0.91561332,        nan,        nan, ...,        nan,
         0.86274521,        nan],
        [       nan,        nan,        nan, ..., 0.71484682,
         0.65998919,        nan]],

       [[       nan, 0.84276618,        nan, ...,        nan,
                nan, 0.59601719],
        [0.78492731,        nan,        nan, ...,        nan,
                nan, 0.86452704],
        [       nan, 0.99587101,        nan, ..., 0.7774153 ,
         0.51868572, 0.54691035],
        ...,
        [0.77281982,        nan, 0.87166967, ..., 0.83113015,
         0.85596892,        nan],
        [       nan,        nan, 0.72000176, ...,        nan,
                nan, 0.74863166],
        [0.52007484,        nan, 0.58487587, ..., 0.55538366,
         0.68744613, 0.52256696]]])

长存短,您只需要将 data 的格式从列表(如果是)更改为 numpy 数组:data = np.array(data).