Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Save SVM in Python

I'm using Python to train an SVM, and I'd like to save the resulting model for use later. However, I get an error:

TypeError: can't pickle SVM objects

Is there another persistence method I should use? Is there something that I could add to the Python SVM module to make it pickle-able?

Minimal working code is below:

import numpy as np
import cv2
import pickle

points = np.array([[1.0, 2.1], [1, -1], [2, 3], [2, 1]], dtype=np.float32)
labels = np.array([0, 1, 0, 1], dtype=np.float32)

model = cv2.SVM(points, labels)
pickle.dump(model, open("save.pkl", 'w'))