Scikit 学习 SVC 决策函数和预测 [英] Scikit Learn SVC decision_function and predict

查看:30
本文介绍了Scikit 学习 SVC 决策函数和预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解decision_function和predict之间的关系,它们是SVC的实例方法(http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html).到目前为止,我已经收集到决策函数返回类之间的成对分数.我的印象是 predict 选择最大化其成对分数的类,但我对此进行了测试并得到了不同的结果.这是我用来尝试理解两者之间关系的代码.首先,我生成了成对得分矩阵,然后打印出具有最大成对得分的类别,该类别与 clf.predict 预测的类别不同.

I'm trying to understand the relationship between decision_function and predict, which are instance methods of SVC (http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html). So far I've gathered that decision function returns pairwise scores between classes. I was under the impression that predict chooses the class that maximizes its pairwise score, but I tested this out and got different results. Here's the code I was using to try and understand the relationship between the two. First I generated the pairwise score matrix, and then I printed out the class that has maximal pairwise score which was different than the class predicted by clf.predict.

        result = clf.decision_function(vector)[0]
        counter = 0
        num_classes = len(clf.classes_)
        pairwise_scores = np.zeros((num_classes, num_classes))
        for r in xrange(num_classes):
            for j in xrange(r + 1, num_classes):
                pairwise_scores[r][j] = result[counter]
                pairwise_scores[j][r] = -result[counter]
                counter += 1

        index = np.argmax(pairwise_scores)
        class = index_star / num_classes
        print class
        print clf.predict(vector)[0]

有谁知道这些预测和决策函数之间的关系吗?

Does anyone know the relationship between these predict and decision_function?

推荐答案

我不完全理解您的代码,但让我们通过您引用的文档页面示例:

I don't fully understand your code, but let's go trough the example of the documentation page you referenced:

import numpy as np
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 2])
from sklearn.svm import SVC
clf = SVC()
clf.fit(X, y) 

现在让我们对样本应用决策函数和预测:

Now let's apply both the decision function and predict to the samples:

clf.decision_function(X)
clf.predict(X)

我们得到的输出是:

array([[-1.00052254],
       [-1.00006594],
       [ 1.00029424],
       [ 1.00029424]])
array([1, 1, 2, 2])

这很容易解释:desion 函数告诉我们我们在分类器生成的超平面的哪一侧(以及我们离它多远).根据这些信息,估计器然后用相应的标签标记示例.

And that is easy to interpret: The desion function tells us on which side of the hyperplane generated by the classifier we are (and how far we are away from it). Based on that information, the estimator then label the examples with the corresponding label.

这篇关于Scikit 学习 SVC 决策函数和预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆