The models in sklearn are objects, using Pickle to save and restore the trained classify or cluster models. Do not us numpy.save and numpy.load to do this, it’s not working.

An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
import Pickle
from sklearn.cluster import Kmeans
# suppose we want to save this kmeans model
kmeans_results = KMeans(n_clusters=2).fit(X)
kmeans_results_file = "kmeans.pkl"

# saving results
with open(kmeans_results_file, 'wb') as f:
pickle.dump(kmeans_results, f)

# loading results
with open(kmeans_results_file, 'rb') as f:
kmeans_results = pickle.load(f)


Comments