我有 (x,y) 数据集,它是连续且可微的。确切的函数形式是未知的。我想在某个时候扩展图表。我试过使用algopy/Adipy。问题是他们需要功能形式。
我附上了algopy的示例代码。
import numpy; from numpy import sin,cos
from algopy import UTPM
def f(x):
return sin(cos(x) + sin(x))
D = 100; P = 1
x = UTPM(numpy.zeros((D,P)))
x.data[0,0] = 0.3
x.data[1,0] = 1
y = f(x)
print('coefficients of y =', y.data[:,0])
其中 D 是多项式的阶数。
我尝试使用以下(x1 和 y1 是一维数组):
from scipy.interpolate import interp1d
f1 = interp1d(x1, y1, kind='cubic')
def f(x):
temp1=f1(x)
return np.float64(temp1)
但是,插值似乎不接受 UTPM 返回的 x 数据类型。
错误信息:
Traceback (most recent call last):
File "tay.py", line 26, in <module>
y = f(x)
File "tay.py", line 15, in f
temp1=f1(x)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/polyint.py", line 54, in __call__
y = self._evaluate(x)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 449, in _evaluate
y_new = self._call(self, x_new)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 441, in _call_spline
return spleval(self._spline, x_new)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 919, in spleval
res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv)
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'