Support vector machines in scikit-learn fit classification boundaries that can stay effective when feature counts are high or classes separate along nonlinear margins. Training an SVM through the estimator interface suits small or medium classification datasets where a held-out score and predicted labels can confirm the fitted model path.
The RBF kernel used by SVC is sensitive to feature scale, so the scaler should live in the same Pipeline as the classifier. The pipeline fits StandardScaler only on training rows and reuses the stored training statistics when held-out rows are transformed for prediction.
The built-in breast cancer dataset provides a deterministic binary smoke test without external files. A stratified split keeps both classes represented in the train and test sets, while the fitted SVC exposes its kernel and support-vector counts for a quick sanity check after training.
Steps to train a scikit-learn SVM classifier:
- Save the training script as train_svm.py.
- train_svm.py
from sklearn.datasets import load_breast_cancer from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC cancer = load_breast_cancer() X = cancer.data y = cancer.target target_names = cancer.target_names X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.25, random_state=42, stratify=y, ) model = make_pipeline( StandardScaler(), SVC(kernel="rbf", C=1.0, gamma="scale"), ) model.fit(X_train, y_train) predictions = model.predict(X_test) svc = model.named_steps["svc"] print(f"train rows: {X_train.shape[0]}") print(f"test rows: {X_test.shape[0]}") print(f"kernel: {svc.kernel}") print(f"support vectors per class: {svc.n_support_.tolist()}") print(f"held-out accuracy: {accuracy_score(y_test, predictions):.3f}") print(f"first predictions: {target_names[predictions[:5]].tolist()}") print(f"first actual labels: {target_names[y_test[:5]].tolist()}")
stratify=y preserves the class mix across the training and held-out rows, which makes the accuracy line easier to compare across reruns.
- Run the script from a Python environment that has scikit-learn installed.
$ python3 train_svm.py train rows: 426 test rows: 143 kernel: rbf support vectors per class: [50, 46] held-out accuracy: 0.979 first predictions: ['benign', 'malignant', 'benign', 'benign', 'malignant'] first actual labels: ['benign', 'malignant', 'benign', 'benign', 'malignant']
- Check the split counts before reading the accuracy.
The breast cancer dataset has 569 rows. A test_size=0.25 split leaves 426 training rows and 143 held-out rows when the same random seed is used.
- Confirm that the fitted classifier used the intended kernel.
kernel: rbf comes from the fitted SVC step inside the pipeline. The support-vector counts show how many training rows from each class remain in the decision function.
- Compare the predicted labels with the actual held-out labels.
Matching labels in the first five rows do not replace full evaluation, but they prove that predict() ran against held-out feature rows after scaling.
- Replace the built-in dataset with your own feature matrix and label vector after the smoke test works.
X = your_feature_matrix y = your_labels
Keep StandardScaler and SVC in the same pipeline so the training-set scaling rules are applied again during prediction.
- Remove the smoke-test script if it is not part of the project.
$ rm train_svm.py
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.