#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。

#学习日记:阿里云天池,机器学习实战进阶:泰坦尼克号乘客获救预测。有一些课程代码运行报错,自己修改了,亲测能跑
#win10 Pycharm环境 ,需要用到的库请提前在terminal终端用命令下载:pip install 库名称

 


#3.3年龄与存活与否的关系 Age
fig, ax=plt.subplots(1,2,figsize = (18,5))
ax[0].set_yticks(range(0,110,10))
sns.violinplot("Pclass","Age",hue="Survived",data=train_data,split=True,ax=ax[0])
ax[0].set_title("Pclass and Age vs Survived")

ax[1].set_yticks(range(0,110,10))
sns.violinplot("Sex","Age",hue="Survived",data=train_data,split=True,ax=ax[1])
ax[1].set_title("Sex and Age vs Survived")
plt.show()

#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。

#分析总体的年龄分布

plt.figure(figsize=(15,5))

plt.subplot(121)
train_data["Age"].hist(bins=100)
plt.xlabel("Age")
plt.ylabel("Num")

plt.subplot(122)
train_data.boxplot(column="Age")
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
#不同年龄下的生存和非生存的分布情况

#aspect每个小图的横轴长度和纵轴的比
facet=sns.FacetGrid(data=train_data,hue="Survived",aspect=4)
facet.map(sns.kdeplot,"Age",shade=True)
facet.set(xlim=(0,train_data["Age"].max()))
facet.add_legend()
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。

#不同年龄下的平均生存率

fig,ax=plt.subplots(1,1,figsize=(18,4))
train_data["Age_int"]=train_data["Age"].astype(int)
average_age=train_data[["Age_int","Survived"]].groupby(["Age_int"],as_index=False).mean()
sns.barplot(x="Age_int",y="Survived",data=average_age)
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
#年龄分组生存率


bins=[12,18,30,65,80]
train_data["Age_group"]=pd.cut(train_data["Age"],bins)
by_Age=train_data.groupby("Age_group")["Survived"].mean()
print(by_Age)
by_Age.plot.pie(startangle=90,autopct="%1.2f%%",pctdistance=0.5,labeldistance=1.2)
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
#3.4 称呼与存活与否的关系 Name

train_data['Title'] = train_data['Name'].str.extract(' ([A-Za-z]+)\.',expand=False)
print(pd.crosstab(train_data['Title'],train_data['Sex']))
ave=train_data[['Title','Survived']].groupby('Title').mean().plot.barh()
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。

fig, axis1 = plt.subplots(1,1,figsize=(18,4))
train_data['Name_length'] = train_data['Name'].apply(len)
name_length = train_data[['Name_length','Survived']].groupby('Name_length', as_index=False).mean()
sns.barplot(x='Name_length', y='Survived',data=name_length)
plt.show() #???,名字长的容易活下来?????
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
# 将数据分为有兄弟姐妹和没有兄弟姐妹的两组:

sibsp_df = train_data[train_data['SibSp'] != 0]
no_sibsp_df = train_data[train_data['SibSp'] == 0]

plt.figure(figsize=(11, 5))
plt.subplot(121)
sibsp_df['Survived'].value_counts().plot.pie(labels=['No Survived', 'Survived'], autopct='%1.1f%%')
plt.xlabel('sibsp')

plt.subplot(122)
no_sibsp_df['Survived'].value_counts().plot.pie(labels=['No Survived', 'Survived'], autopct='%1.1f%%')
plt.xlabel('no_sibsp')

plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
#家庭人数和存活率

train_data['Family_Size'] = train_data['Parch'] + train_data['SibSp']+1
train_data[['Family_Size','Survived']].groupby(['Family_Size']).mean().plot.bar()
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。
#票价、船舱

train_data.boxplot(column='Fare', by='Pclass', showfliers=False)
plt.show()
#学习日记:存活率影响因素分析——阿里云天池,泰坦尼克号乘客获救预测。