Skip to main content
 首页 » 编程设计

python之机器学习中计算机决策的概率

2025年02月15日8powertoolsteam

措辞非常糟糕,但我想问的是,我如何显示使用分类算法预测某些事物的置信度百分比?我正在使用 Scikit-learn。

假设我正在尝试根据质地和重量来识别某物是苹果还是橙子:

#Features:  0 = "bumpy" 1 = "smooth" 
#Labels:    0 = apple 1 = orange 
features = [[140, 1], [130, 1], [150, 0], [170, 0]] 
labels = [0, 0, 1, 1] 
 
# We will be using a Decision Tree in this instance 
clf = tree.DecisionTreeClassifier() 
clf = clf.fit(features, labels) 
print(clf.predict([[160, 0]])) 

因此,根据模式预测 [160, 0],我们和计算机将预测这最有可能是橙子。 Scikit-learn 有没有一种方法可以预测计算机返回 1 或 0 的置信度?当我在特征向量中有更多参数时,这一点尤其重要。

请您参考如下方法:

是的。

只需使用 predict_proba(X)函数(而不是 predict())。

probability = clf.predict_proba([[160, 0]]) 

scikit 中的某些分类器有能力做到这一点,而其他分类器则没有。

在 DecisionTreeClassifier 的情况下,当询问给定类的概率时,模型将给出训练集中元素在特定“叶”中属于同一类的比例.

决策树中的一片叶子是一组条件(规则),表示沿着树向下的路径。

例如,对于示例 [0, 160],它们代表 [x1, x2],规则可能是

if x1 < 10: 
    if x2 > 150: 
        # in our training set of `n` examples, 100 fell under  
        # this rule set. 75 of them were apple, and 25 were orange - thus: 
        probability = [0.75, 0.25]  # P(apple) = .75, P(orange) = .25 

当然,在二元分类情况下(两个类)scikit 返回两者,但您实际上只需要一个或另一个,因为概率是互补的 (1 - .75 = .25)。

查看文档 here了解更多。

希望对您有所帮助。