scikit-learn
SVM
support vectors
save model
load model

scikit learn SVM, how to save/load support vectors?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In scikit-learn, support vectors are stored on the fitted SVM estimator, but in most real workflows you do not save them separately. The practical default is to persist the whole trained model with joblib, because prediction also depends on kernel settings, dual coefficients, intercept terms, and any preprocessing applied before the SVM.

Train the Model and Inspect Support Vectors

After fitting an SVC, you can inspect the support vectors directly through the support_vectors_ attribute.

python
1from sklearn import svm, datasets
2
3X, y = datasets.load_iris(return_X_y=True)
4
5model = svm.SVC(kernel="rbf", gamma="scale")
6model.fit(X, y)
7
8print(model.support_vectors_.shape)
9print(model.n_support_)

support_vectors_ contains the feature rows chosen by the trained model. Scikit-learn also exposes related attributes such as support_ for the original indices and dual_coef_ for the learned coefficients that work together with the support vectors.

Save and Load the Whole Estimator

If your goal is to reuse the classifier later, save the full estimator.

python
1import joblib
2from sklearn import svm, datasets
3
4X, y = datasets.load_iris(return_X_y=True)
5model = svm.SVC(kernel="rbf", gamma="scale")
6model.fit(X, y)
7
8joblib.dump(model, "svc_model.joblib")
9
10loaded = joblib.load("svc_model.joblib")
11print(loaded.predict(X[:3]))

This is the right default because the loaded object keeps everything required for inference. If you used a pipeline with scaling or feature extraction, save the entire pipeline, not only the final SVC.

Save Support Vectors Separately Only When You Need Them

Sometimes you want support vectors for analysis, visualization, or export to another system. In that case, save the array explicitly.

python
1import numpy as np
2from sklearn import svm, datasets
3
4X, y = datasets.load_iris(return_X_y=True)
5model = svm.SVC(kernel="linear")
6model.fit(X, y)
7
8np.save("support_vectors.npy", model.support_vectors_)
9
10loaded_vectors = np.load("support_vectors.npy")
11print(loaded_vectors.shape)

This preserves the support vector matrix, but it does not preserve the full model. On its own, that file is not enough to reconstruct a working scikit-learn SVM for prediction.

Know What Else the Model Needs

An SVM prediction depends on more than just support vectors. At minimum, scikit-learn also needs:

  • the kernel type and kernel parameters
  • the dual coefficients
  • the intercept
  • class information
  • any feature preprocessing done before training

That is why saving only support_vectors_ is usually incomplete. For example, if you standardized the data before training, the raw support vectors are meaningful only together with the same scaler.

A good pattern is:

python
1from sklearn.pipeline import make_pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.svm import SVC
4import joblib
5
6pipeline = make_pipeline(StandardScaler(), SVC(kernel="rbf"))
7pipeline.fit(X, y)
8joblib.dump(pipeline, "svm_pipeline.joblib")

Now both preprocessing and classification are restored together.

When Separate Export Makes Sense

Saving only support vectors can still be useful when:

  • you want to inspect which samples shaped the margin
  • you are building a visualization
  • you are exporting learned artifacts to a custom non-scikit system
  • you are comparing model sparsity across training runs

In those cases, think of support vectors as model diagnostics, not as a complete portable model format.

Common Pitfalls

  • Saving only support_vectors_ and expecting to load a fully working classifier later.
  • Forgetting that preprocessing steps must be saved with the SVM model.
  • Using plain pickle casually when joblib is the more common choice for scikit-learn persistence.
  • Assuming support vector indices from support_ are more useful than the vectors themselves without checking the use case.
  • Loading a saved model in an incompatible environment with different library versions.

Summary

  • Inspect support vectors through support_vectors_ after fitting an SVM.
  • Save the whole estimator with joblib when you want to reuse the model for prediction.
  • Save support vectors separately only for analysis or custom export workflows.
  • Remember that an SVM also depends on coefficients, intercepts, kernel settings, and preprocessing.
  • For production reuse, persist the full pipeline rather than isolated internal arrays.

Course illustration
Course illustration

All Rights Reserved.