How to simplify the resolution of 2-dimensional array data

I have a similar question here.
This question is similar to tiff data, and I want to find a more universal way to deal with it.

My question

For example:

  • A 200x150 two-dimensional matrix represents population density of 1 km x 1 km.

http://i4.tietuku.com/060fe38e9ccf6daf.png

  • My goal : change the resolution of space => 5 km x 5 km.

this is an example image for a random distributed cluster of data in a network grid http://i4.tietuku.com/a4c1355413f8cdbc.png
* red dot: source data
* blue dot: mesh grid is the 2nd array * green circle: find the nearest blue dot for each red dot and sum them up.
* In this question, the difference is that the source data is also a 2-dimensional numpy array.

My decision

  • Similar to my other question here , in which I group a 2-dimensional scatter point to the nearest grid point. And I appreciate the answer @HYRY supports, which greatly improved my code.

  • KD- node . :
    http://i4.tietuku.com/1a420c48ed7bcb1c.png

  • , - 2-d numpy, 2- .

2016-01-09

@Praveen.
scipy interpolate 2d.

:

 xi  = np.linspace(x_map1,x_map2,pop.shape[1])
 yi  = np.linspace(y_map1,y_map2,pop.shape[0])
 hfunc = interpolate.interp2d(xi,yi,pop)

 x_grid  = np.linspace(x_map1,x_map2,new_shape_x)
 y_grid  = np.linspace(y_map1,y_map2,new_shape_y)

 new_pop = np.zeros(new_shape_x * new_shape_y)
 t = 0
 for i in range(0,new_shape_y,1):
     for j in range(0,new_shape_y,1):
         new_pop[t] = hfunc(x_grid[j],y_grid[i])
         t+=1
 new_pop = new_pop.reshape(new_shape_y,new_shape_x)
 plt.pcolormesh(new_pop)

, :
http://i4.tietuku.com/b020db6dc2d75d70.png

  • , ?

2

- , (x, y)?

+4
1

, , , 5x5 . ?

, , 1 1 5 5 , , 1 1 , , 5 5 .

, , KD-! - .

, , , . :

# Suppose the 2D array is pop_density
coarseness = 5
temp = pop_density.reshape((pop_density.shape[0] // coarseness, coarseness,
                            pop_density.shape[1] // coarseness, coarseness))
coarse_pop_density = np.sum(temp, axis=(1,3))

, , pop_density coarseness. , , , 200x150, 5 .

,

# Suppose the size of pop_density was 198x147 instead of 200x150.
# Start by finding the next highest multiple of 5x5
shape = np.array(pop_density.shape, dtype=float)
new_shape = coarseness * np.ceil(shape / coarseness).astype(int)
# new_shape is now (200, 150)

# Create the zero-padded array and assign it with the old density
zp_pop_density = np.zeros(new_shape)
zp_pop_density[:shape[0], :shape[1]] = pop_density

# Now use the same method as before
temp = zp_pop_density.reshape((new_shape[0] // coarseness, coarseness,
                               new_shape[1] // coarseness, coarseness))
coarse_pop_density = np.sum(temp, axis=(1,3))
+6

Source: https://habr.com/ru/post/1623508/


All Articles