如何存储训练有素的分类器?

时间:2016-07-30 16:33:14

标签: python django scipy nlp scikit-learn

训练分类器以在将URL输入其中时预测新闻类别

目前:对于每个输入,我训练分类器然后返回输出,因此我失去了训练有素的分类器

预期:一旦我训练了分类器,我应该能够在需要时从内存中调用此分类器

任何对此的亮光都将受到高度赞赏。

PS:NLP的业余爱好者

2 个答案:

答案 0 :(得分:1)

that你想要的是什么?

来自docs的例子:

>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf.fit(X, y)  
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)

>>> import pickle
>>> s = pickle.dumps(clf)    # <-- save/dump classifier to disk
>>> clf2 = pickle.loads(s)   # <-- read/load saved classifier from disk to a new variable
>>> clf2.predict(X[0:1])     # <-- use loaded (from disk) classifier
array([0])
>>> y[0]
0

答案 1 :(得分:0)

您可以使用 Pickle / cPickle 来转储/加载您的模型。

import pickle

#Some COde
model_path = "classifier.model"
if not os.path.exists(model_path):
    #Some Code
    classifier = # Some Classifer
    pickle.dump(classifier, open(model_path, "wb" ))
else:
    classifier = pickle.load(open(model_path, "rb"))

#Some Code

有关详细信息:Pickle / cPickle