教女朋友学数据挖掘——泰坦尼克号获救预测( 三 )


data['Initial']=0for i in data:data['Initial']=data.Name.str.extract('([A-Za-z]+)\.')
好了,这里我们使用正则表达式:[A-Za-z] +)来提取信息
pd.crosstab(data.Initial,data.Sex).T.style.background_gradient(cmap='summer_r') #Checking the Initials with the Sex
Sex
182
125
male
40
517
data['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True)
data.groupby('Initial')['Age'].mean() #lets check the average age by Initials
InitialMaster4.574167Miss21.860000Mr32.739609Mrs35.981818Other45.888889Name: Age, dtype: float64
填充缺失值
## 使用每组的均值来进行填充data.loc[(data.Age.isnull())&(data.Initial=='Mr'),'Age']=33data.loc[(data.Age.isnull())&(data.Initial=='Mrs'),'Age']=36data.loc[(data.Age.isnull())&(data.Initial=='Master'),'Age']=5data.loc[(data.Age.isnull())&(data.Initial=='Miss'),'Age']=22data.loc[(data.Age.isnull())&(data.Initial=='Other'),'Age']=46
data.Age.isnull().any() #看看填充完了咋样
False
4.7 填充后年龄与存活率关系
f,ax=plt.subplots(1,2,figsize=(20,10))data[data['Survived']==0].Age.plot.hist(ax=ax[0],bins=20,edgecolor='black',color='red')ax[0].set_title('Survived= 0')x1=list(range(0,85,5))ax[0].set_xticks(x1)data[data['Survived']==1].Age.plot.hist(ax=ax[1],color='green',bins=20,edgecolor='black')ax[1].set_title('Survived= 1')x2=list(range(0,85,5))ax[1].set_xticks(x2)plt.show()
观察:
1)幼儿(年龄在5岁以下)获救的还是蛮多的(妇女和儿童优先政策) 。
2)最老的乘客得救了(80岁) 。
3)死亡人数最高的是30-40岁年龄组 。
sns.factorplot('Pclass','Survived',col='Initial',data=http://www.kingceram.com/post/data)plt.show()
因此,无论性别如何,妇女和儿童第一政策都是正确的 。
4.8 登船地点与存活率关系
–> 登船地点
pd.crosstab([data.Embarked,data.Pclass],[data.Sex,data.Survived],margins=True).style.background_gradient(cmap='summer_r')
C1
42
25
17
85
17
15
33
10
66
Q1
24
36
72
S1
46
51
28
127
61
82
15
164
55
33
231
34
353
All
81
231
468
109
889
sns.factorplot('Embarked','Survived',data=http://www.kingceram.com/post/data)fig=plt.gcf()fig.set_size_inches(5,3)plt.show()
C港生存的可能性最高在0.55左右,而S的生存率最低 。
f,ax=plt.subplots(2,2,figsize=(20,15))sns.countplot('Embarked',data=http://www.kingceram.com/post/data,ax=ax[0,0])ax[0,0].set_title('No. Of Passengers Boarded')sns.countplot('Embarked',hue='Sex',data=data,ax=ax[0,1])ax[0,1].set_title('Male-Female Split for Embarked')sns.countplot('Embarked',hue='Survived',data=data,ax=ax[1,0])ax[1,0].set_title('Embarked vs Survived')sns.countplot('Embarked',hue='Pclass',data=data,ax=ax[1,1])ax[1,1].set_title('Embarked vs Pclass')plt.subplots_adjust(wspace=0.2,hspace=0.5)plt.show()