Python 提高关联数组和字典的速度
我有3个数组Python 提高关联数组和字典的速度,python,numpy,dictionary,Python,Numpy,Dictionary,我有3个数组 a = np.array([[1], [4], [5], [11], [7]]) b = np.array([[14], [3], [2], [10], [12]]) c = np.array([[6], [13], [15], [8], [9]]) 我想将它们合并并按每行升序排序,以获得: [[ 1 6 14] [ 3 4 13] [ 2 5 15] [ 8 10 11] [ 7 9 12]] 并查看从哪个初始数组(a、b、c)中选取每个值。因此,我使用了以下代
a = np.array([[1], [4], [5], [11], [7]])
b = np.array([[14], [3], [2], [10], [12]])
c = np.array([[6], [13], [15], [8], [9]])
我想将它们合并并按每行升序排序,以获得:
[[ 1 6 14]
[ 3 4 13]
[ 2 5 15]
[ 8 10 11]
[ 7 9 12]]
并查看从哪个初始数组(a、b、c)中选取每个值。因此,我使用了以下代码:
combined = np.concatenate([a, b, c], axis=1)
names = np.array(['a','b','c'])
L = names[np.argsort(combined)]
它给了我这个结果:
[['a' 'c' 'b']
['b' 'a' 'c']
['b' 'a' 'c']
['c' 'b' 'a']
['a' 'c' 'b']]
我还有一本字典:
Test = np.array(range(101,116)).reshape((5,3), order = 'F')
或
现在,我希望通过以下方式将Dic与L联系起来:
new = []
for i in range(0,3):
for j in range(0,5):
if L[j,i]=='a':
H = Dic['a'][j]
elif L[j,i]=='b':
H = Dic['b'][j]
elif L[j,i]=='c':
H = Dic['c'][j]
new = np.append(new, H)
final = new.reshape((5,3), order = 'F')
给我以下的最终结果:
[[101. 111. 106.]
[107. 102. 112.]
[108. 103. 113.]
[114. 109. 104.]
[105. 115. 110.]]
然而,我的真实数据集非常大,这个过程需要几个小时。我正在寻找一种更好的方法来加速我的代码
换句话说,我正在根据第一个数组对第二个数组进行排序
first array:[['a' 'c' 'b']
['b' 'a' 'c']
['b' 'a' 'c']
['c' 'b' 'a']
['a' 'c' 'b']]
second array: [[101 106 111]
[102 107 112]
[103 108 113]
[104 109 114]
[105 110 115]]
第1列、第2列和第3列对应于“a”、“b”和“c”只需在此处使用整数而不是字母对
名称进行编码(这也将是argsort的结果)
然后使用沿\u轴取\u
:
np.take_along_axis(Test, L, 1)
字典上的循环总是很慢。你应该尽量避免使用字典,而是使用numpy广播。例如:
将numpy导入为np
a=np.array([[1]、[4]、[5]、[11]、[7])
b=np.数组([[14]、[3]、[2]、[10]、[12]])
c=np.数组([[6]、[13]、[15]、[8]、[9]])
组合=np。连接([a,b,c],轴=1)
i=np.argsort(组合)
Test=np.数组(范围(101116)).重塑((5,3),顺序='F')
打印(测试[np.arange(5)[:,无],i])
#数组([[101111106],
# [107, 102, 112],
# [108, 103, 113],
# [114, 109, 104],
# [105, 115, 110]])
names = np.arange(3)
np.take_along_axis(Test, L, 1)
array([[101, 111, 106],
[107, 102, 112],
[108, 103, 113],
[114, 109, 104],
[105, 115, 110]])