Understand the idea
RBF kernels support nonlinear boundaries. C controls the penalty/regularisation trade-off; gamma controls the locality of the kernel. Production searches C and keeps gamma="scale".
Illustrative data
| Column | Values |
|---|---|
| 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=)
example_model.fit(example_X_prepared, example_y)
example_model.n_support_ # A: 4, B: 8C=2, 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
'model__C'- Controls the penalty for training margin violations in the pipeline SVM.
C:0.5,1,2,10
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 withX_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 onX_train,y_train; reuse without refitting.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
length | width | label |
|---|---|---|
| 236.033 | -0.805568 | A |
| 581.297 | 0.728558 | A |
| -1511.27 | -1.00866 | A |
| 99.0248 | -0.24496 | A |
| -13.0141 | -0.660765 | A |
| 681.179 | 0.602475 | A |
| 51.1472 | 0.873157 | A |
| 362.131 | -0.665605 | A |
- Search C=.5,2,10 using five stratified folds.
answerHints
Think: C changes the margin/error trade-off and must be chosen before final testing.
Tools: GridSearchCV, StratifiedKFold and model__C.
Approach: Evaluate the three declared C values with training-only macro-F1 folds.
Explained solution
from sklearn.model_selection import GridSearchCV,StratifiedKFold
search=GridSearchCV(model,{'model__C':[.5,2,10]},cv=StratifiedKFold(5,shuffle=True,random_state=42),scoring='f1_macro').fit(X_train,y_train)
answer=search.cv_results_['mean_test_score']
The shared folds compare regularisation choices while the pipeline keeps scaling within each fit.