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
3
votes
2 answers

N largest / idxmax of a groupby.agg in a pandas data frame

I'd like to find, not the single largest index, but the n largest indexes for a pandas groupby.agg operation. My data frame looks like so: >>> dat = [{"date": datetime.date(2021,1,1), "category": "cats", "var": 1}, {"date":…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
3
votes
3 answers

np.argmax that returns tuple

I have a matrix A of shape (n, m, s). At each position in the 0th axis, I need the position corresponding to the maximum in the (m, s)-shaped array. For example: np.random.seed(1) A = np.random.randint(0, 10, size=[10, 3, 3]) A[0] is: array([[5, 8,…
Liubove
  • 57
  • 6
3
votes
1 answer

Time complexity / algorithm used for pandas `idxmax` method

Hi I am trying to understand how efficient pd.DataFrame.idxmax to see if it is worth replacing with a custom algorithm which might be more efficient (ie using binary search for example). I would like to understand the algorithm behind this method or…
PavlosCh
  • 45
  • 3
3
votes
1 answer

Pandas - rank the input value based on column values

Need help in assigning a rank / variable based on the input value and where does that stand column values of percentiles Example: If input value = Min column value --> Rank 1 input value between Min column value and P25 column value --> Rank…
Sharif
  • 194
  • 2
  • 12
3
votes
2 answers

Is there a way to find the UNIQUE row indices of maximum columnar values in a 2D NumPy array?

For each column in a 2D NumPy array, the column's maximum value can appear more than once. I would like to find the row index for each column maximum, without repeating row indices. Here is an example that demonstrates why np.argmax doesn't…
ToddP
  • 652
  • 13
  • 18
3
votes
2 answers

Does Kotlin have something similar to an argmax method?

So let's say I have a numpy array like this: import numpy as np mat = np.array([[4, 8, 1], [5, 10, 6]]) print(np.argmax(mat)) # prints 4 print(np.argmax(mat, axis=1)) # prints [1 1], index of maximum values along the rows Does Kotlin have a…
dzsezusz
  • 106
  • 9
3
votes
1 answer

How to find source indexes of window max for dataframe?

I have a dataframe with DatetimeIndex and I want to find maximum elements for each window. But also I have to know indexes of elements. Example data: data = pd.DataFrame( index=pd.date_range(start=pd.to_datetime('2010-10-10 12:00:00'), …
3
votes
3 answers

BigQuery argmax: Is array order maintained when doing CROSS JOIN UNNEST

Question: In BigQuery, standard SQL, if I run SELECT * FROM mytable CROSS JOIN UNNEST(mytable.array) Can I be certain that the resulting row order is the same as the array order? Example: Let's say I have the following table mytable: Row | id |…
dlebech
  • 1,817
  • 14
  • 27
3
votes
2 answers

How to build argsecondmax in Numpy

In Numpy, argmax is already defined, but I need argsecondmax, which is basically the second maxima. How can I do this, I'm a bit confused?
S.Mandal
  • 171
  • 2
  • 8
3
votes
2 answers

Tricky numpy argmax on last dimension of 3-dimensional ndarray

if have an array of shape (9,1,3). array([[[ 6, 12, 108]], [[122, 112, 38]], [[ 57, 101, 62]], [[119, 76, 177]], [[ 46, 62, 2]], [[127, 61, 155]], [[ 5, 6, 151]], [[ 5, 8, 185]], [[109, 167, 33]]]) I…
3
votes
1 answer

How to find all argmax in ndarray

I have a 2 dimensional NumPy ndarray. array([[ 0., 20., -2.], [ 2., 1., 0.], [ 4., 3., 20.]]) How can I get all indices of the maximum elements? So I would like as output array([0,1],[2,2]).
lizaveta
  • 353
  • 1
  • 13
3
votes
2 answers

How do you find the top 3 probabilities from 10 columns in a row in Pandas DataFrame?

I have a Pandas Dataframe with a probability for each column and I have 10 columns. Row represents a record. I'd like to select top 3 probabilities and return that column names (kind of like argmax). Since I have a large volume of rows, I don't…
user3368526
  • 2,168
  • 10
  • 37
  • 52
3
votes
1 answer

Python - Get the coordinates of densest point

I'm using numpy and scipy to generate a density plot from 3D coordinate information. I can generate a density plot of the data successfully by generating a KDE with the following code xyz = np.vstack([x,y,z]) kde = stats.gaussian_kde(xyz) density =…
stoves
  • 778
  • 1
  • 11
  • 25
3
votes
2 answers

How to get rows in pandas data frame, with maximal values in a column and keep the original index?

I have a pandas data frame. In the first column it can have the same value several times (in other words, the values in the first column are not unique). Whenever I have several rows that contain the same value in the first column, I would like to…
Roman
  • 124,451
  • 167
  • 349
  • 456
2
votes
1 answer

Optimising finding the index of the highest value in a list

I have a long list of machine learning prediction probabilities for multiple classes and I'm trying to find the highest probability for each prediction. I've implemented the method below for this and it works but it is taking a long time (~10 mins…
JackLidge
  • 391
  • 4
  • 16
1 2
3
11 12