Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get minimum x and y from 2D numpy array of points

Given a numpy 2D array of points, aka 3D array with size of the 3rd dimension equals to 2, how do I get the minimum x and y coordinate over all points?

Examples:

First:

I edited my original example, since it was wrong.

data = np.array(
      [[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[11, 12],
        [13, 14],
        [15, 16]]])

minx = 0 # data[0][0][0]
miny = 1 # data[0][0][1]

4 x 4 x 2:

Second:

array([[[ 0, 77],
        [29, 12],
        [28, 71],
        [46, 17]],
       [[45, 76],
        [33, 82],
        [14, 17],
        [ 3, 18]],
       [[99, 40],
        [96,  3],
        [74, 60],
        [ 4, 57]],
       [[67, 57],
        [23, 81],
        [12, 12],
        [45, 98]]])

minx = 0 # data[0][0][0]
miny = 3 # data[2][1][1]

Is there an easy way to get now the minimum x and y coordinates of all points of the data? I played around with amin and different axis values, but nothing worked.

Clarification:

My array stores positions from different robots over time. First dimension is time, second is the index of an robot. The third dimension is then either x or y of a robots for a given time.

Since I want to draw their paths to pixels, I need to normalize my data, so that the points are as close as possible to the origin without getting negative. I thought that subtracting [minx,miny] from every point will do that for me.

like image 971
jcklie Avatar asked Jan 05 '14 20:01

jcklie


2 Answers

alko's answer didn't work for me, so here's what I did:

import numpy as np

array = np.arange(15).reshape(5,3)
x,y = np.unravel_index(np.argmin(array),array.shape)
like image 92
Kshitiz Sethia Avatar answered Sep 28 '22 19:09

Kshitiz Sethia


Seems you need consecutive min alongaxis. For your first example:

>>> np.min(np.min(data, axis=1), axis=0)
array([ 0, 1])

For the second:

>>> np.min(np.min(data, axis=1), axis=0)
array([0, 3])

The same expression can be stated (in numpy older than 1.7), as pointed out by @Jamie, s

>>> np.min(data, axis=(1, 0))
array([0, 3])
like image 20
alko Avatar answered Sep 28 '22 19:09

alko