C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Numpy: how to use argmax results to get the actual max?

By : User01
Date : November 22 2020, 03:01 PM
will be helpful for those in need You can do this with advanced indexing and numpy broadcasting:
code :
``````m = np.argmax(a, axis=1)
a[np.arange(a.shape[0])[:,None], m, np.arange(a.shape[2])]

#array([[7, 6],
#       [5, 4]])
``````
``````m = np.argmax(a, axis=1)
``````
``````ind1, ind2, ind3 = np.arange(a.shape[0])[:,None], m, np.arange(a.shape[2])
​
``````
``````for x in np.broadcast_arrays(ind1, ind2, ind3):
print(x, '\n')

#[[0 0]
# [1 1]]

#[[0 1]
# [1 0]]

#[[0 1]
# [0 1]]
``````

Share :

## Extracting numpy array slice from numpy.argmax results

By : Branimir Bence
Date : March 29 2020, 07:55 AM
will help you I have a (3,3) numpy array and would like to figure out the sign of the element whose absolute value is maximum: , Try this:
code :
``````# You might need to do this to get X as an ndarray (for example if X is a list)
X = numpy.asarray(X)

# Then you can simply do
X[s, [0, 1, 2]]

# Or more generally
X_argmax = X[s, numpy.arange(X.shape[1])]
``````

## Numpy argmax. How to compute both max and argmax?

By : user2680755
Date : March 29 2020, 07:55 AM
I hope this helps . Is there a way to get max and argmax by one stroke ? , Maybe something like this is faster...
code :
``````index=np.argmax(a)
max=a[index]
``````

## Numpy index of the maximum with reduction - numpy.argmax.reduceat

By : Utkarsh Shukla
Date : March 29 2020, 07:55 AM
like below fixes the issue Borrowing the idea from this post.
Steps involved :
code :
``````def numpy_argmax_reduceat(a, b):
n = a.max()+1  # limit-offset
grp_count = np.append(b[1:] - b[:-1], a.size - b[-1])
shift = n*np.repeat(np.arange(grp_count.size), grp_count)
sortidx = (a+shift).argsort()
grp_shifted_argmax = np.append(b[1:],a.size)-1
return sortidx[grp_shifted_argmax] - b
``````
``````def numpy_argmax_reduceat_v2(a, b):
n = a.max()+1  # limit-offset
id_arr = np.zeros(a.size,dtype=int)
id_arr[b[1:]] = 1
shift = n*id_arr.cumsum()
sortidx = (a+shift).argsort()
grp_shifted_argmax = np.append(b[1:],a.size)-1
return sortidx[grp_shifted_argmax] - b
``````

## TensorFlow Tensor handled differently in numpy argmax vs keras argmax

By : Intelipromo Bussines
Date : March 29 2020, 07:55 AM
help you fix your problem You need to execute/run code (say under a TF session) to have tensors evaluated. Until then, the shapes of tensors are not evaluated.
TF docs say:

## Difference between numpy.argmax(array) and array.argmax() in Python

By : user3653701
Date : March 29 2020, 07:55 AM
To fix this issue No difference, array.argmax is just the method implemented on the array object, whereas numpy.argmax is the method implemented as a standalone function that can be passed any array-like object, e.g. a Python list.
code :
``````>>> import numpy as np
>>> a = np.array([1,2,3])
>>> a.argmax()
2

>>> np.argmax([1,2,3])
2
``````