gpt4 book ai didi

python - numpy 插值以增加矢量大小

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:11 31 4
gpt4 key购买 nike

您好,我必须扩大矢量内部的点数才能将矢量扩大到固定大小。例如:

对于这个简单的向量

>>> a = np.array([0, 1, 2, 3, 4, 5])
>>> len(a)
# 6

现在,我想得到一个大小为 11 的向量,以 a 向量为基础,结果将是

# array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ])

编辑 1

我需要的是一个函数,它将输入基本向量和必须是结果向量的值的数量,然后我返回一个大小等于参数的新向量。像

def enlargeVector(vector, size):
.....
return newVector

像这样使用:

>>> a = np.array([0, 1, 2, 3, 4, 5])
>>> b = enlargeVector(a, 200):
>>> len(b)
# 200

和b包含线性、三次或任何插值方法的数据结果

最佳答案

scipy.interpolate 中有很多方法可以做到这一点。我最喜欢的是 UnivariateSpline,它生成 k 阶样条,保证可微 k 次。

使用方法:

from scipy.interpolate import UnivariateSpline
old_indices = np.arange(0,len(a))
new_length = 11
new_indices = np.linspace(0,len(a)-1,new_length)
spl = UnivariateSpline(old_indices,a,k=3,s=0)
new_array = spl(new_indices)

s 是一个平滑因子,在这种情况下您应该将其设置为 0(因为数据是准确的)。

请注意,对于您指定的问题(因为 a 只是单调递增 1),这是多余的,因为第二个 np.linspace 已经给出了所需的输出.

编辑:澄清长度是任意的

关于python - numpy 插值以增加矢量大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724546/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com