




已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
泰坦尼克數(shù)據(jù)集探索1. 簡(jiǎn)介:從泰塔尼克數(shù)據(jù)集中,根據(jù)每個(gè)乘客的信息,建立模型并進(jìn)行預(yù)測(cè)。整篇文章分為三步:1. 特征選擇2. 缺失數(shù)據(jù)處理3. 預(yù)測(cè)1.1 導(dǎo)入軟件包并檢查數(shù)據(jù) library(ggplot2) # 可視化 library(ggthemes) # 可視化 library(scales) # 可視化 library(dplyr) # 數(shù)據(jù)處理 library(mice) # 填充缺失數(shù)據(jù) library(randomForest) # 分類算法 #數(shù)據(jù)的導(dǎo)入 setwd(D:/Titanic)#設(shè)置默認(rèn)功過路徑 train test full # check data str(full)我們觀察到一共有1309條數(shù)據(jù),每一條數(shù)據(jù)有12個(gè)相關(guān)變量。2. 特征工程頭銜# 從名稱中挖掘 # 從乘客名字中提取頭銜 #R中的grep、grepl、sub、gsub、regexpr、gregexpr等函數(shù)都使用正則表達(dá)式的規(guī)則進(jìn)行匹配。默認(rèn)是egrep的規(guī)則,sub函數(shù)只實(shí)現(xiàn)第一個(gè)位置的替換,gsub函數(shù)實(shí)現(xiàn)全局的替換。 full$Title # 查看按照性別劃分的頭銜數(shù)量 table(full$Sex, full$Title)我們發(fā)現(xiàn)頭銜的類別太多,并且好多出現(xiàn)的頻次是很低的,我們可以將這些類別進(jìn)行合并 rare_title # 重命名稱呼 full$Titlefull$Title = Mlle full$Titlefull$Title = Ms full$Titlefull$Title = Mme full$Titlefull$Title %in% rare_title # 再次查看按照性別劃分的頭銜數(shù)量 table(full$Sex, full$Title)可以看到頭銜的個(gè)數(shù)得到了大量的縮減 #sapply()函數(shù):根據(jù)傳入?yún)?shù)規(guī)則重新構(gòu)建一個(gè)合理的數(shù)據(jù)類型返回 full$Surname # Create a family size variable including the passenger themselves full$Fsize # Create a family variable full$Family #為了直觀顯示,我們可以用ggplot2 畫出家庭成員數(shù)量和生存家庭數(shù)情況的圖形 ggplot(full1:891, aes(x = Fsize, fill = factor(Survived) + geom_bar(stat=count, position=dodge) + scale_x_continuous(breaks=c(1:11) + labs(x = Family Size) + theme_few() full$FsizeDfull$Fsize = 1 full$FsizeDfull$Fsize 1 full$FsizeDfull$Fsize 4 # Show family size by survival using a mosaic plot mosaicplot(table(full$FsizeD, full$Survived), main=Family Size by Survival, shade=TRUE)嘗試創(chuàng)建一些新的特征 # This variable appears to have a lot of missing values full$Cabin1:28 # Create a Deck variable. Get passenger deck A - F: full$Deck # Use ggplot2 to visualize embarkment, passenger class, & median fare ggplot(embark_fare, aes(x = Embarked, y = Fare, fill = factor(Pclass) + geom_boxplot() + geom_hline(aes(yintercept=80), + colour=red, linetype=dashed, lwd=2) + scale_y_continuous(labels=dollar_format() + theme_few()可以看到出發(fā)的一級(jí)乘客的中位票價(jià)與我們的登機(jī)手續(xù)費(fèi)乘客支付的80美元相當(dāng)。我們接近在這里和那里確定了幾個(gè)缺失值的位置。 1044行上的乘客的票價(jià)是缺失值。 # Since their fare was $80 for 1st class, they most likely embarked from C full$Embarkedc(62, 830) # Show row 1044 full1044, 這是從南安普敦(S)出發(fā)的三級(jí)乘客。 讓所有其他人分享他們的班級(jí)和登機(jī)牌(n = 494)可視化票價(jià)。 ggplot(fullfull$Pclass = 3 & full$Embarked = S, , + aes(x = Fare) + geom_density(fill = #99d6ff, alpha=0.4) + + geom_vline(aes(xintercept=median(Fare, na.rm=T),+ colour=red, linetype=dashed, lwd=1) + scale_x_continuous(labels=dollar_format() + theme_few()從這個(gè)可視化的角度來看,將NA票價(jià)值替換為上課時(shí)間為8.05美元的中位數(shù)似乎是相當(dāng)合理的。 # Replace missing fare value with median fare for class/embarkment full$Fare1044 # Show number of missing Age values sum(is.na(full$Age) # Make variables factors into factors factor_vars fullfactor_vars # Set a random seed set.seed(129) # Perform mice imputation, excluding certain less-than-useful variables: mice_mod # Save the complete output mice_output # Plot age distributions par(mfrow=c(1,2) hist(full$Age, freq=F, main=Age: Original Data, + col=darkgreen, ylim=c(0,0.04) hist(mice_output$Age, freq=F, main=Age: MICE Output, + col=lightgreen, ylim=c(0,0.04) # Show new number of missing Age values sum(is.na(full$Age)第二次特征工程現(xiàn)在我們知道每個(gè)人的年齡,我們可以創(chuàng)造幾個(gè)新的年齡變量:兒童和母親。 一個(gè)孩子只會(huì)是18歲以下的人,母親是1)女性2)18歲以上3)有超過0個(gè)孩子4)沒有頭銜 Miss # First well look at the relationship between age & survival ggplot(full1:891, aes(Age, fill = factor(Survived) + + geom_histogram() + + # I include Sex since we know (a priori) its a significant predictor+ facet_grid(.Sex) + + theme_few() # Create the column child, and indicate whether child or adult full$Childfull$Age 18 full$Childfull$Age = 18 # Show counts table(full$Child, full$Survived) # Adding Mother variable full$Mother full$Motherfull$Sex = female & full$Parch 0 & full$Age 18 & full$Title != Miss # Show counts table(full$Mother, full$Survived) # Finish by factorizing our two new factor variables full$Child full$Mother md.pattern(full)預(yù)測(cè)最后,我們準(zhǔn)備根據(jù)我們精心策劃和處理缺失值的變量,預(yù)測(cè)在泰坦尼克號(hào)的乘客中誰能幸存下來。 為此,我們將依靠隨機(jī)森林分類算法; 畢竟,我們花了所有的時(shí)間來進(jìn)行數(shù)據(jù)處理。拆分測(cè)試與訓(xùn)練 # Split the data back into a train set and a test set train test # Set a random seed set.seed(754) # Build the model (note: not all possible variables are used) rf_model # Show model error plot(rf_model, ylim=c(0,0.36) legend(topright, colnames(rf_model$err.rate), col=1:3, fill=1:3)黑線顯示總體錯(cuò)誤率低于20。 紅線和綠線分別顯示“死亡”和“幸存”的錯(cuò)誤率。 我們可以看到,現(xiàn)在我們比預(yù)測(cè)死亡更成功,而不是生存。變量重要性 # Get importance importance varImportance # Create a rank variable based on importance rankImportance %+ mutate(Rank = paste0(#,dense_rank(desc(Importance) # Use ggplot2 to visualize the relative importance of variables ggplot(rankImportance, aes(x = reorder(Variables, Importance), + y = Importance, fill = Importance) + geom_bar(stat=identity) + +
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 差旅服務(wù)定制化解決方案合作協(xié)議
- 車輛抵押反擔(dān)保借款合同解除后的后續(xù)處理
- 建筑行業(yè)常年法律顧問專業(yè)服務(wù)協(xié)議
- 土地利用規(guī)劃保密及實(shí)施合同
- 倉儲(chǔ)空間轉(zhuǎn)讓與倉儲(chǔ)管理軟件應(yīng)用合同
- 釣魚艇轉(zhuǎn)讓協(xié)議書范本
- 木房合同協(xié)議書范本
- 生物質(zhì)發(fā)電項(xiàng)目安裝與運(yùn)營管理合同
- 旅游企業(yè)特色旅游辦公用品采購及服務(wù)協(xié)議
- 城市綜合體停車場(chǎng)場(chǎng)地租賃及收費(fèi)管理制度合同
- 電網(wǎng)工程設(shè)備材料信息參考價(jià)(2024年第四季度)
- 《水性涂料產(chǎn)品介紹》課件
- 2025年森林防火項(xiàng)目立項(xiàng)申請(qǐng)報(bào)告模板
- 人教版數(shù)學(xué)七年級(jí)下冊(cè)6.1.3《平方根》聽評(píng)課記錄2
- 《危重病人護(hù)理常規(guī)》課件
- 2025年青島市即墨區(qū)衛(wèi)生健康局所屬事業(yè)單位和公立醫(yī)院招考聘用358人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025版國際貿(mào)易大宗商品交易平臺(tái)合作合同3篇
- 沙漠治理防塵網(wǎng)安裝協(xié)議
- 2023-2024學(xué)年人教版數(shù)學(xué)八年級(jí)下冊(cè)期末復(fù)習(xí)試卷(含答案)
- 2024年租賃業(yè)務(wù)操作手冊(cè)3篇
- 作家助手簽約標(biāo)準(zhǔn)合同范例
評(píng)論
0/150
提交評(píng)論