奇怪的Numpy分类行为

时间:2019-05-22 06:08:13

标签: python numpy sorting

似乎NumPy argsort在浮标上按字典顺序工作:

npSorted = np.array([[ "a", 12],
  ["B", 8 ],
  ["D", -0.96605562],
  [ "e", -0.28283675],
  ["F",  0.22764316],
  ["T",  0.27752806],
  ["G",  0.36523876],
  ["H",  0.50737573],
  [ "ME",  0.93213482]])

npSorted = npSorted[npSorted[:, 1].argsort()]

for sortedStock in npSorted:
  print(sortedStock[0], ": ", sortedStock[1])

结果是:

e :  -0.28283675
D :  -0.96605562
F :  0.22764316
T :  0.27752806
G :  0.36523876
H :  0.50737573
ME :  0.93213482
a :  12
B :  8

那么我应该如何排序以获得0.5 <8 <12?

谢谢

1 个答案:

答案 0 :(得分:2)

就像@ Tomothy32所说的那样,不要使用异构数据。您可以执行以下操作来修复它。在排序前添加.astype(np.float)

1001 qc101 consented qc102;qc111
1001 qc102 notconsented qc102;qc111
1001 qc111 notconsented qc102;qc111
1221 qc244 notconsented qc244;qc201
1221 qc202 consented qc244;qc201
1221 qc201 notconsented qc244;qc201