Feature Scaling Effect on RBF SVC
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `compare_feature_scaling(X_train, y_train, X_test, y_test, C=1.0, gamma="scale")`. Fit an `SVC(kernel="rbf", C=C, gamma=gamma)` on raw features and a `Pipeline` of `StandardScaler` followed by the same SVC. Return each model's test accuracy and support vector count.
Example:
```python
# feature 0 informative (scale ~2), feature 1 pure noise (scale ~1000)
res = compare_feature_scaling(X_tr, y_tr, X_te, y_te)
# {'raw_accuracy': ~0.5, 'scaled_accuracy': ~0.98,
# 'raw_n_support': large, 'scaled_n_support': small}
```
```hint Reaching the inner model
A fitted `Pipeline` exposes its steps by name, so the SVC inside it still has a `support_` attribute you can read.
```
### Constraints & Assumptions
- Return a dict with keys `raw_accuracy`, `scaled_accuracy` (Python `float`, fraction of exact label matches) and `raw_n_support`, `scaled_n_support` (Python `int`, total `len(support_)` across classes).
- Use default `SVC` and `StandardScaler` settings otherwise: results must match scikit-learn exactly.
- Inputs may be lists or arrays. Labels are binary and may be ints or strings.
- The scaler is fit on training data only.
### What a Strong Answer Covers
- A raw `SVC` and a `StandardScaler` plus `SVC` pipeline with the same `C` and `gamma`, with the scaler fit on the training split only.
- Test accuracy as the fraction of exact label matches, returned as a Python `float` and correct for string labels.
- Total support vector counts from `len(support_)`, returned as Python `int`, including the model inside the pipeline.
- Why the RBF kernel is sensitive to feature scale, and why the raw model lands near chance while keeping many support vectors.
### Follow-up Questions
- Why does the raw model need more support vectors?
- Would `gamma="scale"` alone fix the problem?
Overview: Implement a comparison between an RBF support vector classifier trained on raw features and the same model behind a StandardScaler, reporting test accuracy and support vector counts. It tests why distance-based kernels need per-feature scaling and how a pipeline keeps test statistics out of training.