python-不是一维的一维numpy / scipy插值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-不是一维的一维numpy / scipy插值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2098字,纯文字阅读大概需要3分钟。
内容图文
![python-不是一维的一维numpy / scipy插值](/upload/InfoBanner/zyjiaocheng/678/14d6658cc11c4adb93f81f7cdc984a6d.jpg)
numpy数组数据具有….数据.块状数组z具有距离. data和z具有相同的形状,z的每个点是测量数据的相应点的距离.使事情复杂化的是,用户将提供3、4或5维的data / z数组.
我想将数据插值到一维numpy数组dists中的一组距离.由于数据结构的原因,内插轴始终是从末端开始的两个轴,即,如果数组具有3个维,则内插轴为0;如果数组有4维,插值轴为1,依此类推.由于AFAICT,所有的numpy / scipy插值例程都希望在1D数组中给出原始距离,因此在dists上插值数据和z似乎有点复杂. .这就是我所拥有的:
def dist_interp(data, z, dists):
# construct array to hold interpolation results
num_dims = len(data.shape)
interp_axis = num_dims-3
interp_shape = list(data.shape)
interp_shape[interp_axis] = dists.shape[0]
interp_res = np.zeros(shape=interp_shape)
# depending on usage, data could have between 3 and five dimensions.
# add dims to generalize. I hate doing it this way. Must be
# some other way.
for n in range(num_dims, 5) :
data = np.expand_dims(data, axis=0)
z = np.expand_dims(z, axis=0)
interp_res = np.expand_dims(interp_res, axis=0)
for m in range(data.shape[0]):
for l in range(data.shape[1]):
for j in range(data.shape[3]):
for i in range(data.shape[4]):
interp_res[m,l,:,j,i]=(
np.interp(dists,z[m,l,:,j,i],
data[m,l,:,j,i]))
# now remove extra "wrapping" dimensions
for n in range(0,5-num_dims):
interp_res = interp_res[0]
return(interp_res)
我认为这可以解决问题,但是添加和删除多余的“包装”虚拟尺寸非常笨拙,并且使代码根本不紧凑.还有更好的主意吗?谢谢.
解决方法:
作为此类情况的一般经验法则:
>将要填充的轴移动到形状元组的末端
>将结果数组重塑为2D
>从这个2D模型创建新阵列
>撤消新阵列上的步骤2和1
对于您的情况,它可能类似于:
# Create some random test data
axis = -2
shape = np.random.randint(10, size=(5,))
data = np.random.rand(*shape)
data = np.sort(data, axis=axis)
z = np.random.rand(*shape)
dists = np.linspace(0,1, num=100)
data = np.rollaxis(data, axis, data.ndim)
new_shape = data.shape
data = data.reshape(-1, data.shape[-1])
z = np.rollaxis(z, axis, z.ndim)
z = z.reshape(-1, z.shape[-1])
out = np.empty(z.shape[:1]+dists.shape)
for o, x, f in zip(out, data, z):
o[:] = np.interp(dists, x, f)
out = out.reshape(new_shape[:-1]+dists.shape)
out = np.rollaxis(out, -1, axis)
内容总结
以上是互联网集市为您收集整理的python-不是一维的一维numpy / scipy插值全部内容,希望文章能够帮你解决python-不是一维的一维numpy / scipy插值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。