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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀R語言使用邏輯回歸分類算法
R語言使用邏輯回歸分類算法
2018-05-31
收藏

R語言使用邏輯回歸分類算法

邏輯回歸屬于概率統(tǒng)計(jì)的分類算法模型的算法,是根據(jù)一個(gè)或者多個(gè)特征進(jìn)行類別標(biāo)號(hào)預(yù)測(cè)。在R語言中可以通過調(diào)用logit函數(shù)執(zhí)行邏輯回歸分類算法并預(yù)測(cè)輸出概率。通過調(diào)用glm函數(shù)將family參數(shù)也就是響應(yīng)分布指定為binominal(二項(xiàng)式),就是使用邏輯回歸算法。

操作
同進(jìn)述內(nèi)容一樣準(zhǔn)備好訓(xùn)練數(shù)據(jù)集與測(cè)試數(shù)據(jù)集。
fit = glm(churn ~ .,data = trainset,family = binomial)
summary(fit)
Call:
glm(formula = churn ~ ., family = binomial, data = trainset)

Deviance Residuals:
    Min       1Q   Median       3Q      Max  
-3.1519   0.1983   0.3460   0.5186   2.1284  

Coefficients:
                                Estimate Std. Error z value Pr(>|z|)    
(Intercept)                    8.3462866  0.8364914   9.978  < 2e-16 ***
international_plan1           -2.0534243  0.1726694 -11.892  < 2e-16 ***
voice_mail_plan1               1.3445887  0.6618905   2.031 0.042211 *  
number_vmail_messages         -0.0155101  0.0209220  -0.741 0.458496    
total_day_minutes              0.2398946  3.9168466   0.061 0.951163    
total_day_calls               -0.0014003  0.0032769  -0.427 0.669141    
total_day_charge              -1.4855284 23.0402950  -0.064 0.948592    
total_eve_minutes              0.3600678  1.9349825   0.186 0.852379    
total_eve_calls               -0.0028484  0.0033061  -0.862 0.388928    
total_eve_charge              -4.3204432 22.7644698  -0.190 0.849475    
total_night_minutes            0.4431210  1.0478105   0.423 0.672367    
total_night_calls              0.0003978  0.0033188   0.120 0.904588    
total_night_charge            -9.9162795 23.2836376  -0.426 0.670188    
total_intl_minutes             0.4587114  6.3524560   0.072 0.942435    
total_intl_calls               0.1065264  0.0304318   3.500 0.000464 ***
total_intl_charge             -2.0803428 23.5262100  -0.088 0.929538    
number_customer_service_calls -0.5109077  0.0476289 -10.727  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)
  Null deviance: 1938.8  on 2314  degrees of freedom
Residual deviance: 1515.3  on 2298  degrees of freedom
AIC: 1549.3

Number of Fisher Scoring iterations: 6

找到分類模型中包含的可能導(dǎo)致錯(cuò)誤分類的非顯著變量,僅使用顯著的變量來訓(xùn)練分類模型。

 fit = glm(churn ~ international_plan + voice_mail_plan + number_customer_service_calls,data = trainset,family = binomial)
summary(fit)

Call:
glm(formula = churn ~ international_plan + voice_mail_plan +
    number_customer_service_calls, family = binomial, data = trainset)

Deviance Residuals:
    Min       1Q   Median       3Q      Max  
-2.6485   0.3067   0.4500   0.5542   1.6509  

Coefficients:
                              Estimate Std. Error z value Pr(>|z|)    
(Intercept)                    2.68272    0.12064  22.237  < 2e-16 ***
international_plan1           -1.97626    0.15998 -12.353  < 2e-16 ***
voice_mail_plan1               0.79423    0.16352   4.857 1.19e-06 ***
number_customer_service_calls -0.44341    0.04445  -9.975  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1938.8  on 2314  degrees of freedom
Residual deviance: 1678.5  on 2311  degrees of freedom
AIC: 1686.5

Number of Fisher Scoring iterations: 5
調(diào)用fit使用一個(gè)內(nèi)置模型來預(yù)測(cè)testset數(shù)據(jù)集的輸出,可以通過調(diào)整概率是否高于0.5來改變類別標(biāo)記的輸出結(jié)果。
#這是選擇預(yù)測(cè)之后的輸出結(jié)果,這個(gè)參數(shù)能用在binomial數(shù)據(jù),也就是響應(yīng)變量是二分型的時(shí)候,這個(gè)參數(shù)選成type=response,表示輸出結(jié)果預(yù)測(cè)響應(yīng)變量為1的概率。
pred = predict(fit,testset,type = "response")
#將ped中概率大于0.5的設(shè)置TRUE,代表為“no”,沒有流失客戶,1
#將ped中概率小于0.5的設(shè)置FALSE,代表為“yes”,有流失
客戶,0
Class = pred > 0.5
summary(Class)
   Mode   FALSE    TRUE
logical      28     990

對(duì)測(cè)試數(shù)據(jù)集的分類和預(yù)測(cè)結(jié)果進(jìn)行統(tǒng)計(jì)分析計(jì)數(shù):

tb = table(testset$churn,Class)
> tb
     Class
      FALSE TRUE
  yes    15  126
  no     13  864
將上一步驟的統(tǒng)計(jì)結(jié)果用分類形式表輸出,并生成混淆矩陣

churn.mod = ifelse(testset$churn == "yes",1,0)
> churn.mod
   [1] 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0
  [44] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
  [87] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0
 [130] 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
 [173] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 [216] 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0
 [259] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 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0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
 [818] 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
 [861] 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [904] 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1
 [947] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0
 [990] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0

將Class轉(zhuǎn)化成numeric

ABC = as.numeric(Class)
ABC與churn.mod 中0,1代表的意思相反,將ABC進(jìn)行數(shù)值取反

BC = 1 - ABC
計(jì)算混淆矩陣

confusionMatrix(churn.mod,BC)
Confusion Matrix and Statistics

          Reference
Prediction   0   1
         0 864  13
         1 126  15

               Accuracy : 0.8635         
                 95% CI : (0.8408, 0.884)
    No Information Rate : 0.9725         
    P-Value [Acc > NIR] : 1              

                  Kappa : 0.138          
 Mcnemar's Test P-Value : <2e-16         

            Sensitivity : 0.8727         
            Specificity : 0.5357         
         Pos Pred Value : 0.9852         
         Neg Pred Value : 0.1064         
             Prevalence : 0.9725         
         Detection Rate : 0.8487         
   Detection Prevalence : 0.8615         
      Balanced Accuracy : 0.7042         

       'Positive' Class : 0         

邏輯回歸算法和線性回歸非常相似,兩者區(qū)別是在于線性回歸算法中的變量是連續(xù)變量,而邏輯回歸響應(yīng)變量是二分類的變量(名義變量),使用邏輯回歸算法主要目的是利用logit模型去預(yù)測(cè)和測(cè)量變量相關(guān)的名義變量的概率。邏輯回歸公式:ln(P/(1-P)),P為某事情發(fā)生的概率。

邏輯回歸的算法的優(yōu)勢(shì)是在于算法易于理解,能夠直接輸出預(yù)測(cè)模型的邏輯概率邏輯值以及結(jié)果的置信區(qū)間,與決策樹難以更新模型不同,邏輯回歸算法能夠迅速在邏輯回歸算法中合并新的數(shù)據(jù),更新分類模型,邏輯回歸算法的不足是無法處理多重共線性問題,因此解決變量必須線性無關(guān)。glm提供了一個(gè)通用的線性回歸模型,可以通過設(shè)置family參數(shù)得到,當(dāng)為binomial回歸時(shí),可以實(shí)現(xiàn)二元分類。

調(diào)用fit函數(shù)預(yù)測(cè)測(cè)試數(shù)據(jù)集testset的類別響應(yīng)變量,fit函數(shù)能夠輸出類標(biāo)號(hào)的概率,如果概率值小于等于0.5,意味預(yù)測(cè)得出的類標(biāo)號(hào)與測(cè)試數(shù)據(jù)集的實(shí)際類標(biāo)號(hào)不相符,如果大于0.5則說明兩者是一致的,進(jìn)一步調(diào)用summsary函數(shù)來得到預(yù)測(cè)的模型。最后進(jìn)行計(jì)數(shù)統(tǒng)計(jì)與混淆矩陣。

數(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); }