Fast image coordinate lookup in Numpy - python

I've got a big numpy array full of coordinates (about 400):
[[102, 234],
[304, 104],
....
]
And a numpy 2d array my_map of size 800x800.
What's the fastest way to look up the coordinates given in that array? I tried things like paletting as described in this post: http://opencvpython.blogspot.com/2012/06/fast-array-manipulation-in-numpy.html but couldn't get it to work.
I was also thinking about turning each coordinate into a linear index of the map and then piping it straight into my_map like so:
my_map[linearized_coords]
but I couldn't get vectorize to properly translate the coordinates into a linear fashion. Any ideas?

Try using a tuple for indexing:
my_map[tuple(coords.T)]
This selects elements of the array via advanced indexing.

Related

Find indices of each integer group in a labelled array

I have a labelled array obtained by using scipy measure.label on a binary 2 dimensional array. For argument sake it might look like this:
[
[1,1,0,0,2],
[1,1,1,0,2],
[1,0,0,0,0],
[0,0,0,3,3]
]
I want to get the indices of each group of labels. So in this case:
[
[(0,0),(0,1),(1,0),(1,1),(1,2),(2,0)],
[(0,4),(1,4)],
[(3,3),(3,4)]
]
I can do this using builtin Python like so (n and m are the dimensions of the array):
_dict = {}
for coords in itertools.product(range(n), range(m)):
_dict.setdefault(labelled_array[coords], []).append(coords)
blobs = [np.array(item) for item in _dict.values()]
This is very slow (about 10 times slower than the initial labelling of the binary array using measure.label!)
Scipy also has a function find_objects:
from scipy import ndimage
objs = ndimage.find_objects(labelled_array)
From what I can gather though this is returning the bounding box for each group (object). I don't want the bounding box I want the exact coordinates of each value in the group.
I have also tried using np.where for each integer in the number of labels. This is very slow.
it also seems to me that what I'm tring to do here is something like the minesweeper algorithm. I suspect there must be an efficient solution using numpy or scipy.
Is there an efficient way to obtain these coordinates?

Grouping a numpy array

I have a huge NumPy array of size 778. I would like to pair the elements hence I'm using the following code to do so.
coordinates = coordinates.reshape(-1, 2,2)
However, if I use the following code it just works fine.
coordinates = coordinates[:len(coordinates)-1].reshape(-1, 2,2)
How can I do this in a proper way irrespective of the size?

Reshaping numpy (n, )

I am working with multiple numpy objects that are numpy lists when each list element contains a 2d array. Strangely the shape() function does not reflect this returning only the number of samples overall.
x_train.shape, x_test.shape, x_test.iloc[0].shape
#((22507,), (5627,), (25, 100))
This code snippet accomplishes the task but I am wondering if there is a better/numpy way to accomplish this.
x = []
[x.append(item) for item in x_train]
_np.array(x).shape
# (22507, 25, 100)
I have searched through stack overflow and although there are many reshape questions I have not seen one that can solve this problem efficiently.
A simple and efficient way to convert your 1D dtype=object array of 2D arrays is:
np.stack(x_train)
But it would be more efficient to load the original data into a 3D array in the first place.

How to reshape numpy array of numpy arrays? [duplicate]

I'm trying to turn a list of 2d numpy arrays into a 2d numpy array. For example,
dat_list = []
for i in range(10):
dat_list.append(np.zeros([5, 10]))
What I would like to get out of this list is an array that is (50, 10). However, when I try the following, I get a (10,5,10) array.
output = np.array(dat_list)
Thoughts?
you want to stack them:
np.vstack(dat_list)
Above accepted answer is correct for 2D arrays as you requested. For 3D input arrays though, vstack() will give you a surprising outcome. For those, use stack(<list of 3D arrays>, 0).
See https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
for details. You can use append, but will want to specify the axis on which to append.
dat_list.append(np.zeros([5, 10]),axis=0)

Numpy iterating over 3d vector array

Im trying to iterate over a Numpy Array that contains 3d numpy arrays (3d vectors) inside it.
Something like this:
import numpy as np
Matrix = np.zeros(shape=(10, 3))
# => [
[0,0,0],
[0,0,0],
...
[0,0,0]
]
I need to iterate over it, getting each 3d Vector.
In pseudo code:
for vector in Matrix
print vector #=> [0,0,0]
Is there any Numpy native way of doing this?
What is the fastest way of doing this?
Thanks!
Fran
Your pseudocode is only missing a colon:
for vector in matrix:
print vector
That said, you will generally want to avoid explicit iteration over a NumPy array. Take advantage of broadcasted operations and NumPy built-in functions as much as possible; it moves the loops into C instead of interpreted Python, and it tends to produce shorter code, too.

Categories