Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何存储经过训练的分类器?_Python_Django_Scipy_Nlp_Scikit Learn - Fatal编程技术网

Python 如何存储经过训练的分类器?

Python 如何存储经过训练的分类器?,python,django,scipy,nlp,scikit-learn,Python,Django,Scipy,Nlp,Scikit Learn,当URL被输入到分类器中时,分类器被训练为预测新闻类别 当前:对于每个输入,我训练分类器,然后返回输出,因此我失去了训练过的分类器 预期:一旦我训练了分类器,我应该能够在需要时从内存调用该分类器 如能对此有所了解,我们将不胜感激。 PS:NLP的业余爱好者是你想要的吗 示例来自: 来自sklearn导入svm的>> >>>从sklearn导入数据集 >>>clf=svm.SVC() >>>iris=数据集。加载\u iris() >>>X,y=iris.data,iris.target >>>c

当URL被输入到分类器中时,分类器被训练为预测新闻类别

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

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

如能对此有所了解,我们将不胜感激。

PS:NLP的业余爱好者是你想要的吗

示例来自:

来自sklearn导入svm的
>>
>>>从sklearn导入数据集
>>>clf=svm.SVC()
>>>iris=数据集。加载\u iris()
>>>X,y=iris.data,iris.target
>>>clf.配合(X,y)
SVC(C=1.0,缓存大小=200,类权重=None,coef0=0.0,
决策函数形状=无,度=3,gamma='auto',内核='rbf',
最大值=1,概率=假,随机状态=无,收缩=真,
tol=0.001,详细=False)
>>>进口泡菜
>>>s=pickle.dumps(clf)#>>clf2=pickle.load(s)#>>clf2.predict(X[0:1])#>>y[0]
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

有关更多信息:

您是否建议我使用sklearn而不是nlp?尝试pickle您的
nip
分类器对象-很可能它会起作用。。。
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