Mad*_*May 4 python classification machine-learning scikit-learn
我目前正在使用sklearn的Ridge分类器,我正在寻找使用sklearn和其他库中的分类器来整合这个分类器.为了做到这一点,理想的是提取给定输入属于类列表中的每个类的概率.目前,我正在使用model.decision_function(x)的输出来压缩类,但这会返回超平面的距离,而不是简单的概率.这些距离值从大约-1到大约1不等.
distances = dict(zip(clf.classes_, clf.decision_function(x)[0]))
Run Code Online (Sandbox Code Playgroud)
如何将这些距离转换为更具体的概率(一系列总和为1的正值)?我正在寻找类似于clf.predict_proba()sklearn中的SVC实现的东西.
稍微留意一下的源代码predict显示,decision_function实际的类概率,即实际上是在分对数变换,如果decision funciton是f,那么这个类的概率class 1是exp(f) / (1 + exp(f)).这转换为以下检查sklearn源:
scores = self.decision_function(X)
if len(scores.shape) == 1:
indices = (scores > 0).astype(np.int)
else:
indices = scores.argmax(axis=1)
return self.classes_[indices]
Run Code Online (Sandbox Code Playgroud)
如果您观察此检查,它会告诉您如果决策函数大于零,则预测第1类,否则预测第0类 - 经典的logit方法.
因此,您必须将决策功能转换为:
d = clf.decision_function(x)[0]
probs = numpy.exp(d) / (1 + numpy.exp(d))
Run Code Online (Sandbox Code Playgroud)
然后采取适当的zip等
进一步的探索导致使用softmax功能.
d = clf.decision_function(x)[0]
probs = np.exp(d) / np.sum(np.exp(d))
Run Code Online (Sandbox Code Playgroud)
这保证了0-1有界分布总和为1.