博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
泰坦尼克乘客存活状况(决策树案例)
阅读量:7106 次
发布时间:2019-06-28

本文共 2543 字,大约阅读时间需要 8 分钟。

1912年4月15日凌晨2点20分,“永不沉没”的“泰坦尼克”走完了它短暂的航程,缓缓沉入大西洋这座安静冰冷的坟墓。

Titanix

欢迎你们说我幼稚荒诞,也欢迎你们继续成熟苍凉。说起来,titanic是我至今觉得最为美妙的爱情电影,如饮蜜酒,甘不可言。这是一份绚烂到极致,使得人类的大难做了背景,还妄想突破时间和生死直达永恒的爱情。露丝从救生船上一跃而起,扑到窗边的一刹,因了这份勇敢和贪求,最为美丽。在有生的瞬间能遇到你,竟花光所有运气。

you're going to go on and you're going to make babies and watch them grow and you're going to die an old lady.

你将长寿,子孙满堂


乘客存活数据:

这里用决策树算法,按照乘客的社会阶层(pclass),年龄(age), 性别(sex)三个因素,来预测乘客最终的生存状况(survived)

import pandas as pdfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_extraction import  DictVectorizerfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.tree import export_graphvizfrom sklearn.ensemble import RandomForestClassifierdef descsion():    # 获取数据, 提取特征值和目标值    Titanic_data = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt")    # 打印字段名    print(Titanic_data.columns)    # 分割出特定的字段(社会阶层, 年龄, 性别)对生存率的影响    titanic_x = Titanic_data[["pclass", "age", "sex"]]    titanic_y = Titanic_data[["survived"]]    # 处理缺失值    titanic_x["age"].fillna(titanic_x['age'].mean(), inplace=True)    # 进行数据的分割    x_train, x_test, y_train, y_test = train_test_split(titanic_x, titanic_y, train_size=0.25)    # 对特征们进行字典特征抽取    dict = DictVectorizer(sparse=False)    x_train = dict.fit_transform(x_train.to_dict(orient="records"))    x_test = dict.transform(x_test.to_dict(orient="records"))    # 查看抽取后特征的名字    feature_names = dict.get_feature_names()    print(feature_names)    # 进行决策树预测(可选:限制决策树最大深度为10)    my_decision_tree = DecisionTreeClassifier(max_depth=10)    my_decision_tree.fit(x_train, y_train)    print("单棵决策树预测的准确率为:", my_decision_tree.score(x_test, y_test))    # 将树的结构保存到本地    export_graphviz(my_decision_tree, "./my_decision_tree.dot", feature_names = feature_names)    """    将dot文件装换为png的方法    在本机安装graphviz ubuntu版安装: sudo apt install graphviz   mac版安装: brew install graphviz    然后运行命令: dot -Tpng my_decision_tree.dot -o my_decision_tree.png    生成png格式图片my_desion_tree.png    """    # 随机树森林算法, 建立20棵数, 树的最大深度为15    rf = RandomForestClassifier(n_estimators=21, max_depth=20)    rf.fit(x_train, y_train)    print("随机数森林预测的准确率为:", rf.score(x_test, y_test))if __name__ == '__main__':    descsion()
运行结果

使用graphviz绘制决策树

1. 安装graphviz

  • ubuntu安装方式:
sudo apt install graphviz
  • mac安装方式
brew install graphviz

2. 通过终端,在.dot所在的目录运行命令,将.dot转换为png图片

dot -Tpng my_decision_tree.dot -o my_decision_tree.png
my_decision_tree

那些古板的绅士们要死得很体面。女士和儿童先上,男人们等待死亡。船上的乐队,从容演奏到了最后一刻。谁能告诉我,身边是世界末日的惊恐,但依然安静地演奏,是因为拥有了什么样的力量? “很高兴今晚和你们合作。”想起另外一部电影的一句台词:“假装我们明天还会再见。”生离死别,说了再见,但是没有明天。

转载地址:http://qujhl.baihongyu.com/

你可能感兴趣的文章
数据库数据恢复实验过程
查看>>
RIP理论知识
查看>>
清空memcached中缓存的数据的方法
查看>>
jsp通过include指令引入html乱码的解决方法
查看>>
中文传值火狐、google浏览器无问题,但IE有乱码问题的解决
查看>>
源码编译安装httpd
查看>>
MySQL基础操作
查看>>
知物由学 | 你真的了解网络安全吗?
查看>>
【董天一】IPFS vs Filecoin: 开发者该如何选择
查看>>
在 CentOS 7 上搭建 KVM 虚拟化平台和基础管理
查看>>
ios逆向之:dump应用的头文件
查看>>
解决mysql:The server quit without updating PID file
查看>>
网络实验要求
查看>>
linux7 ntp 开机不自动启动
查看>>
做一次面向对象的体操:将 JSON 字符串转换为嵌套对象的一种方法
查看>>
高可用Redis服务架构分析与搭建
查看>>
论JVM爆炸的几种姿势及自救方法
查看>>
Tomcat远程debug
查看>>
Java的BIO,NIO和AIO的区别于演进
查看>>
核心标签库-------------------二
查看>>