0

对于以下数据,我正在遵循此问题中给出的建议:

import matplotlib.pyplot as plt
from numpy import array, linspace
from scipy.interpolate import spline

xdata = array([25, 36, 49])
ydata = array([145247, 363726, 789055])

xnew = linspace(xdata.min(),xdata.max(),300)

ysmooth = spline(xdata,ydata,xnew)

plt.plot(xnew,ysmooth)
plt.show()

虽然它适用于该问题中的数据,但由于某种原因,使用此数据,它会中断:

Traceback (most recent call last):
  File "test.py", line 526, in <module>
    ysmooth = spline(xdata,ydata,xnew)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 809, in spline
    return spleval(splmake(xk,yk,order=order,kind=kind,conds=conds),xnew)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 771, in splmake
    coefs = func(xk, yk, order, conds, B)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 500, in _find_smoothest
    p = np.dual.solve(Q,tmp)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/scipy/linalg/basic.py", line 70, in solve
    raise LinAlgError("singular matrix")
numpy.linalg.linalg.LinAlgError: singular matrix

我该如何解决?对于算法来说,这似乎是非常容易拟合的数据。

4

1 回答 1

2

你使用的是什么版本的 numpy 和 scipy?添加散点图后,您的代码适用于 numpy 1.6.0 和 scipy 0.9.0(将 linspace 导入从 scipy.interpolate 移动到 numpy 之后):

在此处输入图像描述

于 2011-08-14T02:17:44.243 回答