Skip to main content
 首页 » 编程设计

python之为什么决策树对完全相同的训练数据返回不同的解决方案

2025年05月04日14shangdawei

我正在尝试一个 ML 示例,它在大部分情况下都有效,但是当我连续运行代码时,python 开始吐出不同的预测结果,现在我是 ML 专家,但这看起来很奇怪?

# Example file from Google Developers: "Hello World - Machine Learning Recipes": YouTube: https://youtu.be/cKxRvEZd3Mw 
# Category: Supervised Learning                                                                                
# January 14, 2018                                                                                             
from sklearn import tree                                                                                       
 
# Declarations: Texture                                                                                         
bumpy = 0                                                                                                       
smooth = 1                                                                                                      
 
# Declarations: Labels                                                                                          
apple = 0                                                                                                       
orange = 1                                                                                                                                                                  
 
# Step(1): Collect training data                                                                                
# Features: [Weight, Texture]                                                                                   
features = [[140, smooth], [130, smooth], [150, bumpy], [170, bumpy]]                                           
 
# labels will be used as the index for the features                                                             
labels = [apple, apple, orange, orange]                                                                         
 
# Step(2): Train Classifier: Decision Tree                                                                      
# Use the decision tree object and then fit 'find' paterns in features and labels                               
clf = tree.DecisionTreeClassifier()                                                                             
clf = clf.fit(features, labels)                                                                                 
 
# Step(3): Make Predictions                                                                                     
# the prdict method will return the best fit from the decesion tree                                             
result = clf.predict([[150, bumpy], [130, smooth], [125.5, bumpy], [110, smooth]])                              
# result = clf.predict([[150, bumpy]])                                                                          
print("Step(3): Make Predictions: ")                                                                            
for x in result:                                                                                                
    if x == 0: 
    print("Apple")                                                                                         
        continue                                                                                               
    elif x == 1:                                                                                               
        print("Orange")                                                                                        
        continue                                                                                               
    print("Orange")                                                                                         

Click link to see vim and bash windows

请您参考如下方法:

(大多数?)决策树算法具有随机性元素,并且您的训练集非常小,这可能会夸大效果。随机性通常用于确定要使用多少/哪些样本,而在您的情况下,样本很少。

在创建 DecisionTreeClassifier 时尝试将 random_state 设置为某个固定整数.如果您想要可重复的测试结果,则每次都需要使用相同的“种子”值。他们在示例文档中使用随机种子 0:

clf = DecisionTreeClassifier(random_state=0)