Compare SVM Kernels on Concentric Circles

Quick Overview

Fit linear, polynomial and RBF support vector machines on concentric-circle data with scikit-learn and compare their test accuracy. Tests why a straight boundary fails on radially separated classes and how kernels supply the features that separate them.

Compare SVM Kernels on Concentric Circles

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `compare_kernel_accuracies(X_train, y_train, X_test, y_test, C=1.0, gamma=1.0, degree=2, coef0=1.0)`. Fit three scikit-learn `SVC` models on the training data and return each model's test accuracy (fraction of test rows predicted correctly). Requirements: - `linear`: `SVC(kernel="linear", C=C)` - `poly`: `SVC(kernel="poly", C=C, gamma=gamma, degree=degree, coef0=coef0)` - `rbf`: `SVC(kernel="rbf", C=C, gamma=gamma)` (all other arguments at defaults) - Return a `dict` with keys in order `"linear"`, `"poly"`, `"rbf"` and Python `float` values in [0, 1]. - On `make_circles` data with a clear gap between the rings and little noise, linear should stay near chance while poly and rbf exceed 0.95. - Inputs may be lists or NumPy arrays. Labels are binary. Example: ```python X_train = [[-2.0], [-1.0], [1.0], [2.0]] y_train = [0, 0, 1, 1] X_test = [[-3.0], [3.0], [-1.5], [1.5]] y_test = [0, 1, 1, 1] compare_kernel_accuracies(X_train, y_train, X_test, y_test)["linear"] # -> 0.75 ([-1.5] is predicted 0 but labeled 1) ``` The linear model places its boundary at 0, halfway between the closest training points of the two classes, so three of the four test rows are classified correctly. ```hint What separates the rings Work out which single quantity tells the inner ring from the outer one, then check whether each kernel's implicit feature space can express it as a linear function. ``` ### What a Strong Answer Covers - The three models built with exactly the listed arguments and every other argument at its scikit-learn default. - Accuracy measured on the test set and returned as plain Python floats, with the keys in the order linear, poly, rbf. - Why no straight line separates concentric rings, and which implicit features let the degree-2 polynomial kernel do it. - How the distance-based RBF kernel produces a closed boundary around the inner ring. ### Follow-up Questions - Would a polynomial kernel with `degree=3` and `coef0=0` still separate the rings? - What happens to the RBF model's train and test accuracy as `gamma` grows very large, and how would you choose `C` and `gamma`? - Should the features be scaled before fitting these models?

Overview: Fit linear, polynomial and RBF support vector machines on concentric-circle data with scikit-learn and compare their test accuracy. Tests why a straight boundary fails on radially separated classes and how kernels supply the features that separate them.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement compare_kernel_accuracies(X_train, y_train, X_test, y_test, C=1.0, gamma=1.0, degree=2, coef0=1.0). Fit three scikit-learn SVC models on the training data and return each model's test accuracy (fraction of test rows predicted correctly).

Requirements:

  • linear : SVC(kernel="linear", C=C)
  • poly : SVC(kernel="poly", C=C, gamma=gamma, degree=degree, coef0=coef0)
  • rbf : SVC(kernel="rbf", C=C, gamma=gamma) (all other arguments at defaults)
  • Return a dict with keys in order "linear" , "poly" , "rbf" and Python float values in [0, 1].
  • On make_circles data with a clear gap between the rings and little noise, linear should stay near chance while poly and rbf exceed 0.95.
  • Inputs may be lists or NumPy arrays. Labels are binary.

Example:

X_train = [[-2.0], [-1.0], [1.0], [2.0]]
y_train = [0, 0, 1, 1]
X_test = [[-3.0], [3.0], [-1.5], [1.5]]
y_test = [0, 1, 1, 1]

compare_kernel_accuracies(X_train, y_train, X_test, y_test)["linear"]
# -> 0.75   ([-1.5] is predicted 0 but labeled 1)

The linear model places its boundary at 0, halfway between the closest training points of the two classes, so three of the four test rows are classified correctly.

What a Strong Answer Covers Guidance

  • The three models built with exactly the listed arguments and every other argument at its scikit-learn default.
  • Accuracy measured on the test set and returned as plain Python floats, with the keys in the order linear, poly, rbf.
  • Why no straight line separates concentric rings, and which implicit features let the degree-2 polynomial kernel do it.
  • How the distance-based RBF kernel produces a closed boundary around the inner ring.

Follow-up Questions Guidance

  • Would a polynomial kernel with degree=3 and coef0=0 still separate the rings?
  • What happens to the RBF model's train and test accuracy as gamma grows very large, and how would you choose C and gamma ?
  • Should the features be scaled before fitting these models?