我正在寻找一种高效的方法来压缩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'])
这既丑陋又低效!
您有什么想法可以提高它的效率吗?