Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Margins and rules · ML-C10 · 25–40 MIN

Margins and support vectors

Interpret the separating boundary and supporting examples.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence
  4. ApplyBuild a guided full workflow

Understand the idea

SVM fits boundaries influenced by support vectors near margins. Production SVC uses an RBF kernel and does not enable probability estimation. Decision values are not probabilities. An RBF kernel allows a curved boundary in measurement space. This activity inspects a supplied fitted classifier; Apply builds and evaluates the complete recipe.

Illustrative data

Illustrative data
ColumnValues
Class A(0, 0); (0.5, 0); (-0.5, 0); (0, 0.5); (0, -0.5)
Class B(1.5, 0); (-1.5, 0); (0, 1.5); (0, -1.5); (1, 1); (-1, -1); (1, -1); (-1, 1)

Click the highlighted code to change the result.

example_model = SVC(kernel="rbf", C=, gamma=1)
example_model.fit(example_X_prepared, example_y)
example_model.n_support_  # A: 4, B: 8
RBF kernel: C=1, gamma=1. The curve marks decision_function=0; rings identify support vectors. Negative scores predict A (teal region); positive scores predict B (coral region). Larger C penalises training margin violations more strongly. Choose settings using training validation; these scores are not probabilities.○ A □ B; ring = fitted support vector-2-20022prepared feature 1feature 2C=1 · gamma=1 · support A=4, B=8

C=1, gamma=1: support A=4, B=8. The zero contour separates predicted regions. Rings identify the fitted support vectors; decision scores are not probabilities.

What the syntax does

SVC(kernel='rbf', C=1.0, gamma='scale')
  • Creates a support vector classifier.
  • C: 0.5, 1, 2, 10
  • gamma: "scale", "auto", 0.1, 1, 10
  • kernel: "linear", "rbf", "poly", "sigmoid", "precomputed", kernel_function
  • kernel accepts named choices such as linear and rbf (the default).
  • C must be positive; larger values penalise margin violations more strongly. gamma=scale uses fitted feature variation; a positive numeric gamma sets locality explicitly.
  • Keep scaling and parameter selection within training folds.
model.decision_function(X_test)
  • Returns signed margin-based decision values; these are not probabilities.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['length', 'width']]
y
Target series, aligned with X.
y = df.label
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
model
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CLASS180 · first 8 rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A
Your task
  1. Use the supplied fitted scaled SVM to predict held-away labels.
answer
Hints

Think: The supplied fitted pipeline already contains the scale and margin model.

Tools: Pipeline.predict.

Approach: Pass the held-away features through the existing fitted pipeline.

Explained solution
answer=model.predict(X_test)

Prediction applies the learned preparation and SVM decision rule without refitting on evaluation rows.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.