我应该用吗纽比.波利菲特或者numpy.polynomy.polyfit或者多项式多项式多项式.多项式?

2024-04-20 01:06:40 发布

您现在位置:Python中文网/ 问答频道 /正文

有什么区别

https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

以及

https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html

什么时候我应该用哪一个呢?在

我检查了代码,但是两者都使用纽比.利纳格.利纳格.lstsq在它们的代码中,但在其他方面不同。在

文件纽比.波利菲特也建议使用

https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.Polynomial.fit.html

正确的选择是什么?在

(好处:当我想做的第一件事就是适应我的数据时,我将如何使用该类?)在


Tags: 代码httpsorgnumpydocsdochtmlscipy
1条回答
网友
1楼 · 发布于 2024-04-20 01:06:40

据我所知,这里有很多遗留的包袱,我们不应该使用numpy.polyfit,而应该选择{a1}。在

考虑comments on this github issue from 2016

While the documentation is reasonably clear in noting that coefficients are returned [from numpy.polyfited.] with highest-order last, this is fairly easy to miss, and is inconsistent with, e.g. numpy.polynomial.polynomial.polyfit().

再过一会儿

Having the zero-degree coefficient first, as done in numpy.polynomial.polynomial.polyfit is definitely more logical. I was under the impression that the only reason numpy.polyfit deviates from this is historical accident, which of course is nearly impossible to correct now since many programmes may depend on this behaviour. Maybe the easiest solution would be to point people to the "preferred" solution in numpy.polyfit?

从先前的评论中可以明显看出,“历史事故”是指MATLAB's ^{}的行为,它首先需要高阶。早期的numpy保留了这个令人费解的约定(它甚至可能继承了项目的前身),但是后来实现了{}来正确地执行它™. 关键的区别在于(与MATLAB不同)python使用基于0的索引,在这种情况下,首先有零阶是非常自然的。在这个约定中,有一个漂亮的属性,即条目k对应于术语x**k。在

然后有一个新的帐户in another issue from this year试图给出一个更连贯的图片。引用该期文章中的历史回忆:

History

(not necessarily in chronological order)

  1. A certain JVM-based linear algebra package had a function, polyfit, for fitting polynomials which made some weird design choices, like returning the coefficients highest-degree first.
  2. numpy, in an attempt to support fugitives from said environment, created the function numpy.polyfit which aped that design choice
  3. numpy implemented numpy.ma.polyfit for masked arrays, using numpy.polyfit
  4. In an attempt to fix the mistakes of history, numpy created the function numpy.polynomial.polynomial.polyfit with almost exactly the same signature, but with a more sensible coefficient ordering, and quietly preferred that people use that instead
  5. People were confused by these two very similar functions (#7478); also the new function could not return a covariance matrix and it did not have a masked array counterpart
  6. Powering on towards both API nirvana and worn-out keyboards, numpy introduced the numpy.polynomial.polynomial.Polynomial class, and documented in numpy.polyfit that that was the preferred way of fitting polynomials, although that also had no masked implementation and also did not return a covariance matrix

开发人员对这两个问题的回答清楚地表明numpy.polyfit是技术债务,正如其文档所说,新代码应该使用Polynomial类。自2016年以来,文档已经有了很大的改进,现在有了从numpy.polyfit到{}的指针,但是仍然有很多模糊之处。理想情况下,polyfit方法都应该解释它们相对于另一个方法的情况,并向用户指出Polynomial类是编写新代码的一种明显方式。在

相关问题 更多 >