使用 Numpy 获取多项式的系数

Get the coefficients of a polynomial with Numpy

我正在尝试通过以下方式获取 numpy.polynomial.polynomial.Polynomial 的系数 fit 方法:

import numpy.polynomial as poly

x = [1, 2, 3, 4, 5]
y = [16, 42.25, 81, 132.25, 196]

c = poly.Polynomial.fit(x, y, deg = 2)
print(c(5))
print(c)

这个小程序打印

196.00000000000006
poly([81. 90. 25.])

这是 c(5) 的正确值,但不适用于 2.257.56.25 的多项式系数。 我如何获得实际系数?

根据 documentation.fit() 方法 returns

A series that represents the least squares fit to the data and has the domain and window specified in the call. If the coefficients for the unscaled and unshifted basis polynomials are of interest, do new_series.convert().coef.

运行 c.convert().coef 在您的数据上产生:

array([2.25, 7.5 , 6.25])