Abrian Abir
Abrian Abir

Reputation: 77

How to get the minimum and maximum value of arrays inside an array?

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.

Upvotes: 4

Views: 357

Answers (3)

Andy
Andy

Reputation: 96

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]

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

IIUC, you can do stack:

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

Output:

array([619, 465, 529])

Upvotes: 3

Sharath
Sharath

Reputation: 226

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

Upvotes: 1

Related Questions