我应该使用 numpy.polyfit 还是 numpy.polynomial.polyfit 或 numpy.polynomial.polynomial.Polynomial?
Should I use numpy.polyfit or numpy.polynomial.polyfit or numpy.polynomial.polynomial.Polynomial?
和
有什么区别
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html
和
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html
我应该在什么时候使用哪一个?
我检查了代码,但是都在他们的代码中使用了 numpy.linalg.linalg.lstsq,但在其他方面有所不同。
numpy.polyfit的文档也建议使用
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.Polynomial.fit.html
什么是正确的选择?
(奖金:当我想做的第一件事是适合我的数据时,我将如何使用 class?)
据我所知这里有很多遗留包袱,我们不应该使用 numpy.polyfit
,我们应该更喜欢 numpy.polynomial.polynomial.Polynomial.fit
.
考虑 comments on this github issue from 2016:
While the documentation is reasonably clear in noting that coefficients are returned [from numpy.polyfit
—ed.] 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
?
从之前的评论中可以明显看出,"historical accident" 是 MATLAB's polyfit
的行为,它首先接受高阶命令。早期的 numpy 保留了这个令人困惑的约定(它甚至可能继承自该项目的前身),但后来 numpy.polynomial.polynomial.polyfit
被实施为 Do It Right™。关键区别在于(与 MATLAB 不同)python 使用基于 0 的索引,在这种情况下,第 0 阶优先是非常自然的。根据这个约定,属性 项目 k
对应于术语 x**k
。
然后有一个较新的帐户 in another issue from this year 试图提供更连贯的画面。引用本期历史回忆:
History
(not necessarily in chronological order)
- 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.
- numpy, in an attempt to support fugitives from said environment, created the function
numpy.polyfit
which aped that design choice
- numpy implemented
numpy.ma.polyfit
for masked arrays, using numpy.polyfit
- 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
- 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
- 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
class。自 2016 年以来,文档有了很大改进,现在有从 numpy.polyfit
到 Polynomial
的指针,但仍然存在很多歧义。理想情况下,这两种 polyfit
方法都应该解释它们相对于另一种方法的情况,并向用户指出 Polynomial
class 是编写新代码的一种显而易见的方法。
和
有什么区别https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html
和
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html
我应该在什么时候使用哪一个?
我检查了代码,但是都在他们的代码中使用了 numpy.linalg.linalg.lstsq,但在其他方面有所不同。
numpy.polyfit的文档也建议使用
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.Polynomial.fit.html
什么是正确的选择?
(奖金:当我想做的第一件事是适合我的数据时,我将如何使用 class?)
据我所知这里有很多遗留包袱,我们不应该使用 numpy.polyfit
,我们应该更喜欢 numpy.polynomial.polynomial.Polynomial.fit
.
考虑 comments on this github issue from 2016:
While the documentation is reasonably clear in noting that coefficients are returned [from
numpy.polyfit
—ed.] 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 reasonnumpy.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 innumpy.polyfit
?
从之前的评论中可以明显看出,"historical accident" 是 MATLAB's polyfit
的行为,它首先接受高阶命令。早期的 numpy 保留了这个令人困惑的约定(它甚至可能继承自该项目的前身),但后来 numpy.polynomial.polynomial.polyfit
被实施为 Do It Right™。关键区别在于(与 MATLAB 不同)python 使用基于 0 的索引,在这种情况下,第 0 阶优先是非常自然的。根据这个约定,属性 项目 k
对应于术语 x**k
。
然后有一个较新的帐户 in another issue from this year 试图提供更连贯的画面。引用本期历史回忆:
History
(not necessarily in chronological order)
- 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.- numpy, in an attempt to support fugitives from said environment, created the function
numpy.polyfit
which aped that design choice- numpy implemented
numpy.ma.polyfit
for masked arrays, usingnumpy.polyfit
- 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- 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
- Powering on towards both API nirvana and worn-out keyboards, numpy introduced the
numpy.polynomial.polynomial.Polynomial
class, and documented innumpy.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
class。自 2016 年以来,文档有了很大改进,现在有从 numpy.polyfit
到 Polynomial
的指针,但仍然存在很多歧义。理想情况下,这两种 polyfit
方法都应该解释它们相对于另一种方法的情况,并向用户指出 Polynomial
class 是编写新代码的一种显而易见的方法。