#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 18 10:40:45 2019
@author: liyili2
"""
# =============================================================================
#
# 1.整体来看,存活/整体比例是多少?
# 2.各年龄段存活情况
# 3.老人、小孩存活情况
# 4.男性、女性存活情况
# 5.各仓位存活情况
# 6.有/无兄弟姐妹存活情况
# 7.有/无父母/子女存活情况
# 8.票价存活情况
#
# =============================================================================
# =============================================================================
# 目的:通过已知获救数据,预测乘客生存情况
#
# 研究问题:
# 1、整体来看,存活比例如何?
# 要求:
# ① 读取已知生存数据train.csv
# ② 查看已知存活数据中,存活比例如何?
# 提示:
# ① 注意过程中筛选掉缺失值之后再分析
# ② 这里用seaborn制图辅助研究
#
# 2、结合性别和年龄数据,分析幸存下来的人是哪些人?
# 要求:
# ① 年龄数据的分布情况
# ② 男性和女性存活情况
# ③ 老人和小孩存活情况
#
# 3、结合 SibSp、Parch字段,研究亲人多少与存活的关系
# 要求:
# ① 有无兄弟姐妹/父母子女和存活与否的关系
# ② 亲戚多少与存活与否的关系
#
# 4、结合票的费用情况,研究票价和存活与否的关系
# 要求:
# ① 票价分布和存活与否的关系
# ② 比较研究生还者和未生还者的票价情况
#
# 5、利用KNN分类模型,对结果进行预测
# 要求:
# ① 模型训练字段:'Survived','Pclass','Sex','Age','Fare','Family_Size'
# ② 模型预测test.csv样本数据的生还率
# 提示:
# ① 训练数据集中,性别改为数字表示 → 1代表男性,0代表女性
# =============================================================================
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
from bokeh.plotting import figure,show,output_file
from bokeh.models import ColumnDataSource
from bokeh.models import HoverTool
from bokeh.palettes import brewer
from bokeh.models.annotations import BoxAnnotation
from bokeh.layouts import gridplot
import os
os.chdir('/Users/liyili2/Downloads/datas/python_wei/03/07-tatanic/')
train_data=pd.read_csv('train.csv')
test_data=pd.read_csv('test.csv')
#1.整体存活情况:
live_rate=train_data['Survived'].value_counts()
live_rate.plot.pie(autopct='%1.2f%%')
#2.年龄分布情况:
train_age=train_data[train_data['Age'].notnull()]
fig, axes = plt.subplots(1)
axes.hist(train_age['Age'],bins=70)
#3.男性女性存活情况
#select Sex,count(Survived) from train_data
#groupby Sex
train_Sex=train_data.groupby(['Sex','Survived'])['Survived'].count()
#历史遗留大问题
#结果:
#Sex Survived
#female 0 81
# 1 233
#male 0 468
# 1 109
#4.老人和小孩存活情况
#柱状图
#select age, count(survived) from train_data
#group by age
#where suivived=1
plt.figure(figsize=(18,4))
train_age['Age_int'] = train_age['Age'].astype(int)
average_age = train_age[["Age_int", "Survived"]].groupby(['Age_int'],as_index=False).mean()
sns.barplot(x='Age_int',y='Survived',data=average_age, palette = 'BuPu')
plt.grid(linestyle = '--',alpha = 0.5)
print('灾难中,老人和小孩存活率较高')
# 筛选出有无兄弟姐妹数据
sibsp_df=train_data[train_data['SibSp'] != 0]
np_sibsp_df=train_data[train_data['SibSp'] == 0]
#有兄弟姐妹
# =============================================================================
# select sibsp,count(Survived) from train_data
# group by Survived
#where sibsp!=0
# =============================================================================
plt.figure(figsize=(12,3))
plt.subplot(141)
plt.axis('equal')
sibsp_df['Survived'].value_counts().plot.pie(labels=['No Survived','Survived'],autopct= '%1.1f%%',colormap = 'Blues')
plt.xlabel('sibsp')
#无兄弟姐妹
# =============================================================================
# select sibsp,count(Survived) from train_data
# group by Survived
#where sibsp=0
# =============================================================================
plt.subplot(142)
plt.axis('equal')
np_sibsp_df['Survived'].value_counts().plot.pie(labels=['No Survived','Survived'],autopct= '%1.1f%%',colormap = 'Blues')
plt.xlabel('no_sibsp')
# 筛选出有无父母子女数据
parch_df = train_data[train_data['Parch'] != 0]
no_parch_df = train_data[train_data['Parch'] == 0]
#有父母/子女
# =============================================================================
# select parch,count(survived)
#from Train_data
#where parch!=0
# =============================================================================
plt.subplot(143)
plt.axis('equal')
parch_df['Survived'].value_counts().plot.pie(labels=['No Survived', 'Survived'], autopct= '%1.1f%%',colormap = 'Reds')
plt.xlabel('parch')
#无父母/子女
# =============================================================================
# select parch,count(survived)
#from Train_data
#where parch=0
# =============================================================================
plt.subplot(144)
plt.axis('equal')
no_parch_df['Survived'].value_counts().plot.pie(labels=['No Survived', 'Survived'], autopct = '%1.1f%%',colormap = 'Reds')
plt.xlabel('no_parch')
#我觉得因素好多,需要进行双样本T检验,看是否有显著性差异。
print('有兄弟姐妹、父母子女的乘客存活率更大')
# =============================================================================
# 不同年龄层有无父母、兄弟的存活情况
# select age,sibsp,Parch,Survived,count(Survived) from
# train_data
# group by age,sibsp,Parch,Survived
#
# def age_range():
# if age>=0 and age<14:
# age.repalce("")
# =============================================================================
# 亲戚多少与存活与否的关系
# 查看兄弟姐妹个数与存活率
# =============================================================================
# select Parch,avg(Survived)
#from (select Parch,Survived from train_data)as a
# group by Parch
# =============================================================================
fig, ax=plt.subplots(1,2,figsize=(15,4))
train_data[['Parch','Survived']].groupby(['Parch']).mean().plot.bar(ax=ax[0])
ax[0].set_title('Parch and Survived')
# 查看父母子女个数与存活率
# =============================================================================
# select Family_Size,avg(Survived)
#from (select (Parch+SibSp+1) as Family_Size,Survived from train_data)as a
#group by Family_Size
# =============================================================================
train_data['Family_Size'] = train_data['Parch'] + train_data['SibSp']+1
train_data[['Family_Size','Survived']].groupby(['Family_Size']).mean().plot.bar(figsize = (15,4))
print('若独自一人,那么其存活率比较低;但是如果亲友太多的话,存活率也会很低')
'''
4、结合票的费用情况,研究票价和存活与否的关系
要求:
① 票价分布和存活与否的关系
② 比较研究生还者和未生还者的票价情况
'''
# 查看票价分布情况
fig, ax=plt.subplots(1,2,figsize=(15,4))
train_data['Fare'].hist(bins=70, ax = ax[0])
train_data.boxplot(column='Fare', by='Pclass', showfliers=False,ax = ax[1])
# =============================================================================
# select Fare as fare_not_survived from train_data
# where Survived=0
# select Fare as fare_survived from train_data
# where Survived=1
# =============================================================================
fare_not_survived = train_data['Fare'][train_data['Survived'] == 0]
fare_survived = train_data['Fare'][train_data['Survived'] == 1]
# 基于票价,筛选出生存与否的数据
average_fare = pd.DataFrame([fare_not_survived.mean(),fare_survived.mean()])
std_fare = pd.DataFrame([fare_not_survived.std(),fare_survived.std()])
average_fare.plot(yerr=std_fare,kind='bar',legend=False,figsize = (15,4),grid = True)
# 查看票价与是否生还的关系
print('生还者的平均票价要大于未生还者的平均票价')
'''
5、利用KNN分类模型,对结果进行预测
要求:
① 模型训练字段:'Survived','Pclass','Sex','Age','Fare','Family_Size'
② 模型预测test.csv样本数据的生还率
提示:
① 训练数据集中,性别改为数字表示 → 1代表男性,0代表女性
'''
# 数据清洗,提取训练字段
knn_train = train_data[['Survived','Pclass','Sex','Age','Fare','Family_Size']].dropna()
knn_train['Sex'][knn_train['Sex'] == 'male'] = 1
knn_train['Sex'][knn_train['Sex'] == 'female'] = 0
test_data['Family_Size'] = test_data['Parch'] + test_data['SibSp']+1
knn_test = test_data[['Pclass','Sex','Age','Fare','Family_Size']].dropna()
knn_test['Sex'][knn_test['Sex'] == 'male'] = 1
knn_test['Sex'][knn_test['Sex'] == 'female'] = 0
print('清洗后训练集样本数据量为%i个' % len(knn_train))
knn_train.head()
print('清洗后测试集样本数据量为%i个' % len(knn_test))
knn_test.head()
# 模型预测test.csv样本数据的生还率
from sklearn import neighbors
# 导入KNN分类模块
knn = neighbors.KNeighborsClassifier()
knn.fit(knn_train[['Pclass','Sex','Age','Fare','Family_Size']], knn_train['Survived'])
# 构建模型
knn_test['predict'] = knn.predict(knn_test)
pre_survived = knn_test[knn_test['predict'] == 1].reset_index()
del pre_survived['index']
# 预测存货情况
print('finished!')
pre_survived
![image.png](https://img.haomeiwen.com/i13726474/d83a9fc6dcf3a22e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
网友评论