Titanic倖存預測

 

import numpy as np 
import pandas as pd 

from sklearn import preprocessing
import matplotlib.pyplot as plt 
plt.rc("font", size=14)
import seaborn as sns
sns.set(style="white") #設置seaborn畫圖的背景爲白色
sns.set(style="whitegrid", color_codes=True)

df = pd.read_csv("./titanic_data.csv")

# 預覽數據
df.head()

“““

PassengerId 乘客ID Survived 是否倖存。0遇難,1倖存 Pclass 船艙等級,1Upper,2Middle,3Lower Name 姓名, Sex 性別, Age 年齡 SibSp 兄弟姐妹及配偶個數 Parch 父母或子女個數 Ticket 乘客的船票號, Fare 乘客的船票價 Cabin 乘客所在艙位,object Embarked 乘客登船口岸

”””

print('數據集包含的數據個數 {}.'.format(df.shape[0]))

# 查看數據集中各個特徵缺失的情況
df.isnull().sum()

# "age" 缺失的百分比 
print('"age" 缺失的百分比  %.2f%%' %((df['age'].isnull().sum()/df.shape[0])*100))

ax = df["age"].hist(bins=15, color='teal', alpha=0.6)
ax.set(xlabel='age')
plt.xlim(-10,85)
plt.show()

# 年齡的均值
print('The mean of "Age" is %.2f' %(df["age"].mean(skipna=True)))
# 年齡的中間值
print('The median of "Age" is %.2f' %(df["age"].median(skipna=True)))

# 倉位缺失的百分比
print('"Cabin" 缺失的百分比 %.2f%%' %((df['cabin'].isnull().sum()/df.shape[0])*100))

#"Cabin" 缺失的百分比 77.48%

# 登船地點的缺失率
print('"Embarked" 缺失的百分比 %.2f%%' %((df['embarked'].isnull().sum()/df.shape[0])*100))

#"Embarked" 缺失的百分比 0.23%

#只有 0.23% 的乘客的登船地點數據缺失, 可以使用衆數替代缺失的值.

print('按照登船地點分組 (C = Cherbourg, Q = Queenstown, S = Southampton):')
print(df['embarked'].value_counts())
sns.countplot(x='embarked', data=df, palette='Set2')
plt.show()

print('乘客登船地點的衆數爲 %s.' %df['embarked'].value_counts().idxmax())

print('乘客登船地點的衆數爲 %s.' %df['embarked'].value_counts().idxmax())

“““

基於以上分析, 我們進行如下調整:

  • 如果一條數據的 "Age" 缺失, 使用年齡的中位數 28 替代.
  • 如果一條數據的 "Embarked" 缺失, 使用登船地點的衆數 “S” 替代.
  • 由於太多乘客的 “Cabin” 數據缺失, 從所有數據中丟棄這個特徵的值.

”””

data = df.copy()
data["age"].fillna(df["age"].median(skipna=True), inplace=True)
data["embarked"].fillna(df['embarked'].value_counts().idxmax(), inplace=True)
data.drop('cabin', axis=1, inplace=True)

# 確認數據是否還包含缺失數據
data.isnull().sum()

data['pclass'].fillna(df["pclass"].value_counts().idxmax(), inplace=True)
data['survived'].fillna(df["survived"].value_counts().idxmax(), inplace=True)
data['sex'].fillna(df["sex"].value_counts().idxmax(), inplace=True)
data['sibsp'].fillna(df["sibsp"].value_counts().idxmax(), inplace=True)
data['parch'].fillna(df["parch"].value_counts().idxmax(), inplace=True)
data['ticket'].fillna(df["ticket"].value_counts().idxmax(), inplace=True)
data['fare'].fillna(df["fare"].value_counts().idxmax(), inplace=True)

#查看調整後的年齡分佈

plt.figure(figsize=(15,8))
ax = df["age"].hist(bins=15, normed=True, stacked=True, color='teal', alpha=0.6)
df["age"].plot(kind='density', color='teal')
ax = data["age"].hist(bins=15, normed=True, stacked=True, color='orange', alpha=0.5)
data["age"].plot(kind='density', color='orange')
ax.legend(['Raw Age', 'Adjusted Age'])
ax.set(xlabel='Age')
plt.xlim(-10,85)
plt.show()

## 創建一個新的變量'TravelAlone'記錄是否獨自成行, 丟棄“sibsp” (一同登船的兄弟姐妹或者配偶數量)與“parch”(一同登船的父母或子女數量)
data['TravelAlone']=np.where((data["sibsp"]+data["parch"])>0, 0, 1)
data.drop('sibsp', axis=1, inplace=True)
data.drop('parch', axis=1, inplace=True)

# 對 Embarked","Sex"進行獨熱編碼, 丟棄 'name', 'ticket'
final =pd.get_dummies(data, columns=["embarked","sex"])
final.drop('name', axis=1, inplace=True)
final.drop('ticket', axis=1, inplace=True)

final.head()

#數據分析

#年齡分佈

plt.figure(figsize=(15,8))
ax = sns.kdeplot(final["age"][final.survived == 1], color="darkturquoise", shade=True)
sns.kdeplot(final["age"][final.survived == 0], color="lightcoral", shade=True)
plt.legend(['Survived', 'Died'])
plt.title('Density Plot of Age for Surviving Population and Deceased Population')
ax.set(xlabel='Age')
plt.xlim(-10,85)
plt.show()

#票價

plt.figure(figsize=(15,8))
ax = sns.kdeplot(final["fare"][final.survived == 1], color="darkturquoise", shade=True)
sns.kdeplot(final["fare"][final.survived == 0], color="lightcoral", shade=True)
plt.legend(['Survived', 'Died'])
plt.title('Density Plot of Fare for Surviving Population and Deceased Population')
ax.set(xlabel='Fare')
plt.xlim(-20,200)
plt.show()

“““

生還與遇難羣體的票價分佈差異比較大, 說明這個特徵對預測乘客是否生還非常重要. 票價和倉位相關, 也許是倉位影響了逃生的效果, 我們接下來看倉位的分析.

”””

#倉位

sns.barplot('pclass', 'survived', data=df, color="darkturquoise")
plt.show()

#一等艙的生還可能性較大,說明地位權利金錢會影響獲救情況

sns.barplot('embarked', 'survived', data=df, color="teal")
plt.show()

#一家人獲救機率大一點,獨自一人的獲救率低一點

sns.barplot('TravelAlone', 'survived', data=final, color="mediumturquoise")
plt.show()

#外國女士優先,女士比男士生還可能性大一點

sns.barplot('sex', 'survived', data=df, color="aquamarine")
plt.show()

# 4. 使用Logistic Regression做預測

from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import Imputer
# 使用如下特徵做預測
cols = ["age","fare","TravelAlone","pclass","embarked_C","embarked_S","sex_male"] 

# 創建 X (特徵) 和 y (類別標籤)
X = final[cols]
y = final['survived']
X = Imputer().fit_transform(X)
# 將 X 和 y 分爲兩個部分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
# 檢測 logistic regression 模型的性能
# TODO 添加代碼:
# 1.訓練模型,  
# 2.根據模型, 以 X_test 爲輸入, 生成變量 y_pred

logr = LogisticRegression()
logr.fit(X_train,y_train)
y_pred = logr.predict(X_test)
print('Train/Test split results:' %y_pred)
print("準確率爲 %2.3f:"  %accuracy_score(y_test, y_pred, normalize=True, sample_weight=None))
#使用隨機森林算法
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train,y_train)
y_predrf = clf.predict(X_test)
print('Train/Test split results:' %y_pred)
print("準確率爲 %2.3f:"  %accuracy_score(y_test, y_predrf, normalize=True, sample_weight=None))

 

以上代碼地址http://localhost:8888/notebooks/Titanic-homework/titanic-logistic-regression-homework.ipynb

以上學習有貪心學院課程課後作業,總結添加隨機森林模型,比邏輯迴歸模型好一點點。後期可以在特徵工程上再改善,試試別的模型。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章