4

I am trying to get the minimum and maximum value of a column.

This is my test code:

from numpy import array
import numpy as np

test = [array([[619, 502, 551],
       [623, 502, 551],
       [624, 504, 551]]),
 array([[624, 498, 531],
       [628, 502, 529]]),
 array([[619, 496, 557],
       [892, 508, 559]]),
 array([[619, 494, 561],
       [895, 506, 559],
       [902, 512, 559]]),
 array([[619, 494, 559],
       [918, 510, 567]]),
 array([[619, 493, 561],
       [931, 512, 561],
       [932, 512, 561]]),
 array([[619, 494, 561],
       [942, 510, 559]]),
 array([[619, 493, 561],
       [620, 493, 559],
       [948, 512, 561]]),
 array([[619, 494, 591],
       [752, 542, 633]]),
 array([[626, 465, 567],
       [766, 532, 633]])]

data = array(test)

I've tried np.min, different indices but unsuccessfully.

I expect to get the min and max of ex. column 2 (or any column)

I can't use for loops to go through each item because there are a lot of them in the actual data.

Any and all suggestions would be appreciated. Thank You.

4
  • Are the sizes of the subarrays the same across all rows? Commented Dec 3, 2019 at 17:35
  • The sub-arrays have the same number of columns but a different number of rows. That being said, if a solution works for the test data, it should work for me. Commented Dec 3, 2019 at 17:37
  • 2
    Your code doesn't run on my system, raised only 2 non-keyword arguments accepted error. Commented Dec 3, 2019 at 17:42
  • @QuangHoang Sorry about that, it should work now. Commented Dec 3, 2019 at 17:47

3 Answers 3

3

IIUC, you can do stack:

np.vstack([d for d in data]).min(axis=0)

Output:

array([619, 465, 529])
Sign up to request clarification or add additional context in comments.

Comments

1

You can try the following to get the min/max if the columns are the same.

In [28]: test = [[[619, 502, 551],
    ...:        [624, 504, 551]],
    ...: [[624, 498, 531],
    ...:        [628, 502, 529]],
    ...: [[619, 496, 557],
    ...:        [892, 508, 559]],
    ...: [[619, 494, 561],
    ...:        [895, 506, 559],
    ...:        [902, 512, 559]],
    ...: [[619, 494, 559],
    ...:        [918, 510, 567]],
    ...: [[619, 493, 561],
    ...:        [931, 512, 561],
    ...:        [932, 512, 561]],
    ...: [[619, 494, 561],
    ...:        [942, 510, 559]],
    ...: [[619, 493, 561],
    ...:        [620, 493, 559],
    ...:        [948, 512, 561]],
    ...: [[619, 494, 591],
    ...:        [752, 542, 633]],
    ...: [[626, 465, 567],
    ...:        [766, 532, 633]]]

In [29]: test1 = []

In [30]: [test1.append(t) for t1 in test for t in t1]

In [31]: test1
Out[31]:
[[619, 502, 551],
 [624, 504, 551],
 [624, 498, 531],
 [628, 502, 529],
 [619, 496, 557],
 [892, 508, 559],
 [619, 494, 561],
 [895, 506, 559],
 [902, 512, 559],
 [619, 494, 559],
 [918, 510, 567],
 [619, 493, 561],
 [931, 512, 561],
 [932, 512, 561],
 [619, 494, 561],
 [942, 510, 559],
 [619, 493, 561],
 [620, 493, 559],
 [948, 512, 561],
 [619, 494, 591],
 [752, 542, 633],
 [626, 465, 567],
 [766, 532, 633]]

In [32]: np.amin(test1, None)
Out[32]: 465

In [33]: np.max(test1, None)
Out[33]: 948

Comments

0

If you're looking for just a column you can make use of the indexing of numpy arrays. e.g.

arr = np.asarray(((1,2,3),(4,5,6),(7,8,9)))
print(arr)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

with slicing you can get columns as subarrays like the following example

print(arr[:,0])
[1 4 7]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.