# 机器学习——CART决策树——泰坦尼克还生还预测

``````import pandas as pd

``````# 显示前五行

# 显示行数和列数
data.shape

# 显示所有列的数据类型等信息
data.info()
``````

``````# 显示类别Embarked特征列的所有取值及出现次数
data.Embarked.value_counts()``````

## 三、数据处理

1、缺失值处理

2、特征编码转换

``````# 缺失值处理
data.Age.fillna(data.Age.median(),inplace=True)
data.Embarked.fillna('S',inplace=True)

# 特征编码转换
data.Sex=data.Sex.map({'female':0,'male':1})
embarked_d=pd.get_dummies(data.Embarked,prefix='Embarked',drop_first=True)
data=pd.concat([data,embarked_d],axis=1)

# 将处理好的数据放入
feature_cols=['Pclass','Sex','Age','Embarked_Q','Embarked_S']
X=data[feature_cols]
y=data.Survived``````

## 四、训练和选择模型

``````from sklearn.tree import DecisionTreeClassifier
treeclf = DecisionTreeClassifier(max_depth=3,random_state=1)

treeclf.fit(X,y)``````

## 五、可视化决策树

``````import graphviz
from sklearn import tree
from graphviz import Digraph
dot_data = tree.export_graphviz(treeclf,out_file=None,feature_names=feature_cols,class_names='Survived',filled=True,rounded=True,special_characters=True)
graph=graphviz.Source(dot_data)
graph.render('xgboost1') #输出pdf文件
graph
``````

## 六、查看特征的重要性

``pd.DataFrame({'feature':feature_cols,'importance':treeclf.feature_importances_})``

## 七、模型评估

``````from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, random_state=4)

from sklearn.model_selection import GridSearchCV
parameters = {'max_depth':[1,3,5,10,15,20,30]}
tree_clf=GridSearchCV(DecisionTreeClassifier(),param_grid=parameters,scoring='accuracy')
tree_clf.fit(X_train,y_train)``````

``````print(tree_clf.best_params_)
print(tree_clf.best_score_)``````

``````y_pred=tree_clf.predict(X_test)

from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))``````

## 完整代码：

``````import pandas as pd

data.Age.fillna(data.Age.median(),inplace=True)
data.Embarked.fillna('S',inplace=True)

data.Sex=data.Sex.map({'female':0,'male':1})
embarked_d=pd.get_dummies(data.Embarked,prefix='Embarked',drop_first=True)
data=pd.concat([data,embarked_d],axis=1)

feature_cols=['Pclass','Sex','Age','Embarked_Q','Embarked_S']
X=data[feature_cols]
y=data.Survived

from sklearn.tree import DecisionTreeClassifier
treeclf = DecisionTreeClassifier(max_depth=3,random_state=1)
treeclf.fit(X,y)

import graphviz
from sklearn import tree
from graphviz import Digraph
dot_data = tree.export_graphviz(treeclf,out_file=None,feature_names=feature_cols,class_names='Survived',filled=True,rounded=True,special_characters=True)
graph=graphviz.Source(dot_data)
graph

pd.DataFrame({'feature':feature_cols,'importance':treeclf.feature_importances_})
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, random_state=4)

from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier

parameters = {'max_depth':[1,3,5,10,15,20,30]}
tree_clf=GridSearchCV(DecisionTreeClassifier(),param_grid=parameters,scoring='accuracy')
tree_clf.fit(X_train,y_train)

print(tree_clf.best_params_)
print(tree_clf.best_score_)

y_pred=tree_clf.predict(X_test)
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))

``````

THE END