TF-IDF LinearSVC Pipeline with L1 Term Pruning
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement four helpers for binary text classification with scikit-learn: balanced class weights, a TF-IDF + `LinearSVC` pipeline, L1-based vocabulary pruning, and extraction of the most positive and negative weighted terms.
The helpers must meet these requirements:
- `balanced_class_weights(y)`: dict of native label → `n_samples / (n_classes * count)` as `float`.
- `build_pipeline(C)`: unfitted `Pipeline` with steps `"tfidf"` (default `TfidfVectorizer`) and `"svm"` (`LinearSVC(C=C, class_weight="balanced", penalty="l2", loss="squared_hinge", dual=True, tol=1e-6, max_iter=20000, random_state=0)`).
- `l1_prune_terms(texts, y, C, tol)`: fit default TF-IDF, then `LinearSVC` with `penalty="l1", dual=False` (other settings as above). Return terms with `|coef| > tol` as a `list[str]` in vocabulary order.
- `top_weighted_terms(feature_names, coef, k)`: `coef` may be 1-D or `(1, n)`. Return up to `k` strictly positive terms (largest first) and up to `k` strictly negative terms (most negative first). Ties break alphabetically, zero weights are excluded.
Example:
```python
names = ["e", "b", "a", "d", "c"]
coef = [[0.5, -0.2, 0.5, -0.9, 0.0]]
top_weighted_terms(names, coef, 5)
# (['a', 'e'], ['d', 'b'])
balanced_class_weights([0, 0, 0, 1])
# {0: 0.6666666666666666, 1: 2.0}
```
```hint Equal total weight
Multiply each class's weight by its count. What do you get, and what does that say about what `class_weight="balanced"` is equalizing?
```
```hint Sort keys with ties
A sort key can be a tuple. Choose its first element so that the strongest weight on each side comes first, and let the term name settle ties.
```
### Constraints & Assumptions
- Labels are binary, so a fitted `LinearSVC` has a `coef_` of shape (1, n_features).
- `coef` in `top_weighted_terms` has one weight per entry of `feature_names`.
- `k` is a non-negative integer. The `tol` argument of `l1_prune_terms` is a threshold on absolute weight, separate from the solver's `tol=1e-6`.
### What a Strong Answer Covers
- Balanced weights computed by formula with native Python keys and `float` values, and how they match what `class_weight="balanced"` does inside `LinearSVC`.
- The exact unfitted pipeline, with the named steps and solver settings.
- Why the L1 model is solved in the primal, and how its zero weights define the pruned vocabulary in vocabulary order.
- Deterministic output from `top_weighted_terms`: shape handling, strict sign filters, tie-breaking, and shorter lists when fewer than `k` terms qualify.
- Which class a positive weight points to in a binary `LinearSVC`.
### Follow-up Questions
- How would you choose `C` for the L1 model without leaking test data?
- How would you extend `top_weighted_terms` to multiclass?
- Why use `dual=False` for the L1 penalty?
- How would you put the L1 pruning step inside the main pipeline so it is refit within cross-validation?
Overview: Implement scikit-learn helpers for binary text classification: balanced class weights, a TF-IDF and LinearSVC pipeline, L1-based vocabulary pruning, and ranking of the most positive and negative weighted terms. It tests sparse linear models, liblinear solver settings and deterministic tie-breaking.