使用字符串含义列表从int'flags'的ndarray创建字符串ndarray

时间:2013-07-16 17:03:06

标签: python arrays list numpy mapping

我正试图从ndarray整数'标志'开始:

array([[1, 3, 2],
       [2, 0, 3],
       [3, 2, 0],
       [2, 0, 1]])

ndarray个字符串:

array([['Banana', 'Celery', 'Carrot'],
       ['Carrot', 'Apple', 'Celery'],
       ['Celery', 'Carrot', 'Apple'],
       ['Carrot', 'Apple', 'Banana']],
      dtype='|S6')

使用字符串列表作为'flags'到'含义'的映射:

meanings = ['Apple', 'Banana', 'Carrot', 'Celery']

我想出了以下内容:

>>> import numpy as np
>>> meanings = ['Apple', 'Banana', 'Carrot', 'Celery']
>>> flags = np.array([[1,3,2],[2,0,3],[3,2,0],[2,0,1]])
>>> flags
array([[1, 3, 2],
       [2, 0, 3],
       [3, 2, 0],
       [2, 0, 1]])
>>> mapped = np.array([meanings[f] for f in flags.flatten()]).reshape(flags.shape)
>>> mapped
array([['Banana', 'Celery', 'Carrot'],
       ['Carrot', 'Apple', 'Celery'],
       ['Celery', 'Carrot', 'Apple'],
       ['Carrot', 'Apple', 'Banana']],
      dtype='|S6')

这很有效,但在处理大型flatten时,我关注相关行的效率(list comp,reshapendarrays):

np.array([meanings[f] for f in flags.flatten()]).reshape(flags.shape)

是否有更好/更有效的方式来执行这样的映射?

2 个答案:

答案 0 :(得分:3)

花式索引是 numpythonic 的做法:

mapped = meanings[flags]

或通常更快的等价物:

mapped = np.take(meanings, flags)

答案 1 :(得分:2)

我认为np.vectorize是要走的路,它也非常清晰易懂。我还没有对以下内容进行过测试,但它应该有效。

vfunc = np.vectorize(lambda x : meanings[x])

mapped = vfunc(flags)