Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

python - How does numpy argmax work?

So I know that the numpy argmax retrieves the maximum value along an axis. Thus,

x = np.array([[12,11,10,9],[16,15,14,13],[20,19,18,17]])
print(x)
print(x.sum(axis=1))
print(x.sum(axis=0))

would output,

[[12 11 10  9]
 [16 15 14 13]
 [20 19 18 17]]


[42 58 74]

[48 45 42 39]

This makes sense as the sum along axis 1 (row) is [42 58 74] and axis 0 (column) is [48 45 42 39]. However, i am confused of how argmax work. From my understanding, argmax is supposed to return the max number along the axis. Below is my code and output.

Code: print(np.argmax(x,axis=1)). Output: [0 0 0]

Code: print(np.argmax(x,axis=0)). Output: [2 2 2 2]

Where does 0 and 2 come from? I've deliberately used a set of more complex integer values (9..20) so as to distinguish between the 0 and 2 and the integer values inside the array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

np.argmax(x,axis=1) returns the index of maximum of in every row.

axis=1 means "along axis 1", i.e, row.

[[12 11 10  9]    <-- max at index 0
 [16 15 14 13]    <-- max at index 0
 [20 19 18 17]]   <-- max at index 0

Thus its output is [0 0 0].

It's similar for np.argmax(x,axis=0), but now it returns the index of maximum of in every column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...