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

熱線電話:13121318867

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

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

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

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

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

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

str(iris)#集中展示數(shù)據文件的結構

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

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





plot(iris_ctree, type="simple")




iris的決策樹分析(二)

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


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

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

                 決策樹在構造過程中,由于數(shù)據中含有噪聲和離群點等異常數(shù)據,訓練出來的樹的分枝會過分擬合數(shù)據,處理過分擬合的辦法是對樹進行剪枝,剪枝后的樹更 小,復雜度更低,而且容易理解,常用的剪枝辦法有,先剪枝和后剪枝。                 R語言中實現(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ù)據。         na.action:缺失數(shù)據的處理辦法,默認為刪除因變量缺失的觀測而保留自變量缺失的觀測。         method:樹的末端數(shù)據類型選擇相應的變量分割方法,連續(xù)性method=“anova”,離散型使用method=“class”,,計數(shù)型 method=“poisson”,生存分析型method=“exp”。          parms:設置三個參數(shù),先驗概率,損失矩陣,分類矩陣的度量方法。          control:控制每個節(jié)點上的最小樣本量,交叉驗證的次數(shù),復雜性參量:cp:complexity pamemeter。      2,剪枝使用          prune(tree,cp,....)           tree常是rpart()的結果對象,cp 復雜性參量       3 顯示結果的語句printcp(fit)顯示復雜性表plotcp(fit)畫交叉驗證結果圖rsq.rpart(fit)R-squared 和 relative error for different splits (2 plots). labels are only appropriate for  "anova" method.print(fit)打印結果summary(fit)基本信息plot(fit)決策樹text(fit)給樹添加標簽post(fit,file=)保存結果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ù)據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

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

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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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); }