Variance Inflation Factor Scores for Every Feature With statsmodels
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Write `def vif_scores(X: np.ndarray) -> np.ndarray` returning the Variance Inflation Factor for every feature. `X` has shape `(n_samples, n_features)`, dtype `float64`, with no constant column. The VIF for feature $j$ is ${1 / (1 - R^2_j)}$, where $R^2_j$ comes from regressing feature $j$ on all the other features.
Requirements:
- Build `Xc = statsmodels.api.add_constant(X)`, which prepends a column of ones.
- For each original feature `j` in `0..n_features-1`, compute `statsmodels.stats.outliers_influence.variance_inflation_factor(Xc, j + 1)`.
- Return an `np.ndarray` of shape `(n_features,)`, dtype `float64`, in original column order, with no entry for the constant.
Example:
```python
X.shape == (100, 3) # three predictors, no intercept column
out = vif_scores(X)
out.shape == (3,) # one VIF per original feature, in order
# e.g. array([1.02, 8.71, 8.65]) -> features 1 and 2 are collinear
```
```hint Check the auxiliary regression
Look at what `variance_inflation_factor` regresses the chosen column on, whether it adds an intercept for you, and what the column indices mean once the constant is in the matrix.
```
### Constraints & Assumptions
- `X` is a 2-D `float64` array of shape `(n_samples, n_features)` with more samples than features.
- `X` has no constant column; the function adds the intercept itself.
- The output has one `float64` entry per original feature, in column order.
### What a Strong Answer Covers
- Why the design matrix needs a constant column before the auxiliary regressions are run.
- Correct index mapping between original features and columns of the matrix with the constant prepended.
- Returning an array of the right shape and dtype with the constant's VIF excluded.
- How to read the scores, including what a very large or infinite VIF means.
### Follow-up Questions
- Why must the intercept be added before computing VIF?
- What would you do about features with a high VIF?
- How can you compute all the VIFs at once from the correlation matrix of the predictors, without running one regression per feature?
- Does the VIF of a predictor change when the model is a logistic regression instead of a linear one?
Overview: Write a function that returns the variance inflation factor for every feature of a design matrix using statsmodels. Tests how VIF is defined, why an intercept column must be added first, correct column indexing, and how to read high scores as a multicollinearity warning before fitting a logistic regression.