泰坦尼克號預測(隨機森林預測)
泰坦尼克號預測,本次是用隨機森林模型預測的;下次會把邏輯回歸的預測發出來。
#導入所需要包library("openxlsx")library(stringr)library(dplyr)library(mice)#用來插補缺失值的包library(ggplot2)#圖形可視化的包#導入數據,分為測試集合訓練集ceshi<-read.csv("C:/Users/Administrator/Desktop/test.csv")ceshixunlian<-read.csv("C:/Users/Administrator/Desktop/train.csv")xunlian#導入別人做好的一個集合,後邊會刪除。保持訓練集和預測集一致。xincun<-read.csv("C:/Users/Administrator/Desktop/gender_submission.csv")summary(xunlian)str(xunlian)str(ceshi)class(data)#passengerid表示的是乘客的編號#pclass是社會等級,1上等,2中等,3下等#survived表示的是是否倖存,1表示倖存,0表示未倖存#name表示的是名字#sex表示的是性別#age表示的是年紀#sibsp表示是的同輩親屬的數量#parch非同輩親屬的的的數量#ticket表示的是船票#fare表示是【票價#cabin表示的是船艙號#embarked登陸的口岸xunlianceshiceshi<-merge(ceshi,xincun,by="PassengerId")#測試集添加倖存狀況ceshi#合併數據集data<-rbind(xunlian,ceshi)data#此處模擬其他人的結合過程data2<-bind_rows(xunlian,ceshi)data2datastr(data)colSums(is.na(data))summary(data)#探索倖存者和社會等級的關係data$Survived<-factor(data$Survived)data$Surviveddata$Pclass<-factor(data$Pclass,levels=c(1:3),labels=c("upper","middle","lower"))tupian<-ggplot(data=data,aes(x=Pclass,fill=Survived))+geom_bar(position="dodge")+geom_text(stat = "count",aes(label=..count..),position = position_dodge(width = 1),vjust=-0.5)#ggplot()數據圖層,geo_bar()為幾何圖層,意思為做圖形為柱狀圖,居中;geo_text()為美學圖層,position為位置居中寬度為1,vjust=-0.5是高度意思tupian#由圖可知社會等級越高,倖存的概率越大。cor(data$Survived,data$Pclass)#探索倖存這和姓名的關係,一般有危險都是婦女,兒童,老人先走。探索本次事件倖存者和姓名的關係。data$Nameclass(data$Name)table(data$Survived)#分割字元欄位,str_split_fixed()函數使用兩次。注意第二次使用絕對路徑out<-str_split_fixed(data$Name,",",n=2)out2<-str_split_fixed(out[,2],"\.",n=2)out2[,1]class(out[,2])out[,1]factor(out[,2])class(out)class(out2)factor(out)class(out)as.data.frame(out)class(out)out2[,2]data$title#此處是別人的計算過程。data$title<-gsub("(.*,)|(\..*)","",data$Name)#關注路徑的使用,其中Name可以經過函數str_split_fixed()兩次使用提取中間的尊稱,其中第二次使用時候必須引用絕對路路徑data$titledata$title<-out2[,1]data$title#因子的個數table(data$title)class(data$title)class(data$Survived)factor(data$Survived)draw<-ggplot(data=data,aes(x=title,fill=Survived)+geom_bar(position="dodge")factor(data$title)data$Survived<-factor(data$Survived)table(data$title)class(data$title)as.character.Date(data$title)class(data$title)#個欄位title重新命名data$title<-factor(data$title)data$title[data$title=="Mme"|data$title=="Mlle"]<-"Mlle"data$title[data$title %in% c("Capt","Don","Major","Sir")]<-"Sir"data$title[data$title %in% c("Dona","Lady","the Countess","Jonkheer")]<-"Lady"data$title<-factor(data$title)data$Survived<-factor(data$Survived)#名稱與倖存的關係huatu<-ggplot(data=data[1:nrow(data),],aes(x=title,fill=Survived)+geom_bar(position="stack")+geom_text(stat="count",aes(label=..count..),position = position_stack(vjust = 0.5)))#性別與倖存的關係data$Sex<-factor(data$Sex)ggplot(data=data,aes(x=Sex,fill=Survived))+geom_bar()ggplot(data=data,aes(x=Sex,fill=Survived))+geom_bar(position = "dodge")ggplot(data=data,aes(x=Sex,fill=Survived))+geom_bar(position = "dodge")+geom_text(stat = "count",aes(label=..count..),position = position_dodge(width = 1),vjust=-0.5)#年紀與倖存者的關係factor(data$Age)data$Fareggplot(data=data[!is.na(data$Age),],aes(x=Age,color=Survived))+geom_line(stat = "bin",binwidth_=5)#圖中可得。兒童的存活率較高ggplot(data=data,aes(x=SibSp,fill=Survived))+geom_bar(position ="dodge")+ geom_text(stat = "count",aes(label=..count..),position=position_dodge(width = 1),vjust=-0.5)ggplot(data=data,aes(x=data$SibSp,fill=Survived))+geom_bar(position = "dodge")+ geom_text(stat = "count",aes(label=..count..),position = position_dodge(width = 1),vjust=-0.5)#探索票價和倖存率的關係ggplot(data=data,aes(x=Fare,color=Survived),na.omit=T)+geom_line(stat = "bin",binwidth_=1)#整個圖像成右偏趨勢,說明票價越高倖存的機會越大colSums(is.na(data))#尋找缺失值class(data$Cabin)table(data$Cabin)sapply(data, function(x){sum(is.na(x))})#隨機插補法預測Age的數值age<-mice(data[,!names(data) %in% c("PassengerId","Name","Ticket","cabin","Survivied")],method = "rf")help(mice)md.pattern(data)age<-complete(age)data<-agesum(is.na(data$Age))colSums(is.na(data))traindata<-data[c(1:892),]traindata#訓練隨機森林模型install.packages("randomForest")library(randomForest)rf_model<-randomForest(Survived~Pclass+Sex+SibSp+Parch+Fare+Embarked+title,data=traindata)plot(rf_model)#隨機森林預測testtrain<-data[c(893:1309),]prediction<-predict(rf_model,testtrain)predictionclass(prediction)#寫入文件夾。write.csv(prediction,file = "C:/Users/Administrator/Desktop/presiction.csv")result<-read.csv("C:/Users/Administrator/Desktop/presiction.csv")names(result)<-c("Passengerid","Survived")resultwrite.csv(result,file = "C:/Users/Administrator/Desktop/result.csv")
推薦閱讀:
※銷售運營管理業務應用案例--如何應用Excel創建會講故事的銷售漏斗分析儀
※【數據分析】中文筆記翻譯計劃順利結束
※就是它了-結合自己興趣與事業發展的新方向
※第一次製作數據報告
※探索電影大數據