python – 如何更粗糙的二维数组数据分辨率
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何更粗糙的二维数组数据分辨率,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2968字,纯文字阅读大概需要5分钟。
内容图文
![python – 如何更粗糙的二维数组数据分辨率](/upload/InfoBanner/zyjiaocheng/810/14f09424ec5d4bbca04e8cc586a1137d.jpg)
我有一个类似的问题here
这个问题与tiff数据类似,我想找到一种更普遍的方法来处理它.
我的问题
例如:
>形状为200×150的2维numpy阵列代表1 km x 1 km分辨率的人口密度数据.
http://i4.tietuku.com/060fe38e9ccf6daf.png
>我的目标:改变空间分辨率=> 5公里x 5公里分辨率
this is an example picturefor random distributed data cluster into grid network
07002
* 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.
我的解决方案
>类似于我的另一个问题here,我将2-d散点指向最近的网格点.我很欣赏@HYRY支持的答案,它改进了我的代码.
>在那个问题中,我使用KD树算法来查找每个原始点数据的最近网络节点.结果显示在这里:
http://i4.tietuku.com/1a420c48ed7bcb1c.png
>我认为必须有一些更简单的方法来重塑结构化的2-d numpy数组而不是随机的2-d散点.
添加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)从原始数组数据集中采样一些数据?
解决方法:
如果我理解正确的话,你可以通过聚合每个5×5像素区域内的人口密度来获得非常精细的人口密度图,这是你试图做的粗略的.是对的吗?
因此,当你说你试图将1公里x 1公里变成5公里x 5公里时你的意思是每个像素目前代表1公里x 1公里区域内的人口,而你想让它代表5公里x 5公里区域内的人口.
如果是这样,请不要使用群集和KD树!这将是一种非常低效的方式来做更简单的事情.
This可能就是你想要的.解释:
# 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的形状是粗糙度的精确倍数时才有效.我相信你就是这种情况,因为你说你有一张200×150的图像,你试图让它粗糙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))
内容总结
以上是互联网集市为您收集整理的python – 如何更粗糙的二维数组数据分辨率全部内容,希望文章能够帮你解决python – 如何更粗糙的二维数组数据分辨率所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。