# 泰坦尼克乘客存活状况(决策树案例)

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

Titanix

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 pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import  DictVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from sklearn.ensemble import RandomForestClassifier

def descsion():
# 获取数据, 提取特征值和目标值

# 打印字段名
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