简体   繁体   English

如何将现有系数加载到sklearn SVM分类器?

[英]How to load existing coefficients into sklearn SVM classifier?

I trained a SVM classifier using sklearn.svm.SVC , and stored the weights (coefficients). 我使用sklearn.svm.SVC训练了一个SVM分类器,并存储了权重(系数)。 Then I loaded them and tried to inject them into a new instance of sklearn.svm.SVC , but could not do it because it seems that the attribute coef_ is read only.. 然后我加载它们并尝试将它们注入到sklearn.svm.SVC的新实例中,但是无法执行它,因为似乎属性coef_是只读的。

from sklearn import svm
import pickle

modelSVM = svm.SVC(kernel='linear')
weights = pickle.load(open(weights_path, 'rb'))

modelSVM.coef_ = weights

I expect to have a model with the weights I loaded as new coefficients, but I get this message: 我希望有一个模型,我加载的权重作为新系数,但我收到此消息:

AttributeError: 'SVC' object has no attribute 'dual_coef_'

Which might be due to the fact that coef_ is not the only field needed by the classifier. 这可能是因为coef_不是分类器所需的唯一字段。 So I tried to train and then clone the classifier before injecting weights: 所以我尝试训练然后在注入权重之前克隆分类器:

modelSVM.fit(X, labels)
modelSVM = clone(modelSVM)
modelSVM.coef_ = weights

It gives the output: 它给出了输出:

"Exception has occurred: AttributeError
can't set attribute"

Instead of loading the weights, which is not possible as pointed out by @Mechanic in the comments. 而不是加载权重,这在评论中由@Mechanic指出是不可能的。 You can try to save the model and load the model back. 您可以尝试保存模型并加载模型。 Please refer to this link . 请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM