How to find the indices of the maximum values of the fifth column?

Question:

I have array A as:

    A = [[0.0, 1.0, 3.0, -1.0, -1008.0],
[0.0, 1.0, 3.0, -1.0, -1008.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, -4.0, -1808.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, -3.0, -1648.0],
[0.0, 1.0, 3.0, -3.0, -1648.0],
[0.0, 1.0, 3.0, -3.0, -1648.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, 3.0, -1648.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, 2.0, -1488.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, 2.0, -1488.0],
[0.0, 1.0, 3.0, 3.0, -1648.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 2.0, -1488.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -3.0, -1648.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, -1.0, -1328.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, -3.0, -1648.0],
[0.0, 1.0, 3.0, -2.0, -1488.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, 1.0, -1328.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, 0.0, -1168.0],
[0.0, 1.0, 3.0, 0.0, -1168.0]]

I want to find where the maximum values in column 4 (the fifth column) occur and I want to finally get this result,

m = [0 , 1]

which is where two -1008.0 have happend. I have tried using:

m = np.argwhere(A == np.amax(A))

But I don’t seem to be using it right. Can someone please help me?

Asked By: Sherwin

||

Answers:

I would do it like this:

last_col = np.array(A)[:, -1] # take every row in `A`, but only the last column
print(np.argwhere(last_col == np.amax(last_col)).flatten().tolist())
Answered By: gareth618

You can find the indices where the maximum values occur in the fifth column (column 4) of the array A using NumPy. Here’s how you can do it:

import numpy as np

# Your array
A = np.array([[0.0, 1.0, 3.0, -1.0, -1008.0],
              # ... (your other rows here)
              [0.0, 1.0, 3.0, 0.0, -1168.0]])

# Find the maximum value in the fifth column (column 4)
max_value = np.amax(A[:, 4])

# Find the indices where the maximum value occurs in column 4
max_indices = np.argwhere(A[:, 4] == max_value).flatten()

# Print the result
print(max_indices)

This code first finds the maximum value in the fifth column using np.amax(A[:, 4]). Then, it uses np.argwhere to find the indices where this maximum value occurs in column 4 and flattens the result to get a 1D array of indices. Finally, it prints the max_indices, which will contain the indices where the maximum values occur in column 4.

In your example data, the code should output [0, 1] since there are two occurrences of -1008.0 in column 4 at rows 0 and 1.

Answered By: ApaxPhoenix

I found solution that you should convert it to numpyArray by using

import numpy as np
A=np.array([[....],[.....]])
m = np.argwhere(A == np.amax(A[:,[4]]))
print(m)

it will give you the result that you expected which is

array([[0, 4],[1, 4]])
Answered By: Yousef Rashed

If you are not limited to Numpy, you can achieve this with ordinary lists:

A = [[0.0, 1.0, 3.0, -1.0, -1008.0],
    [0.0, 1.0, 3.0, -1.0, -1008.0],
    [0.0, 1.0, 3.0, 1.0, -1328.0],
    [0.0, 1.0, 3.0, 0.0, -1168.0]]


# Fetch only the 5th column
A_col_5 = [x[4] for x in A]

# Find indices of the maxima
m = [i for i, x in enumerate(A_col_5) if x == max(A_col_5)]

This can likely be condensed into one list comprehension, but I think that having two is better for readability. If the performance is an issue, consider just one or a Numpy solution – they may be faster/leaner on memory.

Here the program first selects only the fifth column and places it into a separate list. Then it finds the indices of all the maxima using this solution.

One important thing here is the comparison x == max(A_col_5). Since your values are floating point values you may not want to rely on a direct comparison with ==. In fact, I would actually use

m = [i for i, x in enumerate(A_col_5) if math.isclose(x, max(A_col_5))]

from the math module (import math upfront), or use integer values if possible.

Answered By: atru
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.