頭歌實踐教學平臺-泰坦尼克生還預測——視覺化與探索性資料分析-答案

educoder王子發表於2024-06-29

非盈利文章,僅提供編輯器內的答案或程式碼,不提供啟動服務及需要命令列的程式碼

第1關:存活率與性別和船艙等級之間的關係

import pandas as pd
import numpy as np
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
sns.set()
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

def student():
# ********* Begin *********#
data_T = pd.read_csv(r'Task1/train.csv')
fig,axes = plt.subplots(1,2)
sns.violinplot(x='Pclass' , y='Age', data=data_T , split=True , ax=axes[0] ,hue='Survived')
sns.violinplot(x='Sex' , y='Age', data=data_T , split=True , ax=axes[1] ,hue='Survived')

plt.savefig('Task1/img/T1.png')
plt.show()
# ********* End *********#

第2關:各個口岸的生還率

import pandas as pd
import numpy as np
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
sns.set()
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

def student():
# ********* Begin *********#
data_T = pd.read_csv('Task2/train.csv')
plt.figure(figsize=(10,10))

sns.factorplot(data=data_T,x='Embarked',y='Survived')

plt.savefig('Task2/img/T1.png')
plt.show()
# ********* End *********#

第3關:統計各登船口岸的登船人數以及生還率

import pandas as pd
import numpy as np
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
sns.set()
import matplotlib.pyplot as plt

def student():
# ********* Begin *********#
data_T = pd.read_csv('Task3/train.csv')
fig,ax = plt.subplots(2,2,figsize=(10,10))

sns.countplot("Embarked",data=data_T,ax=ax[0,0])
ax[0,0].set_title("No.Of Passengers Boarded")
sns.countplot("Embarked",hue="Sex",data=data_T,ax=ax[0,1])  
ax[0,1].set_title("Male-Female Spilt for Embarked")
sns.countplot("Embarked",hue="Survived",data=data_T,ax=ax[1,0])
ax[1,0].set_title("Embarked vs Survived")
sns.countplot("Embarked",hue="Pclass",data=data_T,ax=ax[1,1])
ax[1,1].set_title("Embarked vs Pclass")
plt.savefig("Task3/img/T1.jpg")
plt.show()
# ********* End *********#

第4關:船客兄弟姐妹妻子丈夫的數量與生存率之間的關係

import pandas as pd
import numpy as np
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
sns.set()
import matplotlib.pyplot as plt

def student():
# ********* Begin *********#

data_T = pd.read_csv("Task4/train.csv")
f,ax = plt.subplots(1,2,figsize=(10,10))
sns.barplot(x="SibSp",y="Survived", data=data_T,ax=ax[0])
ax[0].set_title("SibSp vs Survived")
sns.catplot(x='SibSp',y="Survived", data=data_T,ax=ax[1], kind="point")
ax[1].set_title("SibSp vs Survived")
plt.close(2)
plt.savefig("Task4/img/T1.png")
# ********* End *********#

相關文章