利用CART分类器进行预测
读取数据
import pandas as pd
data =pd.read_csv("train.csv")
查看数据
# 显示前五行
data.head()
# 显示行数和列数
data.shape
# 显示所有列的数据类型等信息
data.info()
# 显示类别Embarked特征列的所有取值及出现次数
data.Embarked.value_counts()
三、数据处理
1、缺失值处理
在查看数据发现特征Age和Embarked有缺失值
特征Age使用其均值补充
特征Embarked使用众数“S”进行补充
2、特征编码转换
大部分模型只能处理数值型数据
使用将非数值型转换可计算的编码
采用特征编码转换
生成N个二值特征列(取值0或1),每个对应一种取值
使用决策树模型,一般无须对特征进行缩放
# 缺失值处理
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 =pd.read_csv("train.csv")
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))