压缩numpy数组的有效方法(python)

我正在寻找一种压缩numpy数组的有效方法.

我有一个数组如:dtype = [(name,(np.str_,8),(job,(np.str_,8),(income,np.uint32)](我最喜欢的例子).

如果我正在做这样的事情:my_array.compress(my_array [‘income’]> 10000)我得到一个只收入>的新数组10000,它很快.

但是,如果我想过滤列表中的作业:它不起作用!

my__array.compress(m_y_array['job'] in ['this', 'that'])

错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

所以我必须做这样的事情:

np.array([x for x in my_array if x['job'] in ['this', 'that'])

这既丑陋又低效!

你有想法让它变得高效吗?

最佳答案 它不如你想要的那么好,但我认为你可以这样做:

mask = my_array['job'] == 'this'
for condition in ['that', 'other']:
  mask = numpy.logical_or(mask,my_array['job'] == condition)
selected_array = my_array[mask]
点赞