Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy ufunc替换二维映射以提高效率_Python_Numpy - Fatal编程技术网

Python 使用numpy ufunc替换二维映射以提高效率

Python 使用numpy ufunc替换二维映射以提高效率,python,numpy,Python,Numpy,我有一个映射数组,它将一个值作为索引,并将其映射到以前计算的值 例如: 映射[255]=7 我想执行的任务如下: width, height = ni.shape for x in range(width): for y in range(height): NI_mapped[x][y] = mapping[ni[x][y]] RD_mapped[x][y] = mapping[rd[x][y]] 为了提供上下文,ni和rd是作为具有整数值的Ndarra

我有一个映射数组,它将一个值作为索引,并将其映射到以前计算的值

例如: 映射[255]=7

我想执行的任务如下:

width, height = ni.shape
for x in range(width):
    for y in range(height):
        NI_mapped[x][y] = mapping[ni[x][y]]
        RD_mapped[x][y] = mapping[rd[x][y]]
为了提供上下文,ni和rd是作为具有整数值的Ndarray加载的图像

我想使用numpy ufuncs或其他方法来加速此操作,以避免出现两个for循环


我知道如果我要使用函数映射是从中派生的,我可以只做
NI_mapped=NI.vectorize(myfunc)
,但是在这种情况下,从中派生映射的
myfunc
是昂贵的,所以这是不实际的。

听起来像我。你能提供一些示例数据来说明吗?这些整数值在图像中的x、y位置。图像是否可以直接转换为ndarray?很抱歉不清楚,在这种情况下,图像(ni和rd)已经是包含整数值的ndarray。上面的代码就是我想要的功能,我只是好奇使用ufuncs是否可以提高性能。