泰坦尼克号Python数据分析环境
Spyder3
参考书籍
Python数据分析及应⽤
泰坦尼克号数据集
导⼊必要的库
import numpy as np
import pandas as pd
pandas读取数据到DataFrame
dataset = pd.read_csv("D:/fmt.csv")
lumns)# 显⽰都有哪些项
Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
dtype='object')
我们的⽬的是分析泰坦尼克号幸存者(Survived)的因素,从上⾯可以直接排除⼏个⽆关项:PassengerId 乘客ID
Name 乘客姓名
Ticket 船票
把⽆关项从dataset中去除
dataset = dataset\
.drop("PassengerId", axis=1)\
.drop("Name", axis=1)\
.drop("Ticket", axis=1)\
;
或者这样写,效果⼀样
dataset.drop("PassengerId", axis=1, inplace=True)
dataset.drop("Name", axis=1, inplace=True)
dataset.drop("Ticket", axis=1, inplace=True)
读取数据集的总代码spyder python下载
import numpy as np
import pandas as pd
def read_tiantic():
dataset = pd.read_csv("D:/fmt.csv")
dataset.drop(['PassengerId','Name','Ticket',], axis=1, inplace=True)
return dataset
Q1. 有多少⼈活下来了?⽐率是多少?
alive_sum = dataset.loc[dataset['Survived']==True,:].index.size
alive_rate = alive_sum / dataset.index.size
print("有"+str(alive_sum)+"⼈活下来了,⽐率是"+str(alive_rate))
del alive_sum
del alive_rate
Q2.幸存者的男⼥⽐例,如果你是⼥的能活下来的机率有多⼤?
下⾯是完整的代码
import numpy as np
import pandas as pd
# Q2.幸存者的男⼥⽐例,如果你是⼥的能活下来的机率有多⼤?
def survive_female():
dataset = pd.read_csv("D:/fmt.csv")
dataset = dataset\
.drop("PassengerId", axis=1)\
.drop("Name", axis=1)\
.drop("Ticket", axis=1)\
;
alive = dataset.loc[dataset['Survived']==True]
alive_man = alive.loc[alive['Sex']=='male'].index.size
alive_woman = alive.loc[alive['Sex']=='female'].index.size
sex_rate = alive_man / alive_woman
return[sex_rate, alive_man, alive_woman]
if __name__ =='__main__':
[sex_rate, alive_man, alive_woman]= survive_female()
print("活下来的⼈⾥有"+str(alive_man)+"个男⼈," \
+str(alive_woman)+"个⼥⼈,男⼥⽐例为:"+str(sex_rate))
输出
活下来的⼈⾥有109个男⼈,385个⼥⼈,男⼥⽐例为:0.2831168831168831
Q3.幸存者的Pclass(舱级别)⽣存⽐例,如果你在⼀等舱,你活下来的机率有多⼤?#### Q3.幸存者的Pclass(舱级别)⽐例,如果你在⼀等舱,你活下来的机率有多⼤?
def survive_pclass():
data = read_tiantic()
# 每个级别活下来的⼈的⽐率
l1_rate = data.query("Pclass == 1 & Survived == True").index.size / \
data.loc[data.Pclass ==1].index.size
l2_rate = data.query("Pclass == 2 & Survived == True").index.size / \
data.loc[data.Pclass ==2].index.size
l3_rate = data.query("Pclass == 3 & Survived == True").index.size / \
data.loc[data.Pclass ==3].index.size
return[l1_rate, l2_rate, l3_rate]
Q4.幸存与否是否和在哪个城市上岸(Embarked)有关?是否和年龄有关?
①地点与⽣存率
import numpy as np
import pandas as pd
data = pd.read_csv("D:/fmt.csv")
# 按照登船的地点分组
emb = upby(by="Embarked")
# 每个登陆地点的⽣存⽐率
emb_sur_rate = emb.Survived.sum()/ emb.Survived.size()
print(emb_sur_rate)
Embarked
C 0.492593
Q 0.439024
S 0.333698
Name: Survived, dtype: float64
得出结论: 是否⽣还与在哪下车⽆关
②年龄与⽣存率
# 获取到需要⽤到的数据
age = data.loc[ull(),['Age','Survived',]]
def groupby_age(index,*args,**kwargs):
""" 按照10岁⼀个年龄段划分 """
try:
if age.loc[index].Age <=10:
return'0-10'
elif age.loc[index].Age >10and age.loc[index].Age <=20:
return'11-20'
elif age.loc[index].Age >20and age.loc[index].Age <=30:
return'21-30'
elif age.loc[index].Age >30and age.loc[index].Age <=40:
return'31-40'
elif age.loc[index].Age >40and age.loc[index].Age <=50:
return'41-50'
elif age.loc[index].Age >50and age.loc[index].Age <=60:
return'51-60'
elif age.loc[index].Age >60and age.loc[index].Age <=70:
return'61-70'
elif age.loc[index].Age >70and age.loc[index].Age <=80:
return'71-80'
elif age.loc[index].Age >80and age.loc[index].Age <=90:
return'81-90'
except:
pass
# 按照年龄段分组
age_group = upby(groupby_age)
# 计算每个分组内存活的⼈数与总⼈数的⽐率
age_sur_rate = age_group.Survived.sum()/ age_group.Survived.size() print(age_sur_rate)
输出结果
0-10 0.558140
11-20 0.407407
21-30 0.357341
31-40 0.423810
41-50 0.363636
51-60 0.451613
61-70 0.259259
71-80 0.333333
Name: Survived, dtype: float64
得出结论: 是否⽣还与年龄段有关,但关系不太⼤
Q5.如果你是三等舱⼀个⼥⼈,你⽣还的⼏率有多⼤?data = pd.read_csv("D:/fmt.csv")
# 获取 level3 中 female 的⽣存表
l3_fe = data.query('Pclass == 3 & Sex == "female"').Survived
# 计算⽐率
l3_fe_sur_rate = l3_fe.sum()/len(l3_fe)
得出结果: 2/3
Q6.说明幸存的主要因素
①票价与⽣存率
先看看 票价与⼈数的关系
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv("D:/fmt.csv")
# ⼈数 - 票价
price = data.Fare
rate = pd.DataFrame({
'price': np.linspace(price.min(), price.max(),50),
'persons':0,
})
def rate_append(pri):
for i in range(rate.price.size):
if pri <= rate.price[i]:
rate.persons[i]+=1
break
for pri in price:
rate_append(pri)
plt.bar(rate.price, rate.persons,5)
⼤多数⼈的票价在100块以下
Kaggle需要,我没翻过墙,⾮常⽆语
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论