简体   繁体   English

numpy.polyfit vs numpy.polynomial.polynomial.polyfit

[英]numpy.polyfit vs numpy.polynomial.polynomial.polyfit

Why do numpy.polyfit and numpy.polynomial.polynomial.polyfit produce different plots in the test below?为什么numpy.polyfitnumpy.polynomial.polynomial.polyfit在下面的测试中产生不同的图?

import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 50)
y = 5 * x + 10 + (np.random.random(len(x)) - 0.5) * 5

plt.scatter(x, y,marker='.', label='Data for regression')
plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)), 
         label='numpy.polyfit')
plt.plot(np.unique(x), np.poly1d(polyfit(x, y, 1))(np.unique(x)), 
         label='polynomial.polyfit')
plt.legend()
plt.show()

在此处输入图片说明

At first glance, the documentation seems to indicate they should give the same result -乍一看,文档似乎表明它们应该给出相同的结果 -

numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)

Least squares polynomial fit.最小二乘多项式拟合。

Fit a polynomial p(x) = p[0] * x**deg + ... + p[deg] of degree deg to points (x, y) .拟合多项式p(x) = p[0] * x**deg + ... + p[deg]deg到点(x, y) Returns a vector of coefficients p that minimises the squared error in the order deg, deg-1, … 0 .返回系数p的向量,该向量以deg, deg-1, ... 0的顺序最小化平方误差。

and

numpy.polynomial.polynomial.polyfit(x, y, deg, rcond=None, full=False, w=None)

Least-squares fit of a polynomial to data.多项式对数据的最小二乘拟合。

Return the coefficients of a polynomial of degree deg that is the least squares fit to the data values y given at points x.返回 deg 次多项式的系数,该多项式是对点 x 给出的数据值 y 的最小二乘拟合。 If y is 1-D the returned coefficients will also be 1-D.如果 y 是 1-D,则返回的系数也将是 1-D。 If y is 2-D multiple fits are done, one for each column of y, and the resulting coefficients are stored in the corresponding columns of a 2-D return.如果 y 是二维的,则进行多次拟合,y 的每一列都进行一次拟合,结果系数存储在二维返回的相应列中。 The fitted polynomial(s) are in the form拟合多项式的形式为

p(x) = c 0 + c 1 * x + ... + c n * x n p(x) = c 0 + c 1 * x + ... + c n * x n

But the difference is in the order of coefficients returned from the two methods, at least for the use case in question.但区别在于从两种方法返回的系数的顺序,至少对于所讨论的用例是这样。

  • numpy.polyfit returns the coefficients in descending order of degree, according to the generation equation numpy.polyfit根据生成方程按度数降序返回系数
    p(x) = c n * x n + c (n-1) * x (n-1) + ... + c 1 * x + c 0 p(x) = c n * x n + c (n-1) * x (n-1) + ... + c 1 * x + c 0
  • numpy.polynomial.polynomial.polyfit returns the coefficients in ascending order of degree, according to the generation equation numpy.polynomial.polynomial.polyfit根据生成方程按度数升序返回系数
    p(x) = c 0 + c 1 * x + ... + c (n-1) * x (n-1) + c n * x n p(x) = c 0 + c 1 * x + ... + c (n-1) * x (n-1) + c n * x n

though mathematically identical, those two equations are not the same in ndarray representation.尽管在数学上相同,但这两个方程在ndarray表示中并不相同。 This might be obfuscated by the use of different notations in the documentation.在文档中使用不同的符号可能会混淆这一点。 For demonstration, consider the following为了演示,请考虑以下内容

import numpy as np

x = np.linspace(0, 10, 50)
y = x**2 + 5 * x + 10

print(np.polyfit(x, y, 2))
print(np.polynomial.polynomial.polyfit(x, y, 2))
[ 1.  5. 10.]
[10.  5.  1.]

Both methods get the same result, but in opposite order, the former being what np.poly1d() expects,两种方法都得到相同的结果,但顺序相反,前者是np.poly1d()期望的,

print(np.poly1d(np.polyfit(x, y, 2)))
print(np.poly1d(np.polynomial.polynomial.polyfit(x, y, 2)))
   2
1 x + 5 x + 10
    2
10 x + 5 x + 1

and the latter being what the np.polynomial.polynomial.Polynomial() constructor expects.,后者是np.polynomial.polynomial.Polynomial()构造函数所期望的。,

print(np.polynomial.polynomial.Polynomial(np.polynomial.polynomial.polyfit(x, y, 2)))
print(np.polynomial.polynomial.Polynomial(np.polyfit(x, y, 2)))
poly([10.  5.  1.])  # 10 + 5 * x + 1 * x**2
poly([ 1.  5. 10.])  # 1 + 5 * x + 10 * x**2

Flipping the result from np.polynomial.polynomial.polyfit before passing it to poly1d() or using a np.polynomial.polynomial.Polynomial will produce the expected result: 翻转从结果np.polynomial.polynomial.polyfit它传递给前poly1d()或使用np.polynomial.polynomial.Polynomial将产生预期的结果:

匹配输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM