数据分析案例实战:泰坦尼克船员获救

2023-10-14 13:50

本文主要是介绍数据分析案例实战:泰坦尼克船员获救,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学习唐宇迪《python数据分析与机器学习实战》视频

一、数据分析

可以看到有些数据是字符串形式,有些是数值形式,有数据缺失。

survived:1代表存活,0代表死亡,目标tag。
Pclass:船舱,较重要。
sex:性别,较重要。
age:年龄,有缺失值,较重要。
SibSp:兄弟姐妹。Parch:父母孩子。
Ticket:票。
Fare:船票价格,特征可能与船舱特征重复,也较重要。
Cabin:缺失值很多,先忽略。
Embarked:上船港口的地点,有三个取值,需要转换成数值。

 

二、数据预处理

1.读入数据“titanic_train.csv”

import pandas as pd
#读入数据
titanic = pd.read_csv('titanic_train.csv')
titanic.head()

2.使用.describe()函数查看数据一些统计特征

#查看数据的一些统计特征
#count:个数,mean:均值 std:标准差 min:最小值  max:最大值
#25%:数值从小到大排列位于25%的数 50%: 75%:
print(titanic.describe())
#Age中存在缺失值
#利用pandas的.fillna函数对缺失值进行填充,使用.median()平均值填充
titanic['Age']=titanic['Age'].fillna(titanic['Age'].median())

3.将一些字符串特征属性转换成数值

#将特征的字符串转换成数值
print(titanic['Sex'].unique())
#用0代表male 1代表female
titanic.loc[titanic['Sex']=='male','Sex']=0
titanic.loc[titanic['Sex']=='female','Sex']=1print(titanic['Embarked'].unique())
#有缺失值,以出现次数最多的‘S’进行填充 再转换成数值
titanic['Embarked']=titanic['Embarked'].fillna('S')
titanic.loc[titanic['Embarked']=='S','Embarked']=0
titanic.loc[titanic['Embarked']=='C','Embarked']=1
titanic.loc[titanic['Embarked']=='Q','Embarked']=2

4.对测试集数据进行处理

#对测试集数据进行处理
titanic_test=pd.read_csv('test.csv')
#print(titanic_test.describe())
titanic_test['Age']=titanic_test['Age'].fillna(titanic['Age'].median())
titanic_test['Fare']=titanic_test['Fare'].fillna(titanic_test['Fare'].median())titanic_test.loc[titanic_test['Sex']=='male','Sex']=0
titanic_test.loc[titanic_test['Sex']=='female','Sex']=1titanic_test['Embarked']=titanic_test['Embarked'].fillna('S')
titanic_test.loc[titanic_test['Embarked']=='S','Embarked']=0
titanic_test.loc[titanic_test['Embarked']=='C','Embarked']=1
titanic_test.loc[titanic_test['Embarked']=='Q','Embarked']=2

 

三、模型训练

1.sklearn线性回归算法

#使用sklearn线性回归算法进行分类
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold#选择使用的特征
predictors=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']
#初始化模型对象
alg=LinearRegression()
#将m个样本分成3组
kf=KFold(3,random_state=1)predictions=[]
for train,test in kf.split(titanic):#print('train=',train) 输出的是索引值#print('test=',test)  输出的是索引值#kf.split()得到#训练集train(2份)和验证集test(1份)都是划分后的索引train_predictors=(titanic[predictors].iloc[train,:])#.iloc[train,:]通过训练集的索引,提取样本特征数据。train_target=titanic['Survived'].iloc[train]#.iloc[train]通过训练集的索引,提取样本标签。#训练模型alg.fit(train_predictors,train_target)#用验证集进行验证,得到预测结果test_predictions=alg.predict(titanic[predictors].iloc[test,:])predictions.append(test_predictions)import numpy as np
#np.concatenate拼接三组数据
predictions=np.concatenate(predictions,axis=0)
predictions[predictions>0.5]=1
predictions[predictions<0.5]=0
print(len(predictions[predictions==titanic['Survived']]))
accuracy=len(predictions[predictions==titanic['Survived']])/len(predictions)
print(accuracy)

2.sklearn逻辑回归算法

cross_val_score、cross_validate二者均用于交叉验证,返回值就是scores(每次交叉验证的得分,list形式)。

cross_validate和cross_val_score的区别在于以下两点

允许传入多个metrics作为评价指标它的返回值是个dict而非list,内容包括5类:test_score, train_score, fit_time, score_times, training scores ,(fitted) estimators,它们分别是:验证集得分、训练集得分、每一折cv的estimator fit训练集的时间、每一折cv的estimator对验证集打分的时间、每一折cv的estimator对象。

#使用sklearn逻辑回归算法
from sklearn.model_selection import cross_validate
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression#使用逻辑回归交叉验证
alg=LogisticRegression(random_state=1)
cv_results=cross_validate(alg,titanic[predictors],titanic['Survived'],cv=3)
print(cv_results['test_score'].mean())scores = cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=3)
print(scores.mean())

3.使用随机森林算法进行分类

    样本是随机的,有放回的取样
    特征的选择也是随机的,防止过拟合
    多颗决策树,取平均值

from sklearn.model_selection import cross_validate
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifierpredictors=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']
#random_state=1设置随机种子使代码多次运行得到的随机值都是一样的
#n_estimators: 也就是弱学习器的最大迭代次数,或者说最大的弱学习器的个数。
#内部节点再划分所需最小样本数min_samples_split: 这个值限制了子树继续划分的条件,如果某节点的样本数少于min_samples_split,则不会继续再尝试选择最优特征来进行划分。
#叶子节点最少样本数min_samples_leaf: 这个值限制了叶子节点最少的样本数,如果某叶子节点数目小于样本数,则会和兄弟节点一起被剪枝。
alg=RandomForestClassifier(random_state=1,n_estimators=10,min_samples_split=2,min_samples_leaf=1)
kf=KFold(3,random_state=1)
cv_results=cross_validate(alg,titanic[predictors],titanic['Survived'],cv=kf)
print(cv_results['test_score'].mean())

#更改RF的参数试试
alg=RandomForestClassifier(random_state=1,n_estimators=100,min_samples_split=4,min_samples_leaf=2)
kf=KFold(3,random_state=1)
cv_results=cross_validate(alg,titanic[predictors],titanic['Survived'],cv=kf)
print(cv_results['test_score'].mean())

4.提取新特征

#SibSp是兄弟姐妹数量,Parch是老人小孩數量,可以得到家庭人数
titanic['FamilySize']=titanic['SibSp']+titanic['Parch']
#名字长度……
titanic['NameLength']=titanic['Name'].apply(lambda x:len(x))import redef get_title(name):#正则表达式:(+)代表匹配一一个或多个,\代表转义#总的来说就是匹配带.号的称谓并且前面至少有一个字母title_search=re.search('([A-Za-z]+)\.',name)if title_search:#返回匹配到的元組,group(1)代表返回匹配到的第一个()里的內容return title_search.group(1)return ''
titles=titanic['Name'].apply(get_title)
print(pd.value_counts(titles))
#用数字来代替不同的称谓
title_mapping={'Mr':1,'Miss':2,'Mrs':3,'Master':4,'Dr':5,'Rev':6, "Major": 7, "Col": 7, "Mlle": 8, "Mme": 8, "Don": 9, "Lady": 10, "Countess": 10, "Jonkheer": 10, "Sir": 9, "Capt": 7, "Ms": 2}
for k,v in title_mapping.items():titles[titles==k]=vprint(pd.value_counts(titles))
titanic['Title']=titles

5.选择特征

#利用 feature_selection 对指定特征进行进一步的选择,找出影响较大的特征
import numpy as np
from sklearn.feature_selection import SelectKBest,f_classif
import matplotlib.pyplot as plt
#指定一下特征
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked", "FamilySize", "Title", "NameLength"]
#选择特性
selector=SelectKBest(f_classif,k=5)
selector.fit(titanic[predictors],titanic['Survived'])
scores=-np.log10(selector.pvalues_)
#利用直方图显示不同特征的重要性
plt.bar(range(len(predictors)),scores)
plt.xticks(range(len(predictors)),predictors,rotation='vertical')
plt.show()#选择出几个重要的特征,使用RF重新进行计算
predictors=['Pclass','Sex','Fare','Title']
alg=RandomForestClassifier(random_state=1,n_estimators=50,min_samples_split=8,min_samples_leaf=4)
kf=KFold(3,random_state=1)
cv_results=cross_validate(alg,titanic[predictors],titanic['Survived'],cv=kf)
print(cv_results['test_score'].mean())

6.集成多个分类器方法

#竞赛中常用的耍赖的方法:集成多种算法,取平均值
from sklearn.ensemble import GradientBoostingClassifier
import numpy as np
#GradientBoostingClassifier也是一种随机森林算法,可以集成多个弱分类器,然后变成强分类器
algorithms=[[GradientBoostingClassifier(random_state=1,n_estimators=25,max_depth=3), ["Pclass", "Sex", "Age", "Fare", "Embarked", "FamilySize", "Title"]],[LogisticRegression(random_state=1), ["Pclass", "Sex", "Fare", "FamilySize", "Title", "Age", "Embarked"]]
]kf=KFold(3,random_state=1)predictions=[]
for train,test in kf.split(titanic):train_target=titanic['Survived'].iloc[train]full_test_predictions=[]for alg,predictors in algorithms:alg.fit(titanic[predictors].iloc[train,:],train_target)test_predictions=alg.predict_proba(titanic[predictors].iloc[test,:].astype(float))[:,1]full_test_predictions.append(test_predictions)test_predictions=(full_test_predictions[0]+full_test_predictions[1])/2test_predictions[test_predictions<=0.5]=0test_predictions[test_predictions>0.5]=1predictions.append(test_predictions)predictions=np.concatenate(predictions,axis=0)
accuracy=len(predictions[predictions==titanic['Survived']])/len(predictions)
print(accuracy)

 

四、预测

将新的特征加入测试集数据,预测测试集中每个人的获救几率

titles=titanic_test['Name'].apply(get_title)
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Dr": 5, "Rev": 6, "Major": 7, "Col": 7, "Mlle": 8, "Mme": 8, "Don": 9, "Lady": 10, "Countess": 10, "Jonkheer": 10, "Sir": 9, "Capt": 7, "Ms": 2, "Dona": 10}
for k,v in title_mapping.items():titles[titles==k]=v
titanic_test['Title']=titles
print(pd.value_counts(titanic_test['Title']))
titanic_test['FamilySize']=titanic_test['SibSp']+titanic_test['Parch']

predictors = ["Pclass", "Sex", "Age", "Fare", "Embarked", "FamilySize", "Title"]algorithms = [[GradientBoostingClassifier(random_state=1, n_estimators=25, max_depth=3), predictors],[LogisticRegression(random_state=1), ["Pclass", "Sex", "Fare", "FamilySize", "Title", "Age", "Embarked"]]
]full_predictions = []
for alg, predictors in algorithms:# Fit the algorithm using the full training data.alg.fit(titanic[predictors], titanic["Survived"])# Predict using the test dataset.  We have to convert all the columns to floats to avoid an error.predictions = alg.predict_proba(titanic_test[predictors].astype(float))[:,1]full_predictions.append(predictions)# The gradient boosting classifier generates better predictions, so we weight it higher.
predictions = (full_predictions[0] * 3 + full_predictions[1]) / 4
predictions

这篇关于数据分析案例实战:泰坦尼克船员获救的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/210945

相关文章

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C