如何较粗的2-D阵列数据解析 [英] How to coarser the 2-d array data resolution
问题描述
我有一个类似的问题<一href=\"http://stackoverflow.com/questions/33862849/how-to-change-the-resolution-of-tif-raster-file-without-losing-data\">here
这个问题是关于类似于TIFF数据,我想找到一个更普遍的方式来对付它。
I have a similar question here
That question is about resemble the tiff data, and I want to find a more universal way to deal with it.
例如:
- 2-D numpy的阵列的200x150再present 1公里×1公里分辨率的人口密度数据的形状。
- 我的目标:改变空间分辨率=>5公里×5公里分辨率的
- My target: change the space resolution => 5 km x 5 km resolution
这是一个例子picturefor随机分布的数据集群到网格
搜索
*红点:原始数据结果
*蓝点:网格网络重新present二维数组
*绿色圆圈:查找每个红点最近的蓝点,总结他们的结果。
*在这个问题中,所不同的是,原来的数据是2-D numpy的阵列太
this is an example picturefor random distributed data cluster into grid network
* the red point: original data
* the blue dot: grid network represent the 2-d array * the green circle: find the nearest blue dot for each red point and sum them.
* In this question, the difference is that the original data is 2-d numpy array too.
-
我的另一个问题<相似href=\"http://stackoverflow.com/questions/34668709/find-a-easier-way-to-cluster-2-d-scatter-data-into-grid-array-data/34676067#34676067\">here我簇2-D散点最近的网格点。我AP preciate由@HYRY支持的答案这提高了我的code很多。
My solution
Similar with my another question here which I cluster 2-d scatter point to nearest grid point. And I appreciate the answer supported by @HYRY which improved my code a lot.
在这个问题中,我使用KD树算法查找每个原始点数据的最近的网络节点。结果表明这里:结果
In that question, I use KD-tree algorithm to find the nearest network node of each original point data. The result shows here:
我想一定是重塑结构化的2-D numpy的阵列,而不是随机2-D散点一些简单的方法。
I think there must be some easier way to reshape the structured 2-d numpy array rather than random 2-d scatter point.
感谢您从@Praveen答案。结果
我有另一种方法使用的 SciPy的插值2D 功能。Thanks for the answer from @Praveen.
I have another method using scipy interpolate 2d function.这是我的code:
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)
结果显示,如:结果
The result shows like:
- 是否有一些问题,当我使用插值来粗糙的数据?
有没有办法,我可以按位置(X,Y)?
Is there some useful function that I can sample some data from origin array dataset by location(x,y)?
推荐答案
如果我理解正确的话,你有一个很细的人口密度地图,你正在努力使粗,由每一个5×5像素区域内聚集人口密度。是这样吗?
If I understood you correctly, you have a very fine population density map, which you're trying to make coarse, by aggregating population densities within every 5x5 pixel zone. Is that right?
所以,当你说你试图让1公里点¯x1公里可获得5公里点¯x5公里你的意思是每个像素的目前的1公里外点¯x1公里区域重新presents人口,而要使其在5公里点¯x5公里区域重新present人口。
So when you say you're trying to make 1km x 1km into 5km x 5km you mean that each pixel currently represents population in a 1km x 1km region, whereas you want to make it represent population in a 5km x 5km region.
如果是这样,请不要使用集群和KD树!这将是做一些事情就简单得多了一个可怕的低效的方式。
If so, please don't use clustering and KD-trees! That would be a horribly inefficient way to do something far simpler.
这可能是你想要的东西。为了解释:
This is probably what you want. To explain:
# Suppose the 2D array is pop_density coarseness = 5 temp = pop_density.reshape((pop_density.shape[0] // coarseness, coarseness, pop_density[1] // coarseness, coarseness)) coarse_pop_density = np.sum(temp, axis=(1,3))
作为对方的回答说,这将只有形状
pop_density
是粗糙
。我相信这是你的情况下,因为你说你有一个200x150的图像,你正在努力为5倍,使粗糙。As stated in the other answer, this will only work if the shape of
pop_density
is an exact multiple ofcoarseness
. I believe this is the case for you, since you say you have a 200x150 image, which you're trying to make coarse by a factor of 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))
这篇关于如何较粗的2-D阵列数据解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!