在numpy数组(元组列表)中,由于多次扩展(),处理速度较慢.我想把那部分做得更快



在这段代码中,有一个numpy数组可以通过在类似于"res"的for循环中组合元组数组来形成。(变量名称和内容根据实际代码简化。(

如果仔细观察一下,将执行长度为arr_2的for循环,并执行数组extends((。结果表明,当arr_2变长时,处理速度变得非常快。

难道不可能通过创建好数组来实现高速处理吗?

# -*- coding: utf-8 -*-
import numpy as np
arr_1 = np.array([[0, 0, 1], [0, 0.5, -1], [-1, 0, -1], [0, -0.5, -1], [1, 0, -1]])
arr_2 = np.array([[0, 1, 2], [0, 1, 2]])
all_arr = []
for p in arr_2:
all_arr = [
(arr_1[0], p), (arr_1[1], p), (arr_1[2], p), 
(arr_1[0], p), (arr_1[1], p), (arr_1[4], p),
(arr_1[0], p), (arr_1[2], p), (arr_1[3], p), 
(arr_1[0], p), (arr_1[3], p), (arr_1[4], p),
(arr_1[1], p), (arr_1[2], p), (arr_1[4], p), 
(arr_1[2], p), (arr_1[3], p), (arr_1[4], p)]
all_arr.extend(all_arr)

vtype = [('type_a', np.float32, 3), ('type_b', np.float32, 3)]
res = np.array(all_arr, dtype=vtype)
print(res)

我不明白你为什么对arr_1使用这个索引,所以我只是复制了它

import numpy as np
arr_1 = np.array([[0, 0, 1], [0, 0.5, -1], [-1, 0, -1], [0, -0.5, -1], [1, 0, -1]])
arr_2 = np.array([[0, 1, 2], [0, 1, 2]])
weird_idx = np.array([0,1,2,0,1,4,0,2,3,0,3,4,1,2,4,2,3,4])
weird_arr1 = arr_1[weird_idx]
all_arr = [(wiered_arr1[i],arr_2[j]) for j in range(len(arr_2)) for i in range(len(wiered_arr1)) ]
vtype = [('type_a', np.float32, 3), ('type_b', np.float32, 3)]
res = np.array(all_arr, dtype=vtype)

你也可以重复阵列

arr1_rep = np.tile(weird_arr1.T,2).T 
arr2_rep = np.repeat(arr_2,weird_arr1.shape[0],0)
res = np.empty(arr1_rep.shape[0],dtype=vtype)
res['type_a']=arr1_rep
res['type_b']=arr2_rep

通常对于结构化数组,按字段分配比元组列表方法更快:

In [388]: idx = [0,1,2,0,1,4,0,2,3,0,3,4,1,2,4,2,3,4] 
In [400]: res1 = np.zeros(36, dtype=vtype)                                                     
In [401]: res1['type_a'][:18] = arr_1[idx]                                                     
In [402]: res1['type_a'][18:] = arr_1[idx]                                                     
In [403]: res1['type_b'][:18] = arr_2[0]                                                       
In [404]: res1['type_b'][18:] = arr_2[1]                                                       
In [405]: np.allclose(res['type_a'], res1['type_a'])                                           
Out[405]: True
In [406]: np.allclose(res['type_b'], res1['type_b'])                                           
Out[406]: True

相关内容

最新更新