Fit a least-squares polynomial with NumPy and evaluate it at new points
Quick Overview
Implement a NumPy function that least-squares fits a polynomial of a given degree to paired data and evaluates it at new points. It tests how np.polyfit and np.polyval fit together, output shapes, and awareness of numerical conditioning and overfitting.
Fit a least-squares polynomial with NumPy and evaluate it at new points
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `fit_and_eval(x, y, deg, x_new)` that least-squares fits a degree-`deg` polynomial to `(x, y)` with `np.polyfit` and returns it evaluated at `x_new` with `np.polyval`, as an `np.ndarray` with the same shape as `x_new`.
For example:
```python
x = np.array([0., 1., 2., 3.])
y = np.array([1., 3., 7., 13.]) # y = x^2 + x + 1
x_new = np.array([0.5, 4.0])
fit_and_eval(x, y, 2, x_new)
# array([ 1.75, 21. ])
```
```hint Coefficient order
Check which power comes first in the array `np.polyfit` returns, and which order `np.polyval` reads its coefficients in.
```
### Constraints & Assumptions
- `1 ≤ deg < len(x)`
- `x` and `y` are 1-D arrays of equal length.
### What a Strong Answer Covers
- A working implementation that returns an array with the shape of `x_new`.
- How the coefficient order produced by `np.polyfit` relates to the order `np.polyval` expects.
- What the least-squares fit minimizes, and what happens when `deg` equals `len(x) - 1` or `x` has repeated values.
- Numerical conditioning for high degrees or large `x` values, and the risk of evaluating far outside the range of `x`.
### Follow-up Questions
- How would you also return a goodness-of-fit measure like R²?
- Why might `np.polynomial.Polynomial.fit` be preferred over `np.polyfit`?
- How would you choose `deg` for noisy data without overfitting?
Overview: Implement a NumPy function that least-squares fits a polynomial of a given degree to paired data and evaluates it at new points. It tests how np.polyfit and np.polyval fit together, output shapes, and awareness of numerical conditioning and overfitting.
Implement fit_and_eval(x, y, deg, x_new) that least-squares fits a degree-deg polynomial to (x, y) with np.polyfit and returns it evaluated at x_new with np.polyval, as an np.ndarray with the same shape as x_new.
For example:
x = np.array([0., 1., 2., 3.])
y = np.array([1., 3., 7., 13.]) # y = x^2 + x + 1
x_new = np.array([0.5, 4.0])
fit_and_eval(x, y, 2, x_new)
# array([ 1.75, 21. ])
Constraints & Assumptions
1 ≤ deg < len(x)
x
and
y
are 1-D arrays of equal length.
What a Strong Answer Covers Guidance
A working implementation that returns an array with the shape of
x_new
.
How the coefficient order produced by
np.polyfit
relates to the order
np.polyval
expects.
What the least-squares fit minimizes, and what happens when
deg
equals
len(x) - 1
or
x
has repeated values.
Numerical conditioning for high degrees or large
x
values, and the risk of evaluating far outside the range of
x
.
Follow-up Questions Guidance
How would you also return a goodness-of-fit measure like R²?
Why might
np.polynomial.Polynomial.fit
be preferred over
np.polyfit
?
How would you choose
deg
for noisy data without overfitting?