Support vector classifiers can separate classes even when the useful boundary is not a straight line. In scikit-learn, the radial basis function kernel lets SVC shape that boundary around nearby training examples, which suits small or medium labeled numeric datasets.
Feature scale affects the SVM objective when one measurement spans much larger values than another. Placing StandardScaler before SVC in one Pipeline fits the scaling statistics from the training partition and reuses them when the model receives held-out rows.
The built-in breast cancer dataset supplies 569 rows and two diagnosis classes without an external download. A fixed stratified split makes the smoke test repeatable; larger datasets with tens of thousands of rows may need LinearSVC or another classifier because the libsvm-backed SVC fit time grows at least quadratically with sample count.
from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC data = load_breast_cancer() X_train, X_test, y_train, y_test = train_test_split( data.data, data.target, test_size=0.25, stratify=data.target, random_state=42, )
stratify=data.target keeps both diagnosis classes represented in the training and held-out partitions.
model = make_pipeline( StandardScaler(), SVC(kernel="rbf", C=1.0, gamma="scale"), )
C=1.0 controls regularization, while gamma=“scale” derives the RBF kernel coefficient from the scaled training data. These starting values still need validation against the intended dataset.
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test) predictions = model.predict(X_test[:5]) svc = model.named_steps["svc"] assert accuracy >= 0.90 assert svc.support_.size > 0
The assertions stop the program if held-out accuracy falls below 0.90 or training produces no support vectors.
print(f"training rows: {X_train.shape[0]}") print(f"test rows: {X_test.shape[0]}") print(f"support vectors: {svc.support_.size}") print(f"held-out accuracy: {accuracy:.3f}") print(f"predicted labels: {data.target_names[predictions].tolist()}") print(f"actual labels: {data.target_names[y_test[:5]].tolist()}")
$ python3 train_svm.py training rows: 426 test rows: 143 support vectors: 96 held-out accuracy: 0.979 predicted labels: ['benign', 'malignant', 'benign', 'benign', 'malignant'] actual labels: ['benign', 'malignant', 'benign', 'benign', 'malignant']
The observed output comes from 143 rows excluded from fitting. The accuracy assertion and five predicted labels exercise the fitted scaling-and-classification pipeline on held-out data.