Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python实现样条插值_Python_Interpolation_Spline_Cubic - Fatal编程技术网

用Python实现样条插值

用Python实现样条插值,python,interpolation,spline,cubic,Python,Interpolation,Spline,Cubic,我编写了以下代码来执行样条曲线插值: import numpy as np import scipy as sp x1 = [1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02] y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95] x = np.array(x1

我编写了以下代码来执行样条曲线插值:

import numpy as np
import scipy as sp

x1 = [1., 0.88,  0.67,  0.50,  0.35,  0.27, 0.18,  0.11,  0.08,  0.04,  0.04,  0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

x = np.array(x1)
y = np.array(y1)

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
但我得到了:

ValueError: A value in x_new is below the interpolation range.
interpolate.py中

任何帮助都将不胜感激。

来自:

scipy.interpolate.interp1d(x,y,kind='linear',axis=-1,copy=True,bounds\u error=True,fill\u value=np.nan)

x:你喜欢什么。单调递增实值的一维数组

问题是x值不正确。事实上,它们是单调递减的。让我知道这是否有效,以及这是否仍然是您正在寻找的计算:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x1 = sorted([1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02])
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
从:

scipy.interpolate.interp1d(x,y,kind='linear',axis=-1,copy=True,bounds\u error=True,fill\u value=np.nan)

x:你喜欢什么。单调递增实值的一维数组

问题是x值不正确。事实上,它们是单调递减的。让我知道这是否有效,以及这是否仍然是您正在寻找的计算:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x1 = sorted([1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02])
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

您可以通过以下方式获得:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x1 = [1., 0.88,  0.67,  0.50,  0.35,  0.27, 0.18,  0.11,  0.08,  0.04,  0.04,  0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

# Combine lists into list of tuples
points = zip(x1, y1)

# Sort list of tuples by x-value
points = sorted(points, key=lambda point: point[0])

# Split list of tuples into two list of x values any y values
x1, y1 = zip(*points)

new_length = 25
new_x = np.linspace(min(x1), max(x1), new_length)
new_y = sp.interpolate.interp1d(x1, y1, kind='cubic')(new_x)

您可以通过以下方式获得:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x1 = [1., 0.88,  0.67,  0.50,  0.35,  0.27, 0.18,  0.11,  0.08,  0.04,  0.04,  0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

# Combine lists into list of tuples
points = zip(x1, y1)

# Sort list of tuples by x-value
points = sorted(points, key=lambda point: point[0])

# Split list of tuples into two list of x values any y values
x1, y1 = zip(*points)

new_length = 25
new_x = np.linspace(min(x1), max(x1), new_length)
new_y = sp.interpolate.interp1d(x1, y1, kind='cubic')(new_x)

我刚刚得到了上面的错误,并用删除X和Y数组中的重复值修复了它

x = np.sort(np.array([0, .2, .2, .4, .6, .9]))
y = np.sort(np.sort(np.array([0, .1, .06, .11, .25, .55]))
⬇ 将0.2更改为0.3或任意数字

x = np.sort(np.array([0, .2, .3, .4, .6, .9]))
y = np.sort(np.sort(np.array([0, .1, .06, .11, .25, .55]))

我刚刚得到了上面的错误,并用删除X和Y数组中的重复值修复了它

x = np.sort(np.array([0, .2, .2, .4, .6, .9]))
y = np.sort(np.sort(np.array([0, .1, .06, .11, .25, .55]))
⬇ 将0.2更改为0.3或任意数字

x = np.sort(np.array([0, .2, .3, .4, .6, .9]))
y = np.sort(np.sort(np.array([0, .1, .06, .11, .25, .55]))

我尝试运行您的代码,但发现
名称“np”未定义
。这是您的整个程序吗?不,您需要导入:numpy作为np,scipy作为sp,scipy.interpolation我尝试运行您的代码,但未定义名称“np”。这是您的整个程序吗?不,您需要导入:numpy作为np,scipy作为sp,scipy.interpolatein而不是
sorted()
您可以只使用
x1.reverse()
y1.reverse()
。在本例中,x1,y1对是通过只对x1数组进行排序而断开的。y1数组的洗牌方式应与x1数组的洗牌方式相同。您可以使用
x1.reverse()
y1.reverse()
来代替
sorted()
。在本例中,x1,y1对是通过仅对x1数组进行排序而断开的。y1数组的洗牌方式应该与x1数组的洗牌方式相同。使用上面的代码,我在最后一行中得到了这个错误,使用Python 3.6:
ValueError:Expect x应该是一个类似于一维排序数组的数组。
@K.-MichaelAye对我来说,它可以与Python 2.7和Python 3.5一起工作。我有numpy 1.11和scipy 0.17.0。您使用哪个版本?事实上,对于Python3.5Numpy1.11.3和Scipy0.17.1,它可以正常工作。它也适用于scipy 0.18.1和numpy 1.11.3,但随后使用scipy 0.19.1中断。显然,
interp1d
现在已经被弃用了。@K.-MichaelAye:谢谢。似乎没有被弃用,但实际上它在scipy 0.19上失败了,并且可以与0.18.1一起使用。参见这里的讨论,interp1d现在是单变量样条线,使用上面的代码,我在最后一行中得到了这个错误,使用Python 3.6:
ValueError:Expect x是一个一维排序数组。\u类似。
@K.-MichaelAye,它可以与Python2.7和Python3.5配合使用。我有numpy 1.11和scipy 0.17.0。您使用哪个版本?事实上,对于Python3.5Numpy1.11.3和Scipy0.17.1,它可以正常工作。它也适用于scipy 0.18.1和numpy 1.11.3,但随后使用scipy 0.19.1中断。显然,
interp1d
现在已经被弃用了。@K.-MichaelAye:谢谢。似乎没有被弃用,但实际上它在scipy 0.19中失败,并与0.18.1一起使用。参见此处的讨论,interp1d现在是单变量样条线