Python 属性错误:模块';scipy.interpolate';没有属性';样条曲线';

Python 属性错误:模块';scipy.interpolate';没有属性';样条曲线';,python,scipy,interpolation,spline,cubic-spline,Python,Scipy,Interpolation,Spline,Cubic Spline,我正在使用Python 3.6并尝试运行以下源代码: 但它会生成一个错误,AttributeError:module'scipy.interpolate'没有属性“spline” 然后我通过从scipy.interpolate导入make_interp_spline修改为以下内容: from __future__ import division import numpy as np import matplotlib.pyplot as plt import scipy.interpolate

我正在使用Python 3.6并尝试运行以下源代码:

但它会生成一个错误,
AttributeError:module'scipy.interpolate'没有属性“spline”

然后我通过从
scipy.interpolate
导入
make_interp_spline
修改为以下内容:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
from scipy.interpolate import make_interp_spline

x = np.array([ 2.,  1.,  1.,  2.,  2.,  4.,  4.,  3.])
y = np.array([ 1.,  2.,  3.,  4.,  2.,  3.,  2.,  1.])
plt.plot(x,y, label='poly')

t = np.arange(x.shape[0], dtype=float)
t /= t[-1]
nt = np.linspace(0, 1, 100)
x1 = make_interp_spline(t, x, nt)
y1 = make_interp_spline(t, y, nt)
plt.plot(x1, y1, label='range_spline')

t = np.zeros(x.shape)
t[1:] = np.sqrt((x[1:] - x[:-1])**2 + (y[1:] - y[:-1])**2)
t = np.cumsum(t)
t /= t[-1]
x2 = make_interp_spline(t, x, nt)
y2 = make_interp_spline(t, y, nt)
plt.plot(x2, y2, label='dist_spline')

plt.legend(loc='best')
plt.show()
我得到了一个新的错误
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

输出:

---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
12 t/=t[-1]
13nt=np.linspace(0,1100)
--->14 x1=生成插值样条曲线(t,x,nt)
15 y1=制造内花键(t,y,nt)
16 plt.绘图(x1,y1,label='range\u样条曲线')
/usr/local/lib/python3.7/site-packages/scipy/interpolate//u bsplines.py in make_interp_样条曲线(x,y,k,t,bc_类型,轴,检查有限)
751
752#特例k=0立即
-->753如果k==0:
754如果有(u不是(t,deriv_l,deriv_r)中的无):
755 raise VALUE ERROR(“k=0的信息太多:只能使用t和bc_类型)”
ValueError:包含多个元素的数组的真值不明确。请使用a.any()或a.all()

想知道如何解决这个问题吗?谢谢。

make_interp_spline的第三个参数是样条线顺序(即三次样条线为3),您使用数组
nt
调用它


如果您想指定自己的结,请使用
t=…
kwarg。

请显示上一个ValueError的确切来源。我添加了完整的
ValueError
,请检查。好的,那么很清楚,请查看我的回答抱歉,我不明白。您能运行代码并显示结果吗?谢谢。
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
from scipy.interpolate import make_interp_spline

x = np.array([ 2.,  1.,  1.,  2.,  2.,  4.,  4.,  3.])
y = np.array([ 1.,  2.,  3.,  4.,  2.,  3.,  2.,  1.])
plt.plot(x,y, label='poly')

t = np.arange(x.shape[0], dtype=float)
t /= t[-1]
nt = np.linspace(0, 1, 100)
x1 = make_interp_spline(t, x, nt)
y1 = make_interp_spline(t, y, nt)
plt.plot(x1, y1, label='range_spline')

t = np.zeros(x.shape)
t[1:] = np.sqrt((x[1:] - x[:-1])**2 + (y[1:] - y[:-1])**2)
t = np.cumsum(t)
t /= t[-1]
x2 = make_interp_spline(t, x, nt)
y2 = make_interp_spline(t, y, nt)
plt.plot(x2, y2, label='dist_spline')

plt.legend(loc='best')
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-2b54020d8fb1> in <module>
     12 t /= t[-1]
     13 nt = np.linspace(0, 1, 100)
---> 14 x1 = make_interp_spline(t, x, nt)
     15 y1 = make_interp_spline(t, y, nt)
     16 plt.plot(x1, y1, label='range_spline')

/usr/local/lib/python3.7/site-packages/scipy/interpolate/_bsplines.py in make_interp_spline(x, y, k, t, bc_type, axis, check_finite)
    751 
    752     # special-case k=0 right away
--> 753     if k == 0:
    754         if any(_ is not None for _ in (t, deriv_l, deriv_r)):
    755             raise ValueError("Too much info for k=0: t and bc_type can only "

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()