Questions tagged [argmax]

arguments of the maxima: given a function of some arguments, argmax stands for the values of the arguments that maximizes the function.

Case 1

Here we consider a function f that maps from a limited set.

Say,

f(x) := {13, 42, 1981, 9, 11, 0},

so f(x = 1) = 13,and f(x = 2) = 42, so on.

It is easy to see here that the value of x that maximizes the function f is 3; that is f(x = 3) = 1981 is the highest values. Thus, argmax of f(x) is x = 3.

In R

f = c(13, 42, 1981, 9, 11, 0)
which.max(f)
# 3

Case 2

Here we consider a function of a continues variable.

Consider

f(x) = x(4 - x)

for this function the argmax is x = 2. That is the highest value of f(x) is 4 which corresponds to x = 2.

In R, we can use the optimize function for this to find the argmax of f.

f <- function(x) {
     x*(4 - x)
}

optimize(f, interval = c(-100, 100), maximum = TRUE)
# $maximum
# [1] 2
#
#$objective
#[1] 4
170 questions
1
vote
1 answer

Numpy: for each element in one dimension, find coordinates of maximum of sub-array

I've seen variations of this question asked a few times but so far haven't seen any answers that get to the heart of this general case. I have an n-dimensional array of shape [a, b, c, ...] . For some dimension x, I want to look at each sub-array…
ttshaw1
  • 59
  • 7
1
vote
1 answer

Is there a faster argmin/argmax implementation in OpenACC?

Is there a faster alternative for computing the argmin in OpenACC, than splitting the work in a minimum-reduction loop and another loop to actually find the index of the minimum? This looks very wasteful: float minVal =…
Dunkelkoon
  • 398
  • 2
  • 10
1
vote
1 answer

Find indices for max values subarrays and applying it on that subarray

I have a file f which holds N (unknown) events. Each event carries an (unknown and different for each event, call it i, j etc) amount of reconstructed tracks. Then, each track has properties like energy E and likelihood lik. So, >>>…
1
vote
1 answer

Why does np.argmax(None) return 0 and not throw an Exception?

According to Numpy's argmax documentation, the first argument we pass to np.argmax must be an array. If the array is an empty list, we get a ValueError Exception: import numpy as np print(np.argmax([])) ValueError: attempt to get argmax of an…
ihavenoidea
  • 629
  • 1
  • 7
  • 26
1
vote
0 answers

How to understand the Python Numpy function argmin() and argmax() with bool type

I'm new to python, and now try to understand the argmin() and argmax() function in Numpy. The code for example is shown as follows: import numpy as np b = np.array([4,9,13,20]) print(b.argmin(),b.argmax()) it returns '0' and '3', which represent…
Yongwu Xiu
  • 125
  • 1
  • 9
1
vote
1 answer

Argmax - differentiate between array having same values and array having largest zeroth component

I am implementing the argmax function form numpy library to get the index of the largest element in a vector. Following is my code. import numpy as np a = np.array([2, 0, 0, 0, 0]) idx = np.argmax(a) # Works fine, returns 0 b = np.array([0, 0, 0,…
learner
  • 3,168
  • 3
  • 18
  • 35
1
vote
1 answer

How does argmax work when given a 3d tensor - tensorflow

I was wondering how does argmax work when given a 3D tensor. I know what happens when it has a 2D tesnor but 3D is confusing me a lot. Example: import tensorflow as tf import numpy as np sess = tf.Session() coordinates = np.random.randint(0, 100,…
Perl Del Rey
  • 959
  • 1
  • 11
  • 25
1
vote
2 answers

Python: nanargmax version for ndarray

I'm super new to Python. I have a multidimensional array which contains NaNs. How do I get the index of the maximum value in the array? I tried using numpy.nanargmax(), but it seems that I can't use nanargmax with an ndarray? >>>…
Mrinmayi
  • 37
  • 1
  • 5
1
vote
1 answer

how to find a row in dataset, which has maximum number of cells using pandas library?

for example: index1 dog 32 height index2 cat 20 height weight In this example, the second row has 5 columns so how to find the row which maximum columns(cells)?
1
vote
1 answer

How to find argmax of last 2 axes

Hey I have seen this question - Numpy: argmax over multiple axes without loop but the output is not the shape I desire. So for example, if I give the function an array of dimension: 10x20x12x12x2x2, it will output an array of dimension: 10x20x12x12,…
user10794514
1
vote
1 answer

How to fix the error "Singleton array cannot be considered a valid collection"

I'm starting my first machine learning code with python. But, I encountered an error while developing the confusion matrix for my multiclass model. #Defining the model model =…
Asma Mekki
  • 47
  • 1
  • 10
1
vote
2 answers

Which is an efficient way to output the last value in np.argmax(array > value) instead of 0 if no element was found?

Lets take np.array: array = np.array([[1, 4], [0, 3], [2, 3]]) I use this code to find the first element in the first column, where a value is larger than threshold: index = np.argmax(array[:, 0] >…
Peer Breier
  • 361
  • 2
  • 13
1
vote
1 answer

Is argmax working properly for an array of Strings?

I'm using the function Argmax to get the string with max size in an array of Strings, and when the string contains a repetition of a single character, the result get weird. For example: x = ["ABC", "AAAA"] argmax(x) # 1 # The return of argmax is 1,…
1
vote
2 answers

torch.argmax() fails to find a maximum value in a tensor containing data

I have a tensor of shape [batch_size, channel, depth, height, width]: torch.Size([1, 1, 32, 64, 64]) with data: tensor([[[[[-1.8540, -2.8068, -2.7348, ..., -1.9074, -1.8227, -1.4540], [-2.7012, -4.2785, -3.7421, ..., -3.1961, -2.7786,…
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
1
vote
1 answer

How to use argmax to return indices into multidimensional ndarray that cannot be re-shaped into a matrix?

Given an array of 2, 9x9 images with 2 channels shaped like this: img1 = img1 = np.arange(162).reshape(9,9,2).copy() img2 = img1 * 2 batch = np.array([img1, img2]) I need to slice each image into 3x3x2 (stride=3) regions and then locate and replace…
Kentzo
  • 3,881
  • 29
  • 54