# How to get polynomials coefficients?

See original GitHub issueHi,

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:
- Created 4 years ago
- Comments:12 (7 by maintainers)

#### Top 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 >#### Top Related Medium Post

No results found

#### Top Related StackOverflow Question

No results found

#### Troubleshoot Live Code

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

Start Free#### Top Related Reddit Thread

No results found

#### Top Related Hackernoon Post

No results found

#### Top Related Tweet

No results found

#### Top Related Dev.to Post

No results found

#### Top Related Hashnode Post

No results found

## Top GitHub Comments

Added example here https://github.com/cjekel/piecewise_linear_fit_py/commit/e57d1060b576f357a7cdbb9a60a5c554bff73d3e

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.