logo
down
shadow

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


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 : facebook icon twitter icon
Extracting numpy array slice from numpy.argmax results

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?

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

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

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

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
Related Posts Related Posts :
  • How to use an API that requires user's entry (Sentiment Analysis)
  • Django first app
  • Why is this regex code not working
  • Beautifulsoup - findAll not finding string when link is also in container
  • Python: any() to check if attribute in List of Objects matches a list
  • How do I "enrich" every record in a Pandas dataframe with an hour column?
  • Failing to open an Excel file with Python
  • Python function to modify string
  • Pandas DataFrame seems not to have "factorize" method
  • Row column operations in CSV
  • How to decrypt RSA encrypted file (via PHP and OpenSSL) with pyopenssl?
  • How can we use pandas to generate min, max, mean, median, ...as new columns for the dataframe?
  • Cython: creating an array throws "not allowed in a constant expression"
  • Different thing is shown in html
  • sublimetext3 event for program exit
  • Join contigous tokens if the token includes "@" char
  • transparent background in gif using Python Imageio
  • Enable autologin into flask app using active directory
  • Make a NxN array of 1x3 arrays of random numbers (python)
  • django how to use Max and Count on the same field in back-to-back annotations
  • Using the OR operator seems to only take the first of two conditions when used with np.where filter
  • Elegant Dataframe Operations in Pandas
  • Change metadata of pdf file with pypdf2
  • How can I animate a set of points with matplotlib?
  • error: (-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function arcLength
  • OpenStack KeyStone SSL Exception When Creating an Instance of KeyStone
  • pyspark: The system cannot find the path specified
  • How can I set path to load data from CSV file into PostgreSQL database in Docker container?
  • Summation in python dictionary
  • DRF 3.7.0 removed handling None in fields and broke my foreign key source fields. Is there a way around it?
  • Error with Padlen in signal.filtfilt in Python
  • Abstract matrix multiplication with variables
  • Reading binary data on bit level
  • How to replace multiple instances of a sub strings in a string using a for loop (in a function)?
  • py2neo cypher create several relations to central node in for loop
  • [python-3]TypeError: must be str, not int
  • How to exit/terminate a job earlier and handle the raised exception in apscheduler?
  • python, print intermediate values while loop
  • python to loop over yaml config
  • D3.js is not recognized by PyCharm
  • Access the regularization paths obtained from ElasticNetCV in sklearn
  • Pattern table to Pandas DataFrame
  • Get the earliest date from a column (Python Pandas) after csv.reader
  • Get SystemError: Parent module '' not loaded, cannot perform relative import when trying to import numpy in a Cython Ext
  • Bash or Python : Append and prepend a string recursively in all .tex files
  • Changing a certain index of boolean list of lists change others, too
  • complex dataframe filtering request on the last occurence of a value in Panda/Python [EDIT]
  • How to repeatedly get the contents of a Text widget every loop with tkinter?
  • How to call the tornado.queues message externally
  • How can I use regex in python so that characters not included are disallowed?
  • Discarding randmly scattered empty spaces in pandas data frame
  • Get sums grouped by date by same column filtered by 2 conditions
  • Element disappears when I add an {% include %} tag inside my for loop
  • Django Rest Framework with either a slug or a pk lookup field for the DetailAPIView
  • Flask doesn't stream on Lambda
  • Generate all permutations of fixed length where the elements come from two different sets
  • Making function for calculating distance
  • How to handle multiprocessing based on the limit of CPU's
  • Django - static files is not working
  • Remove x axis and y axis black lines with matplotlib
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org