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
347
votes
14 answers

Find row where values for column is maximal in a pandas DataFrame

How can I find the row for which the value of a specific column is maximal? df.max() will give me the maximal value for each column, I don't know how to get the corresponding row.
Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49
100
votes
1 answer

What is the meaning of axis=-1 in keras.argmax?

I am a beginner in Keras and need help to understand keras.argmax(a, axis=-1) and keras.max(a, axis=-1). What is the meaning of axis=-1 when a.shape = (19, 19, 5, 80)? And also what will be the output of keras.argmax(a, axis=-1) and keras.max(a,…
youngtackpark
  • 1,475
  • 3
  • 12
  • 14
31
votes
5 answers

numpy: what is the logic of the argmin() and argmax() functions?

I can not understand the output of argmax and argmin when use with the axis parameter. For example: >>> a = np.array([[1,2,4,7], [9,88,6,45], [9,76,3,4]]) >>> a array([[ 1, 2, 4, 7], [ 9, 88, 6, 45], [ 9, 76, 3, 4]]) >>>…
user4584333
27
votes
2 answers

Octave position of maximum value in column

I want to find the argmax of the values in a matrix by column, e.g.: 1 2 3 2 3 3 4 5 6 -> 3 7 8 I feel like I should just be able to map an argmax/posmax function over the columns, but I don't see a particularly intuitive way to do this in…
Philip Massey
  • 1,401
  • 3
  • 14
  • 24
17
votes
2 answers

pandas idxmax: return all rows in case of ties

I am working with a dataframe where I have weight each row by its probability. Now, I want to select the row with the highest probability and I am using pandas idxmax() to do so, however when there are ties, it just returns the first row among the…
Praderas
  • 471
  • 4
  • 14
14
votes
2 answers

Why does dim=1 return row indices in torch.argmax?

I am working on argmax function of PyTorch which is defined as: torch.argmax(input, dim=None, keepdim=False) Consider an example a = torch.randn(4, 4) print(a) print(torch.argmax(a, dim=1)) Here when I use dim=1 instead of searching column…
Programmer
  • 165
  • 1
  • 1
  • 7
13
votes
4 answers

Set value of first item in slice in python pandas

So I would like make a slice of a dataframe and then set the value of the first item in that slice without copying the dataframe. For example: df = pandas.DataFrame(numpy.random.rand(3,1)) df[df[0]>0][0] = 0 The slice here is irrelevant and just…
RexFuzzle
  • 1,412
  • 2
  • 17
  • 30
11
votes
3 answers

Is there a Julia analogue to numpy.argmax?

In Python, there is numpy.argmax: In [7]: a = np.random.rand(5,3) In [8]: a Out[8]: array([[ 0.00108039, 0.16885304, 0.18129883], [ 0.42661574, 0.78217538, 0.43942868], [ 0.34321459, 0.53835544, 0.72364813], […
Lanting Guo
  • 795
  • 3
  • 7
  • 21
11
votes
3 answers

arg max in Java 8 streams?

I often need the maximum element of a collection according to the maximization of a criterion which produces a double or int value. Streams have the max() function which requires me to implement a comparator, which I find cumbersome. Is there a…
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
10
votes
4 answers

Find first and last non-zero column in each row of a pandas dataframe

I have DataFrame in view of Name and Date with values of weight in cells : Name Jan17 Jun18 Dec18 Apr19 count Nick 0 1.7 3.7 0 2 Jack 0 0 2.8 3.5 2 Fox 0 1.7 …
Cindy
  • 568
  • 7
  • 20
10
votes
2 answers

Numpy: argmax over multiple axes without loop

I have a N-dimensional array (Named A). For each row of the first axis of A, I want to obtain the coordinates of the maximum value along the other axes of A. Then I would return a 2-dimensional array with the coordinates of the maximum value for…
CarlosH
  • 319
  • 1
  • 4
  • 8
7
votes
3 answers

What is the purpose of the ArgMin and ArgMax type synonyms in Data.Semigroup?

The base library in Haskell has the following type synonyms in Data.Semigroup: type ArgMin a b = Min (Arg a b) type ArgMax a b = Max (Arg a b) Here are links to the haddocks: ArgMin and ArgMax What is the purpose of these two type synonyms? …
illabout
  • 3,517
  • 1
  • 18
  • 39
7
votes
3 answers

Find the column index which has the maximum value for each row

I have below data frame: Name1 Scr1 Name2 Scr2 Name3 Scr3 NY 21 CA 45 SF 37 AZ 31 BK 46 AK 23 I am trying to get the maximum value of each row and corresponding name for each row: df.idxmax(axis=1) But how do i…
msksantosh
  • 379
  • 4
  • 19
7
votes
3 answers

python numpy argmax to max in multidimensional array

I have the following code: import numpy as np sample = np.random.random((10,10,3)) argmax_indices = np.argmax(sample, axis=2) i.e. I take the argmax along axis=2 and it gives me a (10,10) matrix. Now, I want to assign these indices value 0. For…
Rayyan Riaz
  • 97
  • 1
  • 1
  • 6
7
votes
1 answer

Scala/Spark dataframes: find the column name corresponding to the max

In Scala/Spark, having a dataframe: val dfIn = sqlContext.createDataFrame(Seq( ("r0", 0, 2, 3), ("r1", 1, 0, 0), ("r2", 0, 2, 2))).toDF("id", "c0", "c1", "c2") I would like to compute a new column maxCol holding the name of the column…
ivankeller
  • 1,923
  • 1
  • 19
  • 20
1
2 3
11 12