
邏輯回歸屬于概率統(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 0 1 0
[302] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 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 0 0 0
[345] 0 0 0 0 0 0 0 0 0 1 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 0 0
[388] 0 1 1 0 0 0 0 0 0 0 0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 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
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03