日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

泰坦尼克号生存预测入门

發布時間:2024/7/5 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 泰坦尼克号生存预测入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 數據預覽
    • 2. 特征初步選擇
    • 3. 增加特征`Sex`和`Embarked`
    • 4. 選擇隨機森林調參
    • 5. 實踐總結

本文作為學習記錄,參考 此處,如有侵權,聯系刪除。

1. 數據預覽

  • 數據集下載
import pandas as pd import numpy as np from pandas import Series, DataFrame data_train = pd.read_csv("titanic_train.csv") data_test = pd.read_csv("titanic_test.csv") # 讀取前10行 data_train.head(10)

data_train.info() -------------------------------- <class 'pandas.core.frame.DataFrame'> RangeIndex: 891 entries, 0 to 890 Data columns (total 12 columns): PassengerId 891 non-null int64 Survived 891 non-null int64 Pclass 891 non-null int64 Name 891 non-null object Sex 891 non-null object Age 714 non-null float64 SibSp 891 non-null int64 Parch 891 non-null int64 Ticket 891 non-null object Fare 891 non-null float64 Cabin 204 non-null object # 有的原始信息缺失 Embarked 889 non-null object dtypes: float64(2), int64(5), object(5) memory usage: 83.7+ KB data_test.describe() # 可見一些統計信息

2. 特征初步選擇

  • 由于Cabin客艙號大部分都缺失,進行填補,可能會造成較大誤差,不選
  • 乘客id,是個連續數據,跟是否存活應該無關,不選
  • 年齡Age,是個比較重要的特征,對缺失的部分用中位數進行填充
data_train["Age"] = data_train["Age"].fillna(data_train["Age"].median())
  • 初步調用一些模型(默認參數)進行預測:
  • algs = [Perceptron(),KNeighborsClassifier(),GaussianNB(),DecisionTreeClassifier(), LinearRegression(),LogisticRegression(),SVC(),RandomForestClassifier()]
from sklearn.linear_model import Perceptron from sklearn.neighbors import KNeighborsClassifier from sklearn.naive_bayes import GaussianNB from sklearn.tree import DecisionTreeClassifier from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier # boost from sklearn.model_selection import KFold # 交叉驗證 features = ["Pclass","Age","SibSp","Parch","Fare"] algs = [Perceptron(),KNeighborsClassifier(),GaussianNB(),DecisionTreeClassifier(),LinearRegression(),LogisticRegression(),SVC(),RandomForestClassifier()] for alg in algs:kf = KFold(n_splits=5,shuffle=True,random_state=1)predictions = []for train, test in kf.split(data_train):train_features = (data_train[features].iloc[train,:])train_label = data_train["Survived"].iloc[train]alg.fit(train_features,train_label)test_predictions = alg.predict(data_train[features].iloc[test,:])predictions.append(test_predictions)predictions = np.concatenate(predictions,axis=0) # 合并3組數據predictions[predictions>0.5] = 1predictions[predictions<=0.5] = 0accuracy = sum(predictions == data_train["Survived"])/len(predictions)print("模型準確率:", accuracy)

交叉驗證的參數 shuffle = True,打亂數據

模型準確率: 0.531986531986532 模型準確率: 0.5488215488215489 模型準確率: 0.5566778900112234 模型準確率: 0.5353535353535354 模型準確率: 0.5712682379349046 模型準確率: 0.569023569023569 模型準確率: 0.5712682379349046 模型準確率: 0.5364758698092031

交叉驗證參數 shuffle = False,正確率就提高了,why ???求解答

模型準確率: 0.5679012345679012 模型準確率: 0.6644219977553311 模型準確率: 0.6745230078563412 模型準確率: 0.632996632996633 模型準確率: 0.6947250280583613 模型準確率: 0.6980920314253648 模型準確率: 0.6644219977553311 模型準確率: 0.6846240179573513

3. 增加特征Sex和Embarked

  • 上面效果不好,增加一些特征
  • 增加特征Sex和Embarked,查看對預測的影響
  • 這兩個特征為字符串,需要轉成數字
print(pd.value_counts(data_train.loc[:,"Embarked"])) ---------------------- S 644 C 168 Q 77 Name: Embarked, dtype: int64 # sex轉成數字 data_train.loc[data_train["Sex"]=="male","Sex"] = 0 data_train.loc[data_train["Sex"]=="female","Sex"] = 1 # 登船地點,缺失的用最多的S進行填充 data_train["Embarked"] = data_train["Embarked"].fillna('S') data_train.loc[data_train["Embarked"]=="S", "Embarked"]=0 data_train.loc[data_train["Embarked"]=="C", "Embarked"]=1 data_train.loc[data_train["Embarked"]=="Q", "Embarked"]=2 features = ["Pclass","Age","SibSp","Parch","Fare","Embarked","Sex"]

交叉驗證的參數 shuffle = True,正確率依然很低,再次提問,why ???

模型準確率: 0.5521885521885522 模型準確率: 0.5432098765432098 模型準確率: 0.5185185185185185 模型準確率: 0.5286195286195287 模型準確率: 0.5230078563411896 模型準確率: 0.5252525252525253 模型準確率: 0.5723905723905723 模型準確率: 0.5196408529741863

交叉驗證參數 shuffle = False,正確率相比于上面缺少特征Sex和Embarked時,提高了不少,好的特征對預測結果提升很有幫助

模型準確率: 0.675645342312009 模型準確率: 0.691358024691358 模型準確率: 0.7856341189674523 模型準確率: 0.7822671156004489 模型準確率: 0.7878787878787878 模型準確率: 0.792368125701459 模型準確率: 0.6655443322109988 模型準確率: 0.8058361391694725

4. 選擇隨機森林調參

從上面可以看出隨機森林模型的預測效果最好,使用該模型,進行調參

features = ["Pclass","Age","SibSp","Parch","Fare","Embarked","Sex"] estimator_num = [5,10,15,20,25,30] splits_num = [3,5,10,15] for e_n in estimator_num:for sp_n in splits_num:alg = RandomForestClassifier(n_estimators=e_n)kf = KFold(n_splits=sp_n,shuffle=False,random_state=1)predictions_train = []for train, test in kf.split(data_train):train_features = (data_train[features].iloc[train,:])train_label = data_train["Survived"].iloc[train]alg.fit(train_features,train_label)train_pred = alg.predict(data_train[features].iloc[test,:])predictions_train.append(train_pred)predictions_train = np.concatenate(predictions_train,axis=0) # 合并3組數據predictions_train[predictions_train>0.5] = 1predictions_train[predictions_train<=0.5] = 0accuracy = sum(predictions_train == data_train["Survived"])/len(predictions_train)print("%d折數據集,%d棵決策樹,模型準確率:%.4f" %(sp_n,e_n,accuracy)) 3折數據集,5棵決策樹,模型準確率:0.7890 5折數據集,5棵決策樹,模型準確率:0.7901 10折數據集,5棵決策樹,模型準確率:0.7935 15折數據集,5棵決策樹,模型準確率:0.8092 3折數據集,10棵決策樹,模型準確率:0.7890 5折數據集,10棵決策樹,模型準確率:0.8047 10折數據集,10棵決策樹,模型準確率:0.8137 15折數據集,10棵決策樹,模型準確率:0.8092 3折數據集,15棵決策樹,模型準確率:0.7868 5折數據集,15棵決策樹,模型準確率:0.8002 10折數據集,15棵決策樹,模型準確率:0.8092 15折數據集,15棵決策樹,模型準確率:0.8047 3折數據集,20棵決策樹,模型準確率:0.7969 5折數據集,20棵決策樹,模型準確率:0.8092 10折數據集,20棵決策樹,模型準確率:0.8114 15折數據集,20棵決策樹,模型準確率:0.8092 3折數據集,25棵決策樹,模型準確率:0.7924 5折數據集,25棵決策樹,模型準確率:0.8070 10折數據集,25棵決策樹,模型準確率:0.8103 15折數據集,25棵決策樹,模型準確率:0.8025 3折數據集,30棵決策樹,模型準確率:0.7890 5折數據集,30棵決策樹,模型準確率:0.8013 10折數據集,30棵決策樹,模型準確率:0.8081 15折數據集,30棵決策樹,模型準確率:0.8193

最后一種參數下,隨機森林模型的預測效果最好

5. 實踐總結

熟悉了機器學習的基本流程

  • 導入工具包 numpy, pandas, sklearn等
  • 數據讀取,pandas.read_csv(file)
  • pandas的一些數據處理
    data.head(n) 讀取前n行展示
    data.info() 獲取數據的信息
    data.describe() 獲取統計信息(均值、方差等)
    data["Age"] = data["Age"].fillna(data["Age"].median()) 缺失數據填補(均值、最大值、根據別的特征分段填充等)
    性別等字符串特征數字化
  • 選取特征,初步預測
  • 不斷的加入新的特征預測
  • 選定較好的模型,再調整這些模型的參數,選出最好的模型參數

總結

以上是生活随笔為你收集整理的泰坦尼克号生存预测入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。