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.

How to get polynomials coefficients?

See original GitHub issue

Hi,

Is there a way to get the polynomial coefficients in a similar way it was provided in numpy.polyfit?

I did the following as a simple example to illustrate what I was thinking:

import numpy as np
import matplotlib.pyplot as plt
import pwlf

# create the data
x = np.arange(10)
y = np.zeros(10)
y[:5] = 0 + 2 * x[:5]
y[5:] = 20 - 3 * x[5:]

degree = 1
segments = 2

my_pwlf = pwlf.PiecewiseLinFit(x, y, degree=degree, disp_res=True)
res = my_pwlf.fit(segments)

# predict
xHat = np.linspace(min(x), max(x), num=10000)
yHat = my_pwlf.predict(xHat)

# get polynomial coefficients
def get_coeffs(pwlf) -> np.ndarray:
    coefficients = np.array(list(zip(pwlf.slopes, pwlf.intercepts)))
    return coefficients

coeffs = get_coeffs(my_pwlf)
p1 = np.poly1d(coeffs[0])
p2 = np.poly1d(coeffs[1])

breaks = my_pwlf.fit_breaks[1]

# test polynomial
ytest = np.zeros(len(x))
for i in range(len(x)):
    if x[i] <= breaks:
        ytest[i] = p1(x[i])
    else:
        ytest[i] = p2(x[i])

plt.figure()
plt.plot(x, y, 'o', label='data')
plt.plot(xHat, yHat, '-', label='predict')
plt.plot(x, ytest, '--', label='fit test')
plt.legend()
plt.show()

Thank you

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
cjekelcommented, Aug 25, 2019

I don’t think (right now) I want to add sympy as a requirement to this library, but maybe I’ll change my mind in the future.

At the very least, I should add this as an example, since people have asked about equations before.

You bring up a good point, let me reopen this until something is added to the library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Finding coefficients of a polynomial
We need a table of n+1 values of the variables x and y in order to find the coefficients of an n th...
Read more >
Finding coefficient of polynomial?
My Try: ... =1+3(x+x2+x3)+3∗2/2((x+x2+x3)2+3∗2∗1/6(x+x2+x3)3….. The coefficient of x3 will be 10, it is multiplied by x9 outside, so ...
Read more >
Algebra Examples | Simplifying Polynomials
Simplifying Polynomials. Find the Degree, Leading Term, and Leading Coefficient ... The degree of a polynomial is the highest degree of its terms....
Read more >
To Find the Coefficients of a Polynomial
PTC Mathcad returns a vector containing the coefficients of the polynomial. · If all the exponents are positive, then the first element of...
Read more >
Coefficients of polynomial - MATLAB coeffs
Coefficients of polynomial, returned as a symbolic number, variable, expression, vector, matrix, or multidimensional array. If there is only one coefficient 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