提问人:user2954167 提问时间:8/24/2014 更新时间:8/24/2014 访问量:976
interpolate.splev 错误:“前三个参数 (x,y,w) 的长度必须相等”
interpolate.splev error: 'Lengths of the first three arguments (x,y,w) must be equal'
问:
我正在尝试通过以下方式使用 scipy.interpolate 进行最小二乘拟合:
from scipy import interpolate
xnew = np.arange(min(l_bins), max(l_bins))
list1=l_bins
list1.remove(l_bins[0])
list1.remove(l_bins[-1])
tck = interpolate.splrep(l_bins,l_hits,k=3,task=-1,t=list1)
fnew = interpolate.splev(xnew, tck, der=0)
plt.plot(xnew, fnew, 'b-')
当我运行代码时,出现以下错误:
TypeError: Lengths of the first three arguments (x,y,w) must be equal
如何解决此问题?
答:
1赞
Warren Weckesser
8/24/2014
#1
问题可能出在这里:
list1=l_bins
list1.remove(l_bins[0])
list1.remove(l_bins[-1])
当您说 时,指的是与 相同的对象。它不是副本。因此,当您使用 时,您也在修改 .下面是一个示例;请注意,就地修改也会修改list1=l_bins
list1
l_bins
list1
remove
l_bins
b
a
;
In [17]: a = [10, 20, 30]
In [18]: b = a
In [19]: b.remove(10)
In [20]: b
Out[20]: [20, 30]
In [21]: a
Out[21]: [20, 30]
要解决此问题,应为 .它看起来像是一个 Python 列表。在这种情况下,你可以说list1
l_bins
l_bins
list1 = l_bins[:]
但是,您似乎要从 中删除 的第一个和最后一个元素。在这种情况下,您可以替换它l_bins
list1
list1 = l_bins[:]
list1.remove(l_bins[0])
list1.remove(l_bins[-1])
跟
list1 = l_bins[1:-1]
评论
splrep