拟合多项式的numpy和scipy函数不包括像Matlab函数那样自动缩放输入的选项。
首先,这里是你如何能满足您的数据,而无需缩放:
In [39]: x_cord = [58.2986, 39.5842, 23.0044, 10.9427, 3.0465]
In [40]: y_cord = [0.9600, 0.9700, 0.9800, 0.9900, 1.0000]
In [41]: c = np.polyfit(x_cord, y_cord, 3)
In [42]: c
Out[42]:
array([ -1.91755884e-07, 2.43049234e-05, -1.52570960e-03,
1.00431483e+00])
In [43]: p = np.poly1d(c)
In [44]: p(16.574651718139650)
Out[44]: 0.98483061114799408
In [45]: xx = np.linspace(0, 60, 500)
In [46]: plot(xx, p(xx))
Out[46]: [<matplotlib.lines.Line2D at 0x110c8d0f0>]
In [47]: plot(x_cord, y_cord, 'o')
Out[47]: [<matplotlib.lines.Line2D at 0x10d6f8390>]
的numpy的计算agrees with Wolfram Alpha。
下面介绍如何获得非常接近实际的Matlab计算结果。
为了方便,将x_cord
从列表转换为numpy数组。
In [64]: x_cord = np.array(x_cord)
计算x_cord
的平均值和标准差。
In [65]: mu = np.mean(x_cord)
In [66]: std = np.std(x_cord, ddof=1)
呼叫np.polyfit()
,使用x_cord
的位移和缩放版本。
In [67]: cscaled = np.polyfit((x_cord - mu)/std, y_cord, 3)
这些值非常接近Matlab代码注释中显示的数组p
。
In [68]: cscaled
Out[68]: array([-0.00212068, 0.00436168, -0.01410409, 0.97708027])
创建一个可调用的对象poly1d
。
In [69]: pscaled = np.poly1d(cscaled)
输入到pscaled
必须被移动和使用mu
和std
缩放。
In [70]: pscaled((16.574651718139650 - mu)/std)
Out[70]: 0.98483061114799486
谢谢你这样详细的答案。这帮了我很多。 – Vants