What are the coefficients returned by Polynomial Ridge Regression (or any regression)?

### Describe the issue linked to the documentation

I asked and answered a question about Regression in [Stack Overflow](https://stackoverflow.com/questions/79691953/ridge-polynomial-regression-how-to-get-parameters-for-equation-found).  Here is a summary and question.

I ran several regressions using pipeline and gridsearch.  The winning regression was polynomial ridge regression.  What I then wanted to do was extract the coefficients of the successful regression so I could pass them on for an implementation that uses just python (no libraries) and Java (no libraries).  That was not straightforward.

I eventually found the coefficients under `steps` after someone pointed that out.  Even the answers I got on Google indicated they were under the attribute `coef` but I couldn't find them though I thought I had read the docs sufficiently.

As explained at the link above, I expected coefficients for an equation: `a + bx + cx^2 + dx^3`.  If I looked at the coefficients under the attribute `coef_` I got: `[ 0.00000000e+00  9.17291774e-01 -4.25186367e-09  9.06355625e-18]`, from which I assumed that meant that `a=0`,` b=9.17291774e-01`, etc.  It turned out that was only partially true, `b-d` are correct but `a` is not.  `a` is actually the interecept which is another attribute `intercept_`.  At least, that is how I got things to work (code below for an example)

Question:  what is the first element in the coefficients from Polynomial Ridge Regression or have I completely misunderstood?

```
import pandas as pd
import warnings

# regression libs
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Ridge
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures

# useful initializations
warnings.filterwarnings('ignore')

p = [0, 10, -20, .30]

# Create fake data using the preceding coefficients with some noise
def regr_noise(x, p):
    mu = np.random.uniform(0,50E6)
    return (p[0] + p[1]*x + p[2]*x**2 + p[3]*x**3 + mu)

x = range(0,1000, 50)
df_fake = pd.DataFrame({'x':x})
df_fake['y'] = df_fake['x'].apply(lambda x: regr_noise(x, p))

# polynomial ridge regression
x = df_fake[['x']]
y = df_fake[['y']]

ridge_reg_poly = make_pipeline(PolynomialFeatures(degree=3),Ridge(alpha=0, solver="cholesky"))
temp = ridge_reg_poly.fit(x,y)
yp = temp.predict(x)

# get the coefficients and reproduce the results
p_found = ridge_reg_poly.steps[1][1].coef_
p_interc = ridge_reg_poly.steps[1][1].intercept_
print('coeff:',p_found,'\nintercep:' ,p_interc)

def regr_clean(x, p):
    return (p[0] + p[1]*x + p[2]*x**2 + p[3]*x**3 + p_interc)

y_fmcoef = regr_clean(x, p_found).rename(columns={'x':'y'})

Will get a column of zeros b/c same results as sklearn
yp - y_fmcoef.y
```

### Suggest a potential alternative/fix

Clear statements:

1. coefficients are produced and available
2. what the coefficients are, their relationship to an equation
3. how to use them in an equation

COMMENT:
So it seems the issue is you haven't actually read the API docs of the classes you're using?

`Ridge`'s docs clearly state:

```
    coef_ : ndarray of shape (n_features,) or (n_targets, n_features)
        Weight vector(s).

    intercept_ : float or ndarray of shape (n_targets,)
        Independent term in decision function. Set to 0.0 if
        ``fit_intercept = False``.
```

>  Even the answers I got on Google indicated they were under the attribute coef but I couldn't find them though I thought I had read the docs sufficiently.

Not sure if you mean a specific page, or the AI generated text given to you by Google. AI gives wrong results all the time, and you're responsible for validating what it says. I'm closing this issue as everything is very well documented already.

Labels: Documentation