机器学习与R之决策树C50算法(转)

决策树
经验熵是针对所有样本的分类结果而言
经验条件熵是针对每个特征里每个特征样本分类结果之特征样本比例和
基尼不纯度
简单地说就是从一个数据集中随机选取子项,度量其被错误分类到其他分组里的概率


决策树算法使用轴平行分割来表现具体一定的局限性
C5.0算法--可以处理数值型和缺失 只使用最重要的特征--使用的熵度量-可以自动修剪枝
划分数据集
set.seed(123) #设置随机种子
train_sample <- sample(1000, 900)#从1000里随机900个数值
credit_train <- credit[train_sample, ]
credit_test  <- credit[-train_sample, ]
library(C50)
credit_model <- C5.0(credit_train[-17], credit_train$default) #特征数据框-标签
C5.0(train,labers,trials = 1,costs = NULL) 
trials控制自动法循环次数多迭代效果更好 costs可选矩阵 与各类型错误项对应的成本-代价矩阵
summary(credit_model)#查看模型
credit_pred <- predict(credit_model, credit_test)#预测
predict(model,test,type="class")  type取class分类结果或者prob分类概率
单规则算法(1R算法)--单一规则直观,但大数据底下,对噪声预测不准
library(RWeka)
mushroom_1R <- OneR(type ~ ., data = mushrooms)
重复增量修建算法(RIPPER) 基于1R进一步提取规则
library(RWeka)

mushroom_JRip <- JRip(type ~ ., data = mushrooms)

 

 

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. credit <- read.csv("credit.csv")  
  2. str(credit)  
  3.   
  4. # look at two characteristics of the applicant  
  5. table(credit$checking_balance)  
  6. table(credit$savings_balance)  
  7.   
  8. # look at two characteristics of the loan  
  9. summary(credit$months_loan_duration)  
  10. summary(credit$amount)  
  11.   
  12. # look at the class variable  
  13. table(credit$default)  
  14.   
  15. # create a random sample for training and test data  
  16. # use set.seed to use the same random number sequence as the tutorial  
  17. set.seed(123)  
  18. #从1000里随机900个数值  
  19. train_sample <- sample(1000, 900)  
  20.   
  21. str(train_sample)  
  22.   
  23. # split the data frames切分数据集  
  24. credit_train <- credit[train_sample, ]  
  25. credit_test  <- credit[-train_sample, ]  
  26.   
  27. # check the proportion of class variable类别的比例  
  28. prop.table(table(credit_train$default))  
  29. prop.table(table(credit_test$default))  
  30.   
  31. ## Step 3: Training a model on the data ----  
  32. # build the simplest decision tree  
  33. library(C50)  
  34. credit_model <- C5.0(credit_train[-17], credit_train$default)  
  35.   
  36. # display simple facts about the tree  
  37. credit_model  
  38.   
  39. # display detailed information about the tree  
  40. summary(credit_model)  
  41.   
  42. ## Step 4: Evaluating model performance ----  
  43. # create a factor vector of predictions on test data  
  44. credit_pred <- predict(credit_model, credit_test)  
  45.   
  46. # cross tabulation of predicted versus actual classes  
  47. library(gmodels)  
  48. CrossTable(credit_test$default, credit_pred,  
  49.            prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
  50.            dnn = c('actual default', 'predicted default'))  
  51.   
  52. ## Step 5: Improving model performance ----  
  53.   
  54. ## Boosting the accuracy of decision trees  
  55. # boosted decision tree with 10 trials提高模型性能 利用boosting提升  
  56. credit_boost10 <- C5.0(credit_train[-17], credit_train$default,  
  57.                        trials = 10)  
  58. credit_boost10  
  59. summary(credit_boost10)  
  60.   
  61. credit_boost_pred10 <- predict(credit_boost10, credit_test)  
  62. CrossTable(credit_test$default, credit_boost_pred10,  
  63.            prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
  64.            dnn = c('actual default', 'predicted default'))  
  65.   
  66. ## Making some mistakes more costly than others  
  67.   
  68. # create dimensions for a cost matrix  
  69. matrix_dimensions <- list(c("no", "yes"), c("no", "yes"))  
  70. names(matrix_dimensions) <- c("predicted", "actual")  
  71. matrix_dimensions  
  72.   
  73. # build the matrix设置代价矩阵  
  74. error_cost <- matrix(c(0, 1, 4, 0), nrow = 2, dimnames = matrix_dimensions)  
  75. error_cost  
  76.   
  77. # apply the cost matrix to the tree  
  78. credit_cost <- C5.0(credit_train[-17], credit_train$default,  
  79.                           costs = error_cost)  
  80. credit_cost_pred <- predict(credit_cost, credit_test)  
  81.   
  82. CrossTable(credit_test$default, credit_cost_pred,  
  83.            prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
  84.            dnn = c('actual default', 'predicted default'))  
  85.   
  86. #### Part 2: Rule Learners -------------------  
  87.   
  88. ## Example: Identifying Poisonous Mushrooms ----  
  89. ## Step 2: Exploring and preparing the data ---- 自动因子转换--将字符标记为因子减少存储  
  90. mushrooms <- read.csv("mushrooms.csv", stringsAsFactors = TRUE)  
  91.   
  92. # examine the structure of the data frame  
  93. str(mushrooms)  
  94.   
  95. # drop the veil_type feature  
  96. mushrooms$veil_type <- NULL  
  97.   
  98. # examine the class distribution  
  99. table(mushrooms$type)  
  100.   
  101. ## Step 3: Training a model on the data ----  
  102. library(RWeka)  
  103.   
  104. # train OneR() on the data  
  105. mushroom_1R <- OneR(type ~ ., data = mushrooms)  
  106.   
  107. ## Step 4: Evaluating model performance ----  
  108. mushroom_1R  
  109. summary(mushroom_1R)  
  110.   
  111. ## Step 5: Improving model performance ----  
  112. mushroom_JRip <- JRip(type ~ ., data = mushrooms)  
  113. mushroom_JRip  
  114. summary(mushroom_JRip)  
  115.   
  116. # Rule Learner Using C5.0 Decision Trees (not in text)  
  117. library(C50)  
  118. mushroom_c5rules <- C5.0(type ~ odor + gill_size, data = mushrooms, rules = TRUE)  
  119. summary(mushroom_c5rules)  
 
 
 
 

 

posted @ 2016-07-25 10:52  payton数据之旅  阅读(2123)  评论(0)    收藏  举报