99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀機(jī)器學(xué)習(xí)與R之決策樹C50算法
機(jī)器學(xué)習(xí)與R之決策樹C50算法
2018-01-21
收藏

機(jī)器學(xué)習(xí)與R之決策樹C50算法

決策樹
經(jīng)驗(yàn)熵是針對(duì)所有樣本的分類結(jié)果而言
經(jīng)驗(yàn)條件熵是針對(duì)每個(gè)特征里每個(gè)特征樣本分類結(jié)果之特征樣本比例和
基尼不純度
簡(jiǎn)單地說就是從一個(gè)數(shù)據(jù)集中隨機(jī)選取子項(xiàng),度量其被錯(cuò)誤分類到其他分組里的概率

決策樹算法使用軸平行分割來表現(xiàn)具體一定的局限性
C5.0算法--可以處理數(shù)值型和缺失 只使用最重要的特征--使用的熵度量-可以自動(dòng)修剪枝
劃分?jǐn)?shù)據(jù)集
set.seed(123) #設(shè)置隨機(jī)種子
train_sample <- sample(1000, 900)#從1000里隨機(jī)900個(gè)數(shù)值
credit_train <- credit[train_sample, ]
credit_test  <- credit[-train_sample, ]
library(C50)
credit_model <- C5.0(credit_train[-17], credit_train$default) #特征數(shù)據(jù)框-標(biāo)簽
C5.0(train,labers,trials = 1,costs = NULL)
trials控制自動(dòng)法循環(huán)次數(shù)多迭代效果更好 costs可選矩陣 與各類型錯(cuò)誤項(xiàng)對(duì)應(yīng)的成本-代價(jià)矩陣
summary(credit_model)#查看模型
credit_pred <- predict(credit_model, credit_test)#預(yù)測(cè)
predict(model,test,type="class")  type取class分類結(jié)果或者prob分類概率
單規(guī)則算法(1R算法)--單一規(guī)則直觀,但大數(shù)據(jù)底下,對(duì)噪聲預(yù)測(cè)不準(zhǔn)
library(RWeka)
mushroom_1R <- OneR(type ~ ., data = mushrooms)
重復(fù)增量修建算法(RIPPER) 基于1R進(jìn)一步提取規(guī)則
library(RWeka)

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


[plain] view plain copy

    credit <- read.csv("credit.csv")  
    str(credit)  
      
    # look at two characteristics of the applicant  
    table(credit$checking_balance)  
    table(credit$savings_balance)  
      
    # look at two characteristics of the loan  
    summary(credit$months_loan_duration)  
    summary(credit$amount)  
      
    # look at the class variable  
    table(credit$default)  
      
    # create a random sample for training and test data  
    # use set.seed to use the same random number sequence as the tutorial  
    set.seed(123)  
    #從1000里隨機(jī)900個(gè)數(shù)值  
    train_sample <- sample(1000, 900)  
      
    str(train_sample)  
      
    # split the data frames切分?jǐn)?shù)據(jù)集  
    credit_train <- credit[train_sample, ]  
    credit_test  <- credit[-train_sample, ]  
      
    # check the proportion of class variable類別的比例  
    prop.table(table(credit_train$default))  
    prop.table(table(credit_test$default))  
      
    ## Step 3: Training a model on the data ----  
    # build the simplest decision tree  
    library(C50)  
    credit_model <- C5.0(credit_train[-17], credit_train$default)  
      
    # display simple facts about the tree  
    credit_model  
      
    # display detailed information about the tree  
    summary(credit_model)  
      
    ## Step 4: Evaluating model performance ----  
    # create a factor vector of predictions on test data  
    credit_pred <- predict(credit_model, credit_test)  
      
    # cross tabulation of predicted versus actual classes  
    library(gmodels)  
    CrossTable(credit_test$default, credit_pred,  
               prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
               dnn = c('actual default', 'predicted default'))  
      
    ## Step 5: Improving model performance ----  
      
    ## Boosting the accuracy of decision trees  
    # boosted decision tree with 10 trials提高模型性能 利用boosting提升  
    credit_boost10 <- C5.0(credit_train[-17], credit_train$default,  
                           trials = 10)  
    credit_boost10  
    summary(credit_boost10)  
      
    credit_boost_pred10 <- predict(credit_boost10, credit_test)  
    CrossTable(credit_test$default, credit_boost_pred10,  
               prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
               dnn = c('actual default', 'predicted default'))  
      
    ## Making some mistakes more costly than others  
      
    # create dimensions for a cost matrix  
    matrix_dimensions <- list(c("no", "yes"), c("no", "yes"))  
    names(matrix_dimensions) <- c("predicted", "actual")  
    matrix_dimensions  
      
    # build the matrix設(shè)置代價(jià)矩陣  
    error_cost <- matrix(c(0, 1, 4, 0), nrow = 2, dimnames = matrix_dimensions)  
    error_cost  
      
    # apply the cost matrix to the tree  
    credit_cost <- C5.0(credit_train[-17], credit_train$default,  
                              costs = error_cost)  
    credit_cost_pred <- predict(credit_cost, credit_test)  
      
    CrossTable(credit_test$default, credit_cost_pred,  
               prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,  
               dnn = c('actual default', 'predicted default'))  
      
    #### Part 2: Rule Learners -------------------  
      
    ## Example: Identifying Poisonous Mushrooms ----  
    ## Step 2: Exploring and preparing the data ---- 自動(dòng)因子轉(zhuǎn)換--將字符標(biāo)記為因子減少存儲(chǔ)  
    mushrooms <- read.csv("mushrooms.csv", stringsAsFactors = TRUE)  
      
    # examine the structure of the data frame  
    str(mushrooms)  
      
    # drop the veil_type feature  
    mushrooms$veil_type <- NULL  
      
    # examine the class distribution  
    table(mushrooms$type)  
      
    ## Step 3: Training a model on the data ----  
    library(RWeka)  
      
    # train OneR() on the data  
    mushroom_1R <- OneR(type ~ ., data = mushrooms)  
      
    ## Step 4: Evaluating model performance ----  
    mushroom_1R  
    summary(mushroom_1R)  
      
    ## Step 5: Improving model performance ----  
    mushroom_JRip <- JRip(type ~ ., data = mushrooms)  
    mushroom_JRip  
    summary(mushroom_JRip)  
      
    # Rule Learner Using C5.0 Decision Trees (not in text)  
    library(C50)  
    mushroom_c5rules <- C5.0(type ~ odor + gill_size, data = mushrooms, rules = TRUE)  
    summary(mushroom_c5rules)

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }