我有一个非常大的numpy数组(包含多达一百万个元素),如下所示:
[ 0 1 6 5 1 2 7 6 2 3 8 7 3 4 9 8 5 6 11 10 6 7 12 11 7
8 13 12 8 9 14 13 10 11 16 15 11 12 17 16 12 13 18 17 13 14 19 18 15 16
21 20 16 17 22 21 17 18 23 22 18 19 24 23]
Run Code Online (Sandbox Code Playgroud)
和一个小的字典映射,用于替换上面数组中的一些元素
{4: 0, 9: 5, 14: 10, 19: 15, 20: 0, 21: 1, 22: …Run Code Online (Sandbox Code Playgroud) 在我生成的数组中,我有一个图像读入numpy中有相当多的像素.
我计算了一个包含256个值的查找表.现在我想做以下事情:
for i in image.rows:
for j in image.cols:
mapped_image[i,j] = lut[image[i,j]]
Run Code Online (Sandbox Code Playgroud)
是的,这基本上就像一个人.
唯一的问题是:我想做到高效并在python中调用该循环将让我等待几秒钟才能完成.
我知道numpy.vectorize(),它只是一个调用相同python代码的便捷函数.
首先,如果在其他地方已经回答了这个问题,我深表歉意。我能找到的只是关于替换给定值的元素的问题,而不是多个值的元素。
我有几千个大型 np.arrays,如下所示:
# generate dummy data
input_array = np.zeros((100,100))
input_array[0:10,0:10] = 1
input_array[20:56, 21:43] = 5
input_array[34:43, 70:89] = 8
Run Code Online (Sandbox Code Playgroud)
在这些数组中,我想根据字典替换值:
mapping = {1:2, 5:3, 8:6}
Run Code Online (Sandbox Code Playgroud)
这时候,我使用了一个简单的循环,结合花哨的索引:
output_array = np.zeros_like(input_array)
for key in mapping:
output_array[input_array==key] = mapping[key]
Run Code Online (Sandbox Code Playgroud)
我的数组的维度为 2000 到 2000,字典有大约 1000 个条目,因此,这些循环需要永远。
是否有一个函数,它只需要一个数组和一个字典(或类似形式)形式的映射,并输出更改后的值?
非常感谢帮助!
我在 Ipython 中测试了各个解决方案,使用
%%timeit -r 10 -n 10
import numpy as np
np.random.seed(123)
sources = range(100)
outs = [a for a in range(100)]
np.random.shuffle(outs)
mapping = {sources[a]:outs[a] …Run Code Online (Sandbox Code Playgroud) 我想将“黑匣子”Python 函数f应用于大数组arr。额外的假设是:
f是“纯的”,例如是确定性的,没有副作用。arr具有少量唯一元素。我可以使用一个装饰器来实现这一点,该装饰器f为每个唯一元素计算arr如下:
import numpy as np
from time import sleep
from functools import wraps
N = 1000
np.random.seed(0)
arr = np.random.randint(0, 10, size=(N, 2))
def vectorize_pure(f):
@wraps(f)
def f_vec(arr):
uniques, ix = np.unique(arr, return_inverse=True)
f_range = np.array([f(x) for x in uniques])
return f_range[ix].reshape(arr.shape)
return f_vec
@np.vectorize
def usual_vectorize(x):
sleep(0.001)
return x
@vectorize_pure
def pure_vectorize(x):
sleep(0.001)
return x
# In [47]: %timeit usual_vectorize(arr)
# 1.33 s …Run Code Online (Sandbox Code Playgroud) 简而言之,我想做的与这个问题类似:Convert RGB image to index image,但我想要获取 n 通道图像,而不是 1 通道索引图像,其中img[h, w]是 one-hot 编码向量。例如,如果输入图像为[[[0, 0, 0], [255, 255, 255]],索引 0 分配给黑色,索引 1 分配给白色,则所需的输出为[[[1, 0], [0, 1]]]。
就像上一个人问的问题一样,我天真地实现了这个,但是代码运行得很慢,我相信使用 numpy 的正确解决方案会明显更快。
另外,正如上一篇文章中所建议的,我可以将每个图像预处理为灰度并对图像进行单热编码,但我想要一个更通用的解决方案。
假设我想将白色分配给 0,红色分配给 1,蓝色分配给 2,黄色分配给 3:
(255, 255, 255): 0
(255, 0, 0): 1
(0, 0, 255): 2
(255, 255, 0): 3
Run Code Online (Sandbox Code Playgroud)
,我有一个由这四种颜色组成的图像,其中图像是一个 3D 数组,其中包含每个像素的 R、G、B 值:
[
[[255, 255, 255], [255, 255, 255], [255, 0, 0], [255, 0, 0]],
[[ 0, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个数组,确定元素的排序:
order = [3, 1, 4, 2]
Run Code Online (Sandbox Code Playgroud)
然后我想要排序另一个更大的数组(仅包含那些元素):
a = np.array([4, 2, 1, 1, 4, 3, 1, 3])
Run Code Online (Sandbox Code Playgroud)
这样首先出现的元素order在结果中排在第一位,等等.
在Python中,我会用一个关键函数来做:
sorted(a, key=order.index)
[3, 3, 1, 1, 1, 4, 4, 2]
Run Code Online (Sandbox Code Playgroud)
如何(有效地)使用numpy这样做?numpy数组是否有类似的"关键功能"概念?
numpy ×6
python ×5
arrays ×2
image ×1
pandas ×1
performance ×1
python-2.x ×1
replace ×1
sorting ×1
unique ×1