numpy argsort,基于另一个排序列
numpys argsort, sorting column based on another one
我想根据第一列对数组 a
进行排序:
import numpy as np
a = np.array([[ 1.50000000e+03, 1.68693009e-01],
[ 5.00000000e+02, 1.26512097e-01],
[ 1.50000000e+03, 1.68338907e-01],
[ 5.00000000e+02, 1.22073944e-01],
[ 5.00000000e+02, 1.31257631e-01],
[ 1.50000000e+03, 1.72864036e-01],
[ 1.50000000e+03, 1.70665459e-01],
[ 5.00000000e+02, 1.32951654e-01],
[ 1.00000000e+03, 1.49342105e-01]])
sort = a.T.argsort(0)
b = a[sort]
print(b)
给我:
[[[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]]
[[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]]]
我的条目在哪里 a[:,0] == 1000
?它消失了。我如何生成一个数组,如 a_sorted[0] = sorted x 和 a_sorted[1] = sorted y?
如果我不在 argsort 之前转置 a
,我会得到一个形状为 (9, 2, 2)
的数组
a.sort(0)
根据第一列对数组进行排序(并保留 1000):
array([[ 5.00000000e+02, 1.22073944e-01],
[ 5.00000000e+02, 1.26512097e-01],
[ 5.00000000e+02, 1.31257631e-01],
[ 5.00000000e+02, 1.32951654e-01],
[ 1.00000000e+03, 1.49342105e-01],
[ 1.50000000e+03, 1.68338907e-01],
[ 1.50000000e+03, 1.68693009e-01],
[ 1.50000000e+03, 1.70665459e-01],
[ 1.50000000e+03, 1.72864036e-01]])
如果不想就地排序,可以使用:
a[a.argsort(0)[:,0]]
我想根据第一列对数组 a
进行排序:
import numpy as np
a = np.array([[ 1.50000000e+03, 1.68693009e-01],
[ 5.00000000e+02, 1.26512097e-01],
[ 1.50000000e+03, 1.68338907e-01],
[ 5.00000000e+02, 1.22073944e-01],
[ 5.00000000e+02, 1.31257631e-01],
[ 1.50000000e+03, 1.72864036e-01],
[ 1.50000000e+03, 1.70665459e-01],
[ 5.00000000e+02, 1.32951654e-01],
[ 1.00000000e+03, 1.49342105e-01]])
sort = a.T.argsort(0)
b = a[sort]
print(b)
给我:
[[[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]
[ 5.00000000e+02 1.26512097e-01]]
[[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]
[ 1.50000000e+03 1.68693009e-01]]]
我的条目在哪里 a[:,0] == 1000
?它消失了。我如何生成一个数组,如 a_sorted[0] = sorted x 和 a_sorted[1] = sorted y?
如果我不在 argsort 之前转置 a
,我会得到一个形状为 (9, 2, 2)
a.sort(0)
根据第一列对数组进行排序(并保留 1000):
array([[ 5.00000000e+02, 1.22073944e-01],
[ 5.00000000e+02, 1.26512097e-01],
[ 5.00000000e+02, 1.31257631e-01],
[ 5.00000000e+02, 1.32951654e-01],
[ 1.00000000e+03, 1.49342105e-01],
[ 1.50000000e+03, 1.68338907e-01],
[ 1.50000000e+03, 1.68693009e-01],
[ 1.50000000e+03, 1.70665459e-01],
[ 1.50000000e+03, 1.72864036e-01]])
如果不想就地排序,可以使用:
a[a.argsort(0)[:,0]]