通过sklearn中的stdin设置决策函数的权重 [英] Set the weights of decision functions through stdin in Sklearn
问题描述
有没有一种方法可以在我的脚本中将系数输入到 SVC 的 clf
中,然后应用 clf.score()
或 clf.predict()
函数进一步测试?
Is there a method that I can input the coefficients to the clf
of SVC in my script, then apply clf.score()
or clf.predict()
function for further test?
目前我正在使用 joblib.dump(clf,'file.plk')
来保存经过训练的 clf
的所有信息.但这涉及磁盘写入/读取.如果我能定义一个带有两个数组的 clf
表示支持向量 (clf.support_vectors_
)、权重 (clf.coef_
code>/clf.dual_coef_
) 和偏差 (clf.intercept_
).
Currently I am using joblib.dump(clf,'file.plk')
to save all the information of a trained clf
. But this involves the disk writing/reading. It will be helpful for me if I can just define a clf
with two arrays representing the support vector (clf.support_vectors_
), weights (clf.coef_
/clf.dual_coef_
), and bias (clf.intercept_
) respectively.
推荐答案
这一行 调用来自 libsvm 的预测函数.它看起来像这样(但请看一下整个函数_dense_predict
):
This line calls the prediction function from libsvm. It looks like this (but please take a look at the whole function _dense_predict
):
libsvm.predict(
X, self.support_, self.support_vectors_, self.n_support_,
self.dual_coef_, self._intercept_,
self.probA_, self.probB_, svm_type=svm_type, kernel=kernel,
degree=self.degree, coef0=self.coef0, gamma=self._gamma,
cache_size=self.cache_size)
您可以使用此行并直接为其提供所有相关信息,从而获得原始预测.为此,您必须导入 libsvm from sklearn.svm import libsvm
.如果您的初始拟合分类器称为 svc
,那么您可以通过将所有 self
关键字替换为 svc
并保持价值.如果 svc._impl
给你 "c_svc"
,那么你设置 svm_type=0
.
You can use this line and give it all the relevant information directly and will obtain a raw prediction. In order to do this, you must import the libsvm from sklearn.svm import libsvm
. If your initial fitted classifier is called svc
, then you can obtain all the relevant information from it by replacing all the self
keywords with svc
and keeping the values. If svc._impl
gives you "c_svc"
, then you set svm_type=0
.
请注意,在 _dense_predict
函数的开头,您有 X = self._compute_kernel(X)
.如果你的数据是X
,那么你需要通过做K = svc._compute_kernel(X)
来转换它,并调用libsvm.predict
K
作为第一个参数的函数
Note that at the beginning of the _dense_predict
function you have X = self._compute_kernel(X)
. If your data is X
, then you need to transform it by doing K = svc._compute_kernel(X)
, and call the libsvm.predict
function with K
as the first argument
评分与这一切无关.看看 sklearn.metrics
,你会在那里找到例如accuracy_score
,这是 SVM 中的默认分数.
Scoring is independent from all this. Take a look at sklearn.metrics
, where you will find e.g. the accuracy_score
, which is the default score in SVM.
这当然是一种有点次优的做事方式,但在这种特定情况下,如果不可能(我没有非常努力地检查)设置系数,则进入代码并查看它的作用并提取相关部分肯定是一种选择.
This is of course a somewhat suboptimal way of doing things, but in this specific case, if is impossible (I didn't check very hard) to set coefficients, then going into the code and seeing what it does and extracting the relevant part is surely an option.
这篇关于通过sklearn中的stdin设置决策函数的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!