根据键翻译numpy数组中的每个元素 [英] Translate every element in numpy array according to key
本文介绍了根据键翻译numpy数组中的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据给定的键翻译 numpy.array
的每个元素:
例如:
a = np.array([[1,2,3],[3,2,4]])my_dict = {1:23, 2:34, 3:36, 4:45}
我想得到:
array([[ 23., 34., 36.],[ 36., 34., 45.]])
我可以看到如何用循环来做到这一点:
def loop_translate(a, my_dict):new_a = np.empty(a.shape)对于 i,row in enumerate(a):new_a[i,:] = map(my_dict.get, row)返回 new_a
有没有更有效和/或纯粹的麻木方式?
我计时了,DSM 提出的 np.vectorize
方法对于更大的数组要快得多:
在 [13]: def loop_translate(a, my_dict):....: new_a = np.empty(a.shape)....: 对于 i,row in enumerate(a):....: new_a[i,:] = map(my_dict.get, row)....: 返回 new_a....:在 [14]: def vec_translate(a, my_dict):....:返回 np.vectorize(my_dict.__getitem__)(a)....:在 [15] 中:a = np.random.randint(1,5, (4,5))在 [16] 中:一个出[16]:数组([[2, 4, 3, 1, 1],[2, 4, 3, 2, 4],[4, 2, 1, 3, 1],[2, 4, 3, 4, 1]])在 [17]: %timeit loop_translate(a, my_dict)10000 个循环,最好的 3 个:每个循环 77.9 us在 [18]: %timeit vec_translate(a, my_dict)10000 个循环,最好的 3 个:每个循环 70.5 us在 [19] 中:a = np.random.randint(1, 5, (500,500))在 [20]: %timeit loop_translate(a, my_dict)1 个循环,最好的 3 个:每个循环 298 毫秒在 [21]: %timeit vec_translate(a, my_dict)10 个循环,最好的 3 个:每个循环 37.6 毫秒在 [22]: %timeit loop_translate(a, my_dict)
解决方案
我不知道效率,但你可以在 .get
上使用 np.vectorize
字典的方法:
I am trying to translate every element of a numpy.array
according to a given key:
For example:
a = np.array([[1,2,3],
[3,2,4]])
my_dict = {1:23, 2:34, 3:36, 4:45}
I want to get:
array([[ 23., 34., 36.],
[ 36., 34., 45.]])
I can see how to do it with a loop:
def loop_translate(a, my_dict):
new_a = np.empty(a.shape)
for i,row in enumerate(a):
new_a[i,:] = map(my_dict.get, row)
return new_a
Is there a more efficient and/or pure numpy way?
Edit:
I timed it, and np.vectorize
method proposed by DSM is considerably faster for larger arrays:
In [13]: def loop_translate(a, my_dict):
....: new_a = np.empty(a.shape)
....: for i,row in enumerate(a):
....: new_a[i,:] = map(my_dict.get, row)
....: return new_a
....:
In [14]: def vec_translate(a, my_dict):
....: return np.vectorize(my_dict.__getitem__)(a)
....:
In [15]: a = np.random.randint(1,5, (4,5))
In [16]: a
Out[16]:
array([[2, 4, 3, 1, 1],
[2, 4, 3, 2, 4],
[4, 2, 1, 3, 1],
[2, 4, 3, 4, 1]])
In [17]: %timeit loop_translate(a, my_dict)
10000 loops, best of 3: 77.9 us per loop
In [18]: %timeit vec_translate(a, my_dict)
10000 loops, best of 3: 70.5 us per loop
In [19]: a = np.random.randint(1, 5, (500,500))
In [20]: %timeit loop_translate(a, my_dict)
1 loops, best of 3: 298 ms per loop
In [21]: %timeit vec_translate(a, my_dict)
10 loops, best of 3: 37.6 ms per loop
In [22]: %timeit loop_translate(a, my_dict)
解决方案
I don't know about efficient, but you could use np.vectorize
on the .get
method of dictionaries:
>>> a = np.array([[1,2,3],
[3,2,4]])
>>> my_dict = {1:23, 2:34, 3:36, 4:45}
>>> np.vectorize(my_dict.get)(a)
array([[23, 34, 36],
[36, 34, 45]])
这篇关于根据键翻译numpy数组中的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文