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

查看:50
本文介绍了numpy.polyfit vs numpy.polynomial.polynomial.polyfit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要

解决方案

乍一看,文档似乎表明他们应该给出相同的结果 -

<块引用>

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

最小二乘多项式拟合.

拟合一个多项式 p(x) = p[0] * x**deg + ... + p[deg] 度数 deg 到点 (x, y).返回一个系数向量 p,它以 deg, deg-1, ... 0 的顺序最小化平方误差.

<块引用>

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

多项式对数据的最小二乘拟合.

返回 deg 次多项式的系数,该多项式是对点 x 给出的数据值 y 的最小二乘拟合.如果 y 是 1-D,则返回的系数也将是 1-D.如果 y 是二维的,则进行多次拟合,y 的每一列都进行一次拟合,结果系数存储在二维返回的相应列中.拟合多项式的形式为

p(x) = c0 + c1 * x + ... + cn * xn

但区别在于从两种方法返回的系数的顺序,至少对于所讨论的用例是这样.

  • numpy.polyfit 根据生成方程以降序的度数返回系数
    p(x) = cn * xn + c(n-1) * x(n-1) + ... + c1 * x + c0
  • numpy.polynomial.polynomial.polyfit 根据生成方程以升序的度数返回系数
    p(x) = c0 + c1 * x + ... + c(n-1) * x(n-1) + cn * xn

虽然数学上相同,但这两个方程在 ndarray 表示中并不相同.在文档中使用不同的符号可能会混淆这一点.为了演示,请考虑以下内容

将 numpy 导入为 npx = np.linspace(0, 10, 50)y = x**2 + 5 * x + 10打印(np.polyfit(x,y,2))打印(np.polynomial.polynomial.polyfit(x,y,2))

[ 1. 5. 10.][10.5. 1.]

两种方法都得到相同的结果,但顺序相反,前者是

Why do numpy.polyfit and numpy.polynomial.polynomial.polyfit produce different plots in the test below?

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). Returns a vector of coefficients p that minimises the squared error in the order 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. If y is 1-D the returned coefficients will also be 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. The fitted polynomial(s) are in the form

p(x) = c0 + c1 * x + ... + cn * xn

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
    p(x) = cn * xn + c(n-1) * x(n-1) + ... + c1 * x + c0
  • numpy.polynomial.polynomial.polyfit returns the coefficients in ascending order of degree, according to the generation equation
    p(x) = c0 + c1 * x + ... + c(n-1) * x(n-1) + cn * xn

though mathematically identical, those two equations are not the same in ndarray representation. 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,

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.,

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:

这篇关于numpy.polyfit vs numpy.polynomial.polynomial.polyfit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆