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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言實(shí)現(xiàn)決策樹分析
R語言實(shí)現(xiàn)決策樹分析
2018-06-13
收藏

R語言實(shí)現(xiàn)決策樹分析

決策樹是附加概率結(jié)果的一個(gè)樹狀的決策圖,是直觀的運(yùn)用統(tǒng)計(jì)概率分析的圖法。機(jī)器學(xué)習(xí)決策樹是一個(gè)預(yù)測模型,它表示對象屬性和對象值之間的一種映射,樹中的每一個(gè)節(jié)點(diǎn)表示對象屬性的判斷條件,其分支表示符合節(jié)點(diǎn)條件的對象。樹的葉子節(jié)點(diǎn)表示對象所屬的預(yù)測結(jié)果。

    這一節(jié)學(xué)習(xí)使用包party里面的函數(shù)ctree()為數(shù)據(jù)集iris建立一個(gè)決策樹。屬性Sepal.Length(萼片長度)、Sepal.Width(萼片寬度)、Petal.Length(花瓣長度)以及Petal.Width(花瓣寬度)被用來預(yù)測鳶尾花的Species(種類)。在這個(gè)包里面,函數(shù)ctree()建立了一個(gè)決策樹,predict()預(yù)測另外一個(gè)數(shù)據(jù)集。

    在建立模型之前,iris(鳶尾花)數(shù)據(jù)集被分為兩個(gè)子集:訓(xùn)練集(70%)和測試集(30%)。使用隨機(jī)種子設(shè)置固定的隨機(jī)數(shù),可以使得隨機(jī)選取的數(shù)據(jù)是可重復(fù)利用的。

#iris的決策樹分析(二)
install.packages("party")
library("party")             #導(dǎo)入數(shù)據(jù)包

str(iris)#集中展示數(shù)據(jù)文件的結(jié)構(gòu)

#構(gòu)建決策樹
iris_ctree<-ctree(Species ~ Sepal.Length 
                  + Sepal.Width 
                  + Petal.Length 
                  + Petal.Width,
                  data=iris)
#查看決策樹信息
print(iris_ctree)

#構(gòu)建完的決策樹
plot(iris_ctree)





plot(iris_ctree, type="simple")




iris的決策樹分析(二)

install.packages("party")
library("party")             #導(dǎo)入數(shù)據(jù)包
str(iris)#集中展示數(shù)據(jù)文件的結(jié)構(gòu)
#構(gòu)建決策樹
iris_ctree<-ctree(Species ~ Sepal.Length 
                  + Sepal.Width 
                  + Petal.Length 
                  + Petal.Width,
                  data=iris)
#查看決策樹信息
print(iris_ctree)
#構(gòu)建完的決策樹
plot(iris_ctree)
#決策樹案例擬合圖
plot(iris_ctree, type="simple")


---------------------------------------------------------------------------------------------

決策樹(decision tree)            決策樹是使用類似于一棵樹的結(jié)構(gòu)來表示類的劃分,樹的構(gòu)建可以看成是變量(屬性)選擇的過程,內(nèi)部節(jié)點(diǎn)表示樹選擇那幾個(gè)變量(屬性)作為劃分,每棵樹的葉節(jié)點(diǎn)表示為一個(gè)類的標(biāo)號(hào),樹的最頂層為根節(jié)點(diǎn)。
           ID3, C4.5和CART均采用貪心算法,自頂向下遞歸的分治方式構(gòu)造,從訓(xùn)練數(shù)據(jù)集和他們相關(guān)聯(lián)的類標(biāo)號(hào)開始構(gòu)造樹。隨著樹的構(gòu)造,訓(xùn)練數(shù)據(jù)集遞歸的劃分成較小的子集。
            算法當(dāng)中三個(gè)重要的參數(shù):D 訓(xùn)練數(shù)據(jù)集和類標(biāo)號(hào) ,Attitude_list 描述觀測的變量(屬性)列表,Attribute_selection_list變量(屬性)選擇度量,確定一個(gè)分裂準(zhǔn)側(cè)(splitting criterion),這種分裂準(zhǔn)則選擇哪些變量(屬性)為按類“最好的”進(jìn)行劃分,也確定分枝選擇哪些觀測的為“最好的劃分”。即分裂準(zhǔn)則可以確定分裂 變量(屬性),也可以確定分裂點(diǎn)(splitting--point),使最后分裂的準(zhǔn)則盡可能的“純”,“純”表示所有觀測都屬于同一類,常用的有信息 增益,增益率,Gini指標(biāo)作為度量。
            信息增益:ID3使用信息增益,假定D中類標(biāo)號(hào)變量(屬性)具有m個(gè)值,定義m個(gè)不同的類C(i)(i=1,2---m),C(i,d)是D中C(i)類的元組的集合,|D|和|C(i,D)|分別是D,C(i,D)中的個(gè)數(shù)。

                 決策樹在構(gòu)造過程中,由于數(shù)據(jù)中含有噪聲和離群點(diǎn)等異常數(shù)據(jù),訓(xùn)練出來的樹的分枝會(huì)過分?jǐn)M合數(shù)據(jù),處理過分?jǐn)M合的辦法是對樹進(jìn)行剪枝,剪枝后的樹更 小,復(fù)雜度更低,而且容易理解,常用的剪枝辦法有,先剪枝和后剪枝。                 R語言中實(shí)現(xiàn)決策樹的包rpart;     1,生成樹:rpart()函數(shù) raprt(formular,data,weight,subset,na.action=na.rpart,method,model=FALSE,x=FALSE,y=TRUE,parms,control,cost,...)         fomula :模型格式形如outcome~predictor1+predictor2+predictor3+ect。         data     :數(shù)據(jù)。         na.action:缺失數(shù)據(jù)的處理辦法,默認(rèn)為刪除因變量缺失的觀測而保留自變量缺失的觀測。         method:樹的末端數(shù)據(jù)類型選擇相應(yīng)的變量分割方法,連續(xù)性method=“anova”,離散型使用method=“class”,,計(jì)數(shù)型 method=“poisson”,生存分析型method=“exp”。          parms:設(shè)置三個(gè)參數(shù),先驗(yàn)概率,損失矩陣,分類矩陣的度量方法。          control:控制每個(gè)節(jié)點(diǎn)上的最小樣本量,交叉驗(yàn)證的次數(shù),復(fù)雜性參量:cp:complexity pamemeter。      2,剪枝使用          prune(tree,cp,....)           tree常是rpart()的結(jié)果對象,cp 復(fù)雜性參量       3 顯示結(jié)果的語句printcp(fit)顯示復(fù)雜性表plotcp(fit)畫交叉驗(yàn)證結(jié)果圖rsq.rpart(fit)R-squared 和 relative error for different splits (2 plots). labels are only appropriate for  "anova" method.print(fit)打印結(jié)果summary(fit)基本信息plot(fit)決策樹text(fit)給樹添加標(biāo)簽post(fit,file=)保存結(jié)果ps,pdf,等格式  # Classification Tree with rpart
library(rpart)

# grow tree
fit <- rpart(Kyphosis ~ Age + Number + Start,
method="class", data=kyphosis)

printcp(fit) # display the results
plotcp(fit) # visualize cross-validation results
summary(fit) # detailed summary of splits

# plot tree
plot(fit, uniform=TRUE,
main="Classification Tree for Kyphosis")
text(fit, use.n=TRUE, all=TRUE, cex=.8)

# create attractive postscript plot of tree
post(fit, file = "G:/dataplay/tree.ps",
title = "Classification Tree for Kyphosis")pfit<- prune(fit, cp= fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])

# plot the pruned tree
plot(pfit, uniform=TRUE,
main="Pruned Classification Tree for Kyphosis")
text(pfit, use.n=TRUE, all=TRUE, cex=.8)
post(pfit, file = "G:/dataplay/ptree.ps",
title = "Pruned Classification Tree for Kyphosis")


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

若不方便掃碼,搜微信號(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)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測極驗(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ù)說明請參見: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 = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+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); }