如何加快Numpy数组的筛选/选择?

时间:2018-11-22 15:56:53

标签: python performance numpy parallel-processing multiprocessing

我大约有4万行,我想在行上测试各种选择组合。通过选择,我的意思是布尔型掩码。面具/滤镜的数量约为250MM。

当前的简化代码:

np_arr = np.random.randint(1, 40000, 40000)
results = np.empty(250000000)
filters = np.random.randint(1, size=(250000000, 40000))
for i in range(250000000):
    row_selection = np_arr[filters[i].astype(np.bool_)] # Select rows based on next filter
    # Performing simple calculations such as sum, prod, count on selected rows and saving to result
    results[i] = row_selection.sum() # Save simple calculation result to results array

我尝试了Numba和Multiprocessing,但是由于大多数处理是在过滤器选择中而不是计算中,所以并没有太大帮助。

解决这个问题的最有效方法是什么?有什么办法可以并行化吗?据我所知,我需要遍历每个过滤器,然后分别计算总和,产品,计数等,因为我不能并行应用过滤器(即使应用过滤器之后的计算非常简单)。

赞赏有关性能改进/加速的任何建议。

2 个答案:

答案 0 :(得分:3)

要在Numba中获得良好的性能,只需避免掩盖,因此阵列复制非常昂贵。您必须自己实现过滤器,但是您提到的过滤器应该不会有任何问题。

并行化也非常容易。

示例

import numpy as np
import numba as nb

max_num = 250000 #250000000
max_num2 = 4000#40000
np_arr = np.random.randint(1, max_num2, max_num2)
filters = np.random.randint(low=0,high=2, size=(max_num, max_num2)).astype(np.bool_)

#Implement your functions like this, avoid masking
#Sum Filter
@nb.njit(fastmath=True)
def sum_filter(filter,arr):
  sum=0.
  for i in range(filter.shape[0]):
    if filter[i]==True:
      sum+=arr[i]
  return sum

#Implement your functions like this, avoid masking
#Prod Filter
@nb.njit(fastmath=True)
def prod_filter(filter,arr):
  prod=1.
  for i in range(filter.shape[0]):
    if filter[i]==True:
      prod*=arr[i]
  return sum

@nb.njit(parallel=True)
def main_func(np_arr,filters):
  results = np.empty(filters.shape[0])
  for i in nb.prange(max_num):
    results[i]=sum_filter(filters[i],np_arr)
    #results[i]=prod_filter(filters[i],np_arr)
  return results

答案 1 :(得分:1)

一种改进的方法是将as_type移出循环。在我的测试中,它将执行时间减少了一半以上。 为了进行比较,请检查以下两个代码:

import numpy as np
import time

max_num = 250000 #250000000
max_num2 = 4000#40000
np_arr = np.random.randint(1, max_num2, max_num2)
results = np.empty(max_num)
filters = np.random.randint(1, size=(max_num, max_num2))
start = time.time()
for i in range(max_num):
    row_selection = np_arr[filters[i].astype(np.bool_)] # Select rows based on next filter
    # Performing simple calculations such as sum, prod, count on selected rows and saving to result
    results[i] = row_selection.sum() # Save simple calculation result to results array

end = time.time()
print(end - start)

需要2.12

同时

import numpy as np
import time

max_num = 250000 #250000000
max_num2 = 4000#40000
np_arr = np.random.randint(1, max_num2, max_num2)
results = np.empty(max_num)
filters = np.random.randint(1, size=(max_num, max_num2)).astype(np.bool_)
start = time.time()
for i in range(max_num):
    row_selection = np_arr[filters[i]] # Select rows based on next filter
    # Performing simple calculations such as sum, prod, count on selected rows and saving to result
    results[i] = row_selection.sum() # Save simple calculation result to results array

end = time.time()
print(end - start)

需要0.940