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.
Steps to train a scikit-learn SVM classifier:
- Create train_svm.py with the imports and stratified breast cancer train/test split.
- train_svm.py
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.
- Append the scaled RBF classifier definition after the split.
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.
- Append the fitting call below the pipeline definition.
model.fit(X_train, y_train)
- Append the held-out evaluation block after fitting.
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.
- Append the result reporting below the assertions.
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()}")
- Run the completed SVM training script from its containing directory.
$ 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.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.