Fit a single-feature linear regression with scikit-learn and return slope and intercept
Quick Overview
Implement a function that fits scikit-learn's LinearRegression on one 1-D predictor and returns the slope and intercept as plain Python floats. It tests reshaping a single feature into a 2-D matrix, reading the fitted coefficient and intercept attributes, and how the result relates to closed-form OLS.
Fit a single-feature linear regression with scikit-learn and return slope and intercept
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `def sklearn_simple_fit(x: np.ndarray, y: np.ndarray) -> tuple[float, float]` that fits OLS on a single 1-D predictor `x` of shape `(n,)` using `sklearn.linear_model.LinearRegression`. Return `(slope, intercept)` as plain Python floats, slope first.
Requirements:
- Reshape `x` to `(n, 1)` before calling `.fit(X, y)`.
- The slope is `coef_[0]` and the intercept is `intercept_`.
Example:
```python
x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([3.0, 5.0, 7.0, 9.0])
sklearn_simple_fit(x, y) # (2.0, 1.0)
```
```hint Shapes and types
Check what shape scikit-learn expects for the feature matrix, and what type each fitted attribute (`coef_`, `intercept_`) has after the fit.
```
### Constraints & Assumptions
- `x` and `y` are 1-D numeric arrays of the same length `n`.
- The model fits an intercept (the `LinearRegression` default).
### What a Strong Answer Covers
- Turning the 1-D predictor into a 2-D feature matrix with one column, and why scikit-learn needs that.
- Reading the slope from the `coef_` array and the intercept from `intercept_`.
- Returning plain Python floats in the required order.
- How the result relates to the closed-form OLS slope and intercept.
### Follow-up Questions
- What happens if you pass the 1-D `x` straight to `.fit()`?
- How would this generalize to multiple predictors?
- What does `LinearRegression` return when every `x` value is the same?
- How would you compute the same slope and intercept without scikit-learn, and confirm the two agree?
Overview: Implement a function that fits scikit-learn's LinearRegression on one 1-D predictor and returns the slope and intercept as plain Python floats. It tests reshaping a single feature into a 2-D matrix, reading the fitted coefficient and intercept attributes, and how the result relates to closed-form OLS.
Fit a single-feature linear regression with scikit-learn and return slope and intercept
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0
Implement def sklearn_simple_fit(x: np.ndarray, y: np.ndarray) -> tuple[float, float] that fits OLS on a single 1-D predictor x of shape (n,) using sklearn.linear_model.LinearRegression. Return (slope, intercept) as plain Python floats, slope first.
Requirements:
Reshape
x
to
(n, 1)
before calling
.fit(X, y)
.
The slope is
coef_[0]
and the intercept is
intercept_
.
Example:
x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([3.0, 5.0, 7.0, 9.0])
sklearn_simple_fit(x, y) # (2.0, 1.0)
Constraints & Assumptions
x
and
y
are 1-D numeric arrays of the same length
n
.
The model fits an intercept (the
LinearRegression
default).
What a Strong Answer Covers Guidance
Turning the 1-D predictor into a 2-D feature matrix with one column, and why scikit-learn needs that.
Reading the slope from the
coef_
array and the intercept from
intercept_
.
Returning plain Python floats in the required order.
How the result relates to the closed-form OLS slope and intercept.
Follow-up Questions Guidance
What happens if you pass the 1-D
x
straight to
.fit()
?
How would this generalize to multiple predictors?
What does
LinearRegression
return when every
x
value is the same?
How would you compute the same slope and intercept without scikit-learn, and confirm the two agree?