【问题标题】:Why does the decision tree return different solutions for the exact same training data为什么决策树为完全相同的训练数据返回不同的解决方案
【发布时间】:2018-06-23 10:51:52
【问题描述】:

我正在尝试一个 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

【问题讨论】:

    标签: python scikit-learn decision-tree


    【解决方案1】:

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

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

    clf = DecisionTreeClassifier(random_state=0)
    

    【讨论】:

    • 您的建议非常好,泰勒的工作就像一个魅力,谢谢:)
    猜你喜欢
    • 2020-10-18
    • 2021-05-31
    • 2010-12-06
    • 2019-06-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多