7

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.

7
  • What's the desired output? Commented Jan 5, 2014 at 20:17
  • The comment behind the ???. Given a 2D matrix of points, return the x-and y-value which is lowest. Commented Jan 5, 2014 at 20:18
  • So... basically data consists of two 2D arrays, the first representing all the x coordinates and the second representing all the y coordinates, and you want to get the minimum from each? In that case I think your description is a little inconsistent: it would be the last two coordinates, not the first two, that don't have any important meaning. Commented Jan 5, 2014 at 20:28
  • I clarified what I want. Commented Jan 5, 2014 at 20:32
  • @reindeer don't look that you did :) what output you expect if you replace 11, 12 with 13, 12? and please provide sample data with more points Commented Jan 5, 2014 at 20:36

5 Answers 5

8

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure that's what OP asked for, but it answered my question "How do I find the position of the minimum element in a 2D array?"!
7

Seems like you need consecutive min along axis. 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 like this (with numpy >= 1.7, as pointed out by @Jaime)

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

6 Comments

I'm pretty sure this is not what the question is asking for. It wants the minimum value from each pair, and the fact that the result is the same as taking a slice is just coincidental.
Why do you use first time min and second time amin?
@reindeer just a legacy from your code; this is the same function (np.amin is np.min returs True for sure)
In numpy > 1.7 you can do np.min(data, axis=(0, 1)) and spare yourself an intermediate array.
@Jaime just have thought about giving it a try (didn't yet ever), my initial guess was that this functionality would be rather implemented in pandas :) Can't test with exactly 1.7 (have 1.6 and 1.8 at hand), will it work in 1.7?
|
1

You should take the min of a slice of the array. Assuming first coordinate is x and second is y

minx = min(a[:,0])
miny = min(a[:,1])

>>>a=np.array([[1,2],[3,4],[5,6]])

>>> a
array([[1, 2],
   [3, 4],
   [5, 6]])

>>> min(a[:,0])
1
>>> max(a[:,0])
5
>>> max(a[:,1])
6

Comments

0
minx = np.min(data[0])
miny = np.min(data[1])

Comments

0

You can use function numpy.amin to find the minima along the desired axis.

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.