question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polyfit is returning coefficients from low to high instead of high to low.

See original GitHub issue

Docs say that np.polyfit returns:

Polynomial coefficients, highest power first. If y was 2-D, the coefficients for k-th data set are in p[:,k].

However, at least for the linear case, it is actually returning coefficients lowest power first. Thus, for a fit y=mx+b, polyfit returns (b,m) and not (m,b) as the docs indicate.

y=np.linspace(0,10,10) 
x=y

plt.plot(x,y)
m,b=polyfit(x,y,1)  #Returns highest power first
print 'm is', m, 'b is', b

Output gives:

m is 0.0 b is 1.0

See that it’s reversed?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
charriscommented, Mar 4, 2013

There are two polyfits, are you by chance using the one in numpy.polynomial.polynomial.

0reactions
hugadamscommented, Mar 9, 2013

Yes thanks On Mar 8, 2013 7:29 PM, “Charles Harris” notifications@github.com wrote:

Seems settled.

— Reply to this email directly or view it on GitHubhttps://github.com/numpy/numpy/issues/3119#issuecomment-14654012 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polynomial curve fitting - MATLAB polyfit - MathWorks
This MATLAB function returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for...
Read more >
Why do numpy's polyfit and polyval expect polynomial ...
Both polyfit and polyval expect and return polynomial coefficients ordered from low to high degree. Also note that polyval expects parameters ...
Read more >
numpy.polyfit — NumPy v1.24 Manual
Computes spline fits. The coefficient matrix of the coefficients p is a Vandermonde matrix. polyfit issues a RankWarning when the least-squares fit is...
Read more >
Extremely poor polyfit, what am I doing wrong?
1) Once one move away from the range of data, the quality of a polyfit based extrapolation can deteriorate pretty quickly. Don't use...
Read more >
Week 6 Lecture 1
Polyfit and polyval ... This function takes the coefficients of a polynomial (remember, ... Our line is too low at the edges and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found