OLS Design Matrix With Intercept
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `def design_matrix(X: np.ndarray) -> np.ndarray` that prepends an intercept column of ones to `X`, a float64 2-D array of shape `(n, p)` with no ones column. Return a new `float64` array of shape `(n, p+1)`.
Requirements:
- Column 0 is all `1.0`, and columns 1 to p equal the columns of `X` in their original order.
- Do not mutate `X`.
Example:
```python
X = np.array([[2.0, 3.0],
[4.0, 5.0]])
design_matrix(X)
# array([[1., 2., 3.],
# [1., 4., 5.]])
```
```hint Watch the shape of the ones
`np.ones(n)` is 1-D. Check how your stacking function treats a 1-D array next to a 2-D one before you pass it in.
```
### Constraints & Assumptions
- `X` is a float64 2-D array of shape `(n, p)`.
- `X` does not already contain a column of ones.
### What a Strong Answer Covers
- A ones column with `n` rows placed to the left of `X`, giving shape `(n, p+1)` with the intercept at column 0.
- The original feature columns kept in order in columns 1 to p.
- A new float64 array returned, with a reason why the chosen operation never writes into or aliases `X`.
- Edge cases such as zero rows, zero feature columns, or integer input.
### Follow-up Questions
- Why does OLS need the intercept column, and what does the fit assume when you leave it out?
- What goes wrong if `X` already has a constant column, or a full set of one-hot dummies for a categorical feature?
- How would you build the design matrix for a large sparse feature matrix?
Overview: Write a NumPy function that prepends a column of ones to a feature matrix to build an OLS design matrix with an intercept. It tests array shapes and stacking, returning a new float64 array without mutating the input, and why the intercept column matters for linear regression.