Kev1n91
Kev1n91

Reputation: 3673

What's the purpose of numpys argmax used with a scalar?

Recently I used the numpy argmax function which gives the index of the maximum value in a numpy array. Due to some circumstances I found out that when used with a scalar it just gives out 0, so like this:

np.argmax(3) # equals 0
np.argmax(1000) #equals 0

which makes sense of course, since there is only one index - but is there an actual application where one needs to find the maximum index of a scalar?

Upvotes: 1

Views: 193

Answers (1)

Stef
Stef

Reputation: 30589

I think this is just for consistency as explained in the documentation on scalars:

Array scalars have the same attributes and methods as ndarrays. This allows one to treat items of an array partly on the same footing as arrays, smoothing out rough edges that result when mixing scalar and array operations.

When you don't specify axis in argmax it returns the index into the flattened array, so even in this case the scalar is internally viewed as a 0D array.

Upvotes: 2

Related Questions