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