
邏輯回歸屬于概率統(tǒng)計(jì)的分類算法模型的算法,是根據(jù)一個(gè)或者多個(gè)特征進(jìn)行類別標(biāo)號(hào)預(yù)測(cè)。在R語(yǔ)言中可以通過(guò)調(diào)用logit函數(shù)執(zhí)行邏輯回歸分類算法并預(yù)測(cè)輸出概率。通過(guò)調(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ò)誤分類的非顯著變量,僅使用顯著的變量來(lái)訓(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)置模型來(lái)預(yù)測(cè)testset數(shù)據(jù)集的輸出,可以通過(guò)調(diào)整概率是否高于0.5來(lái)改變類別標(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”,沒(méi)有流失客戶,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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 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ù),更新分類模型,邏輯回歸算法的不足是無(wú)法處理多重共線性問(wèn)題,因此解決變量必須線性無(wú)關(guān)。glm提供了一個(gè)通用的線性回歸模型,可以通過(guò)設(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則說(shuō)明兩者是一致的,進(jìn)一步調(diào)用summsary函數(shù)來(lái)得到預(yù)測(cè)的模型。最后進(jìn)行計(jì)數(shù)統(tǒng)計(jì)與混淆矩陣。
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫(kù)管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫(kù)表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動(dòng)態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價(jià)值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫(kù)表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫(kù))處理 Excel 數(shù)據(jù)時(shí),“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場(chǎng)景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計(jì)劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計(jì)劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對(duì)象的 text 與 content:區(qū)別、場(chǎng)景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請(qǐng)求開發(fā)時(shí)(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價(jià)值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫(kù)表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請(qǐng)求工具對(duì)比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請(qǐng)求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)的科學(xué)計(jì)數(shù)法問(wèn)題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)時(shí)的科學(xué)計(jì)數(shù)法問(wèn)題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價(jià)值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營(yíng)問(wèn)題、提升執(zhí)行效率的核心手段,其價(jià)值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過(guò)程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計(jì)” 與 “用戶體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營(yíng)銷案例:數(shù)據(jù)驅(qū)動(dòng)下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見(jiàn)頂” 的當(dāng)下,精準(zhǔn)營(yíng)銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價(jià)值 在數(shù)據(jù)驅(qū)動(dòng)決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實(shí)踐到業(yè)務(wù)價(jià)值挖掘 在數(shù)據(jù)分析場(chǎng)景中,聚類分析作為 “無(wú)監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計(jì)模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價(jià)值導(dǎo)向 統(tǒng)計(jì)模型作為數(shù)據(jù)分析的核心工具,并非簡(jiǎn)單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10CDA 數(shù)據(jù)分析師:商業(yè)數(shù)據(jù)分析實(shí)踐的落地者與價(jià)值創(chuàng)造者 商業(yè)數(shù)據(jù)分析的價(jià)值,最終要在 “實(shí)踐” 中體現(xiàn) —— 脫離業(yè)務(wù)場(chǎng)景的分 ...
2025-09-10