scipy.odr.
polynomial#
- scipy.odr.polynomial(order)[source]#
通用多項式模型的工廠函數。
- 參數:
- order整數或序列
如果是一個整數,它會變成要擬合的多項式的階數。如果是一個數字序列,那麼這些就是多項式中的顯式冪次。常數項(0 次冪)始終包含在內,所以不要包含 0。因此,polynomial(n) 等價於 polynomial(range(1, n+1))。
- 返回:
- polynomial模型實例
模型實例。
範例
我們可以使用正交距離迴歸 (ODR) 和多項式模型來擬合輸入數據
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from scipy import odr >>> x = np.linspace(0.0, 5.0) >>> y = np.sin(x) >>> poly_model = odr.polynomial(3) # using third order polynomial model >>> data = odr.Data(x, y) >>> odr_obj = odr.ODR(data, poly_model) >>> output = odr_obj.run() # running ODR fitting >>> poly = np.poly1d(output.beta[::-1]) >>> poly_y = poly(x) >>> plt.plot(x, y, label="input data") >>> plt.plot(x, poly_y, label="polynomial ODR") >>> plt.legend() >>> plt.show()