Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Numpy 2D Array with data from triplets of (x,y,value)

I have a lot of data in database in (x, y, value) triplet form.
I would like to be able to create dynamically a 2D numpy array from this data by setting value at the coords (x,y) of the array.

For instance if I have :

(0,0,8)
(0,1,5)
(0,2,3)
(1,0,4)
(1,1,0)
(1,2,0)
(2,0,1)
(2,1,2)
(2,2,5)

The resulting array should be :

Array([[8,5,3],[4,0,0],[1,2,5]])

I'm new to numpy, is there any method in numpy to do so ? If not, what approach would you advice to do this ?

like image 594
ibi0tux Avatar asked Jul 16 '16 07:07

ibi0tux


People also ask

How do you make a 2D NumPy array?

In Python to declare a new 2-dimensional array we can easily use the combination of arange and reshape() method. The reshape() method is used to shape a numpy array without updating its data and arange() function is used to create a new array.

Can you create multi dimensional arrays using NumPy?

In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.

What does [: :] mean on NumPy arrays?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

Can NumPy have mixed data types?

Having a data type (dtype) is one of the key features that distinguishes NumPy arrays from lists. In lists, the types of elements can be mixed.


1 Answers

Extending the answer from @MaxU, in case the coordinates are not ordered in a grid fashion (or in case some coordinates are missing), you can create your array as follows:

import numpy as np

a = np.array([(0,0,8),(0,1,5),(0,2,3),
              (1,0,4),(1,1,0),(1,2,0),
              (2,0,1),(2,1,2),(2,2,5)])

Here a represents your coordinates. It is an (N, 3) array, where N is the number of coordinates (it doesn't have to contain ALL the coordinates). The first column of a (a[:, 0]) contains the Y positions while the second columne (a[:, 1]) contains the X positions. Similarly, the last column (a[:, 2]) contains your values.

Then you can extract the maximum dimensions of your target array:

# Maximum Y and X coordinates
ymax = a[:, 0].max()
xmax = a[:, 1].max()

# Target array
target = np.zeros((ymax+1, xmax+1), a.dtype)

And finally, fill the array with data from your coordinates:

target[a[:, 0], a[:, 1]] = a[:, 2]

The line above sets values in target at a[:, 0] (all Y) and a[:, 1] (all X) locations to their corresponding a[:, 2] value (your value).

>>> target
array([[8, 5, 3],
       [4, 0, 0],
       [1, 2, 5]])

Additionally, if you have missing coordinates, and you want to replace those missing values by some number, you can initialize the array as:

default_value = -1
target = np.full((ymax+1, xmax+1), default_value, a.type)

This way, the coordinates not present in your list will be filled with -1 in the target array/

like image 137
Imanol Luengo Avatar answered Sep 28 '22 02:09

Imanol Luengo