如何使用线性插值扩展数组 [英] How to extend an array with linear interpolation

查看:181
本文介绍了如何使用线性插值扩展数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是将长度为m的数组扩展为长度为 n(n> m)的数组,然后线性插入缺失值。

What I want is to extend an array of length m to an array of length n (n>m), and interpolate the missing values linearly.

例如,我想将此数组 [1,5,1,7] 扩展为长度为<$ c的数组$ c> 7 ,结果应为[1, 3 ,5, 3 ,1, 5 , 7],其中粗体数字是由线性插值产生的。

For example, I want to extend this array [1,5,1,7] to an array of length 7, the result should be [1,3,5,3,1,5,7], where the bold figures result from linear interpolation.

在Python中有一种简单的方法吗?
预先感谢。

Is there an easy way to do this in Python? Thanks in advance.

推荐答案

interp 函数可以满足您的要求。

The interp function from numpy can do what you want.

示例:

>>> xp = [1, 2, 3]
>>> fp = [3, 2, 0]
>>> np.interp(2.5, xp, fp)
1.0
>>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
array([ 3. ,  3. ,  2.5 ,  0.56,  0. ])

这篇关于如何使用线性插值扩展数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆