RBF SVM Gamma Sweep with Underfit and Overfit Diagnostics

Quick Overview

Write a Python function that fits an RBF-kernel SVM for each value in a gamma grid and returns a typed pandas table of train accuracy, test accuracy, support vector count and underfit or overfit flags. It tests how gamma controls model complexity and careful handling of output dtypes.

RBF SVM Gamma Sweep with Underfit and Overfit Diagnostics

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `sweep_rbf_gamma(X_train, y_train, X_test, y_test, gammas, C=1.0, min_train_accuracy=0.8, max_gap=0.1)`. For each value in `gammas`, fit scikit-learn's `SVC(kernel="rbf", gamma=g, C=C)` on the training set and record its diagnostics. Return a `pandas.DataFrame` with one row per gamma, in input order. The returned frame must follow these rules: - Columns, in order: `gamma`, `train_accuracy`, `test_accuracy`, `n_support_vectors`, `underfit`, `overfit`. - Accuracies are the fraction of correct predictions (`float64` in [0, 1]). `gamma` is `float64`. - `n_support_vectors` is `len(model.support_)` as `int64`. - `underfit` (`bool`) is `train_accuracy < min_train_accuracy`. - `overfit` (`bool`) is `train_accuracy - test_accuracy > max_gap` (strict). Sample call: ```python from sklearn.datasets import make_moons X, y = make_moons(n_samples=200, noise=0.3, random_state=0) df = sweep_rbf_gamma(X[:140], y[:140], X[140:], y[140:], gammas=[0.001, 1.0, 1000.0]) # 3 rows: tiny gamma -> underfit=True, huge gamma -> overfit=True ``` ```hint Watch the column types pandas infers a dtype from whatever you hand it, and an empty sweep or a mix of Python and NumPy scalars can come out with the wrong type. Decide how each column gets its exact dtype. ``` ### Constraints & Assumptions - Labels can be any two classes, such as 0/1 or -1/1. - No feature scaling is required, and the fit involves no randomness. - `gammas` is a list of positive numbers. The output keeps its order, duplicates included. ### What a Strong Answer Covers - Fitting a fresh RBF `SVC` per gamma with the given `C`, on the training data only, in input order. - Computing train and test accuracy as the fraction correct and the support vector count from the fitted model. - Applying both flags with the exact strict comparisons from the spec. - Returning the six columns in order with the required dtypes, including when `gammas` is empty. - Explaining why small gamma tends to underfit and large gamma tends to overfit. ### Follow-up Questions - Why does the support vector count change with gamma, and why can it be high at both ends of the sweep? - How would you pick the best gamma without peeking at the test set? - How do `C` and gamma interact, and what would a two-dimensional sweep show? - Why does feature scaling matter for an RBF SVM, and how would you add it without leaking test data?

Overview: Write a Python function that fits an RBF-kernel SVM for each value in a gamma grid and returns a typed pandas table of train accuracy, test accuracy, support vector count and underfit or overfit flags. It tests how gamma controls model complexity and careful handling of output dtypes.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement sweep_rbf_gamma(X_train, y_train, X_test, y_test, gammas, C=1.0, min_train_accuracy=0.8, max_gap=0.1). For each value in gammas, fit scikit-learn's SVC(kernel="rbf", gamma=g, C=C) on the training set and record its diagnostics. Return a pandas.DataFrame with one row per gamma, in input order.

The returned frame must follow these rules:

  • Columns, in order: gamma , train_accuracy , test_accuracy , n_support_vectors , underfit , overfit .
  • Accuracies are the fraction of correct predictions ( float64 in [0, 1]). gamma is float64 .
  • n_support_vectors is len(model.support_) as int64 .
  • underfit ( bool ) is train_accuracy < min_train_accuracy .
  • overfit ( bool ) is train_accuracy - test_accuracy > max_gap (strict).

Sample call:

from sklearn.datasets import make_moons

X, y = make_moons(n_samples=200, noise=0.3, random_state=0)
df = sweep_rbf_gamma(X[:140], y[:140], X[140:], y[140:],
                     gammas=[0.001, 1.0, 1000.0])
# 3 rows: tiny gamma -> underfit=True, huge gamma -> overfit=True

Constraints & Assumptions

  • Labels can be any two classes, such as 0/1 or -1/1.
  • No feature scaling is required, and the fit involves no randomness.
  • gammas is a list of positive numbers. The output keeps its order, duplicates included.

What a Strong Answer Covers Guidance

  • Fitting a fresh RBF SVC per gamma with the given C , on the training data only, in input order.
  • Computing train and test accuracy as the fraction correct and the support vector count from the fitted model.
  • Applying both flags with the exact strict comparisons from the spec.
  • Returning the six columns in order with the required dtypes, including when gammas is empty.
  • Explaining why small gamma tends to underfit and large gamma tends to overfit.

Follow-up Questions Guidance

  • Why does the support vector count change with gamma, and why can it be high at both ends of the sweep?
  • How would you pick the best gamma without peeking at the test set?
  • How do C and gamma interact, and what would a two-dimensional sweep show?
  • Why does feature scaling matter for an RBF SVM, and how would you add it without leaking test data?