ibi0tux - 1 year ago 86

Python Question

I have a lot of data in database under (x, y, value) triplet form.

I would like to be able to create dynamically a 2d numpy array from this data by setting

`value`

`(x,y)`

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 ?

Answer Source

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/