2

我有一个像下面这样的数组。如何从该数组中获取 argmax 并将其保存为数据框。

大批

([[  4.33578761e-03,   1.11002535e-01,   1.18002132e-01,
      2.23158062e-01,   3.57010156e-01,   9.33912754e-01],
   [  3.21856714e-05,   3.46616306e-03,   4.58798464e-03,
      7.65424687e-03,   1.02789648e-01,   9.99610841e-01]], dtype=float32) was passed
4

2 回答 2

2

将 n 视为 numpy 数组

import numpy as np
n = np.array([[  4.33578761e-03,   1.11002535e-01,   1.18002132e-01,
      2.23158062e-01,   3.57010156e-01,   9.33912754e-01],
   [  3.21856714e-05,   3.46616306e-03,   4.58798464e-03,
      7.65424687e-03,   1.02789648e-01,   9.99610841e-01]], dtype=float)

# printing argmax at axis = 0
print(np.argmax(n, axis=0))
#print(n.argmax(axis=0))

a_max = np.argmax(n, axis=0)
np.save(open("your_file_path", "w"), a_max)

并加载

a_max = np.load(open("your_file_path"))

参考

于 2017-12-20T06:20:36.300 回答
1

我相信你需要:

print (arr.argmax(axis=1))
[5 5]

print (arr.argmax(axis=0))
[0 0 0 0 0 1]

先getargmax再使用DataFrame构造函数:

print (pd.DataFrame(arr.argmax(axis=0), columns=['a']))
   a
0  0
1  0
2  0
3  0
4  0
5  1

print (pd.DataFrame(arr.argmax(axis=0)[None, :]))
   0  1  2  3  4  5
0  0  0  0  0  0  1

print (pd.DataFrame(arr.argmax(axis=1), columns=['a']))
   a
0  5
1  5

或创建DataFrame并调用idxmax

print (pd.DataFrame(arr).idxmax().to_frame('a'))
#same as
#print (pd.DataFrame(arr).idxmax(axis=0).to_frame('a'))
   a
0  0
1  0
2  0
3  0
4  0
5  1

print (pd.DataFrame(arr).idxmax().to_frame(0).T)
   0  1  2  3  4  5
0  0  0  0  0  0  1


print (pd.DataFrame(arr).idxmax(axis=1).to_frame('a'))
   a
0  5
1  5
于 2017-12-20T06:04:55.007 回答