zoukankan      html  css  js  c++  java
  • 【机器学习与R语言】7-回归树和模型树

    1.理解回归树和模型树

    决策树用于数值预测:

    • 回归树:基于到达叶节点的案例的平均值做出预测,没有使用线性回归的方法。
    • 模型树:在每个叶节点,根据到达该节点的案例建立多元线性回归模型。因此叶节点数目越多,一颗模型树越大,比同等回归树更难理解,但模型可能更精确。

    将回归加入到决策树:

    image.png

    分类决策树中,一致性(均匀性)由熵值来度量;数值决策树,则通过统计量(如方差、标准差或平均绝对偏差等)来度量。

    标准偏差减少SDR:一个常见的分割标准。

    原始值的标准差减去分割后加权标准差的减少量
    比如计算特征A和特征B的SDR分别为1.2和1.4,即特征B标准差减少得更多(更加均匀),所以首先使用特征B,这就是回归树。而模型树则需要再建立一个结果相对于特征A的线性回归模型,然后根据两个线性模型中的任何一个为新的案例做出预测。

    2.回归树和模型树应用示例

    葡萄酒质量评级

    1)收集数据

    白葡萄酒数据包含4898个葡萄酒案例的11种化学特征的信息(如酸性/含糖量/pH/密度等,还包含一列质量等级)。
    数据下载:

    链接: https://pan.baidu.com/s/1pN_PtZOYjOz2I-KJqSq6pw 提取码: 6swg

    2)探索和准备数据

    ## Step 2: Exploring and preparing the data ----
    wine <- read.csv("whitewines.csv")
    
    # examine the wine data
    str(wine)
    
    # the distribution of quality ratings
    hist(wine$quality)
    
    # summary statistics of the wine data
    summary(wine)
    
    wine_train <- wine[1:3750, ]
    wine_test <- wine[3751:4898, ]
    

    3)训练数据

    ## Step 3: Training a model on the data ----
    # regression tree using rpart
    library(rpart)
    m.rpart <- rpart(quality ~ ., data = wine_train)
    
    # get basic information about the tree
    m.rpart
    
    # get more detailed information about the tree
    summary(m.rpart)
    
    # use the rpart.plot package to create a visualization
    library(rpart.plot)
    
    # a basic decision tree diagram
    rpart.plot(m.rpart, digits = 3)
    
    # a few adjustments to the diagram
    rpart.plot(m.rpart, digits = 4, fallen.leaves = TRUE, type = 3, extra = 101)
    

    image.png
    alcohol是决策树种第一个使用的变量,所以它是葡萄酒质量种唯一重要的指标。

    4)评估模型

    ①预测值与真实值的范围以及相关性
    ②用平均绝对误差度量性能
    平均绝对误差MAE:考虑预测值离真实值有多远

    ## Step 4: Evaluate model performance ----
    
    # generate predictions for the testing dataset
    p.rpart <- predict(m.rpart, wine_test)
    
    # compare the distribution of predicted values vs. actual values
    summary(p.rpart)
    summary(wine_test$quality)
    
    # compare the correlation
    cor(p.rpart, wine_test$quality)
    
    # function to calculate the mean absolute error
    MAE <- function(actual, predicted) {
      mean(abs(actual - predicted))  
    }
    
    # mean absolute error between predicted and actual values
    MAE(p.rpart, wine_test$quality)
    
    # mean absolute error between actual values and mean value
    mean(wine_train$quality) # result = 5.87
    MAE(5.87, wine_test$quality)
    

    5)提高模型性能

    回归树在叶节点进行预测时只使用了一个单一的值,模型树可以通过回归树模型取代叶节点来改善回归树。

    • M5'算法(M5-prime):RWeka::M5P函数
    
    ## Step 5: Improving model performance ----
    # train a M5' Model Tree
    library(RWeka)
    m.m5p <- M5P(quality ~ ., data = wine_train)
    
    # display the tree
    m.m5p
    
    # get a summary of the model's performance
    summary(m.m5p)
    
    # generate predictions for the model
    p.m5p <- predict(m.m5p, wine_test)
    
    # summary statistics about the predictions
    summary(p.m5p)
    
    # correlation between the predicted and true values
    cor(p.m5p, wine_test$quality)
    
    # mean absolute error of predicted and true values
    # (uses a custom function defined above)
    MAE(wine_test$quality, p.m5p)
    

    image.png
    分割与回归树相似,但节点不是以一个数值预测终止,而是以一个线性模型终止(LM1,LM2...LM163)
    image.png
    模型树的预测范围、相关性、平均绝对误差比回归树都有所改善。

    PS:回归树和模型树的结果比较费解,这篇推文解读有点简单


    机器学习与R语言系列推文汇总:
    【机器学习与R语言】1-机器学习简介
    【机器学习与R语言】2-K近邻(kNN)
    【机器学习与R语言】3-朴素贝叶斯(NB)
    【机器学习与R语言】4-决策树
    【机器学习与R语言】5-规则学习
    【机器学习与R语言】6-线性回归
    【机器学习与R语言】7-回归树和模型树
    【机器学习与R语言】8-神经网络
    【机器学习与R语言】9-支持向量机
    【机器学习与R语言】10-关联规则
    【机器学习与R语言】11-Kmeans聚类
    【机器学习与R语言】12-如何评估模型的性能?
    【机器学习与R语言】13-如何提高模型的性能?

  • 相关阅读:
    数组的地址和vector数组的地址
    字节跳动 测试开发工程师 面经
    最短路径树
    SPFA
    树的直径
    树的重心
    CF1401D Maximum Distributed Tree
    期望简述
    CF723E One-Way Reform
    CF1409E Two Platforms
  • 原文地址:https://www.cnblogs.com/jessepeng/p/13611105.html
Copyright © 2011-2022 走看看