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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言使用隨機(jī)森林方法對數(shù)據(jù)分類
R語言使用隨機(jī)森林方法對數(shù)據(jù)分類
2018-01-19
收藏

R語言使用隨機(jī)森林方法對數(shù)據(jù)分類

說明

隨機(jī)森林是另一類可用的集成學(xué)習(xí)方法,該算法在訓(xùn)練過程中將產(chǎn)生多棵決策樹,每棵決策樹會根據(jù)輸入數(shù)據(jù)集產(chǎn)生相應(yīng)的預(yù)測輸出,算法采用投票機(jī)制選擇類別眾數(shù)做為預(yù)測結(jié)果。
操作
導(dǎo)入隨機(jī)森林包:
library(randomForest)
使用隨機(jī)森林分類器處理訓(xùn)練數(shù)據(jù):
churn.rf = randomForest(churn ~ .,data = trainset,importance = T)
churn.rf

Call:
 randomForest(formula = churn ~ ., data = trainset, importance = T)
               Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 4

        OOB estimate of  error rate: 5.27%
Confusion matrix:
    yes   no class.error
yes 245   97  0.28362573
no   25 1948  0.01267106

利用訓(xùn)練好的模型對測試集進(jìn)行分類預(yù)測:

churn.prediction = predict(churn.rf,testset)

類似其它分類處理,產(chǎn)生分類表:

table(churn.prediction,testset$churn)

churn.prediction yes  no
             yes 111   7
             no   30 870
調(diào)用plot函數(shù)繪制森林對象均方差:

plot(churn.rf)

隨機(jī)森林的均方差
根據(jù)建立好的模型評估各屬性的重要度:

importance(churn.rf)
                                     yes        no MeanDecreaseAccuracy MeanDecreaseGini
international_plan            68.9592890 54.118994            72.190204         50.35584
voice_mail_plan               18.8899994 15.832400            19.607844         10.44601
number_vmail_messages         21.3080062 16.262770            22.068514         19.05619
total_day_minutes             28.3237379 30.323756            39.961077         79.91474
total_day_calls                0.6325725 -1.131930            -0.802642         20.80946
total_day_charge              28.4798708 28.146414            35.858906         77.84837
total_eve_minutes             18.5242988 20.572464            24.484322         42.99373
total_eve_calls               -3.3431379 -2.301767            -3.495801         17.45619
total_eve_charge              20.4379809 20.619705            24.489771         44.02855
total_night_minutes            0.9451961 16.105720            16.694651         22.93663
total_night_calls             -0.3497164  2.202619             1.869193         19.94091
total_night_charge             0.1110824 15.977083            16.593633         22.22769
total_intl_minutes            17.3951655 20.063485            24.967698         26.05059
total_intl_calls              37.3613313 23.415764            35.497785         33.03289
total_intl_charge             16.7925666 19.636891            24.498369         26.60077
number_customer_service_calls 79.7530696 59.731615            85.221845         67.29635

調(diào)用varlmPlot函數(shù)繪制變量重要性曲線

varImpPlot(churn.rf)


變量重要性示例
調(diào)用margin及plot函數(shù)并繪制邊緣累計(jì)分布圖:

margins.rf = margin(churn.rf,trainset)
plot(margins.rf)


隨機(jī)森林算法邊緣累積分布圖
還可以用直方圖來繪制隨機(jī)森林的邊緣分布:

hist(margins.rf,main = "Margines of Random Forest for churn dataset")


邊緣分布直方圖
調(diào)用boxplot繪制隨機(jī)森林各類別邊緣的箱線圖

boxplot(margins.rf ~ trainset$churn,main = "Margines of Random Forest for churn dataset by class")

隨機(jī)森林類別邊緣箱圖
原理:
隨機(jī)森林算法目標(biāo)是通過將多個(gè)弱學(xué)習(xí)機(jī)(如單棵決策樹)組合得到一個(gè)強(qiáng)學(xué)習(xí)機(jī),算法的處理過程與bagging方法非常相似,假設(shè)當(dāng)擁有N個(gè)特征數(shù)為M的樣例,首先采用bootstrap對數(shù)據(jù)集進(jìn)行采樣,每次隨機(jī)采樣N個(gè)樣本作為單個(gè)決策樹的訓(xùn)練數(shù)據(jù)集。在每個(gè)節(jié)點(diǎn),算法首先隨機(jī)選取m(m << M)個(gè)變量,從它們中間找到能夠提供最佳分割效果的預(yù)測屬性。
然后,算法在不剪枝的前提下生成單顆決策樹,最后從每個(gè)決策樹都得到一個(gè)分類預(yù)測結(jié)果。
如果是回歸分析,算法將取所有預(yù)測的平均值或者加權(quán)平均值作為最后剛出,如果是分類問題,則選擇類別預(yù)測眾數(shù)做為最終預(yù)測輸出。
隨機(jī)森林包括兩個(gè)參數(shù),ntree(決策樹個(gè)數(shù))和mtry(可用來尋找最佳特征特征個(gè)數(shù)),而bagging算法只使用了一個(gè)ntree參數(shù),因此,如果將mtry設(shè)置成與訓(xùn)練數(shù)據(jù)集特征值一樣大時(shí),隨機(jī)森林算法就等同于bagging算法。
本例利用randomForest包提供的隨機(jī)森林算法建立了分類模型,將importance值設(shè)置為“T”,以確保對預(yù)測器的重要性進(jìn)行評估。
與bagging和boosting方法類似,一旦隨機(jī)森林的模型構(gòu)建完成,我們就能利用其對測試數(shù)據(jù)集進(jìn)行預(yù)測,并得到相應(yīng)的分類表。
randomForest包還提供了importance和varlmpPlot函數(shù)則可以通過繪制平均精確度下降或者平均基尼下降曲線實(shí)現(xiàn)屬性重要性的可視化。

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

若不方便掃碼,搜微信號: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, // 表示用戶后臺檢測極驗(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); }