决策树
决策树简介
决策树思想的来源非常朴素,程序设计中的条件分支结构就是if-then结构,最早的决策树就是利用这类结构分割数据的一种分类学习方法
你如何去划分是否能得到贷款?
ID3
信息增益 最大的准则
C4.5
信息增益比 最大的准则
CART
回归树: 平方误差 最小
分类树: 基尼系数 最小的准则 在sklearn中可以选择划分的原则
决策树API
class sklearn.tree.DecisionTreeClassifier(criterion=’gini’, max_depth=None,random_state=None)
决策树分类器
criterion:默认是’gini’系数,也可以选择信息增益的熵’entropy’
max_depth:树的深度大小
random_state:随机数种子
method:
decision_path:返回决策树的路径
代码分析
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.tree import DecisionTreeClassifier, export_graphviz
def decision():
"""
决策树对泰坦尼克号进行预测生死
:return:
"""
# 获取数据
data = pd.read_csv("titanic_data.csv")
# print(data.head())
# 处理数据,找出特征值和目标值
x = data[["Pclass","Age","Sex"]]
y = data["Survived"]
# print(x)
print(y)
# 缺失值处理
x['Age'].fillna(x['Age'].mean(),inplace=True)
# 分割数据集到训练集和测试集
x_train,x_test,y_train,y_test = train_test_split(x,y,test_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"))
print(dict.get_feature_names())
print(x_train)
# 用决策树进行预测
dec = DecisionTreeClassifier(max_depth=4)
dec.fit(x_train,y_train)
print(dec.score(x_test,y_test))
# 导出决策树的结构
export_graphviz(dec,out_file="tree.dot",feature_names=['Age', 'Pclass', 'Sex=female', 'Sex=male'])
return None
随机森林
定义:在机器学习中,随机森林是一个包含多个决策树的分类器,并且其输出的类别是由个别树输出的类别的众数而定。
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.tree import DecisionTreeClassifier, export_graphviz
def decision():
"""
决策树对泰坦尼克号进行预测生死
:return:
"""
# 获取数据
data = pd.read_csv("titanic_data.csv")
# print(data.head())
# 处理数据,找出特征值和目标值
x = data[["Pclass","Age","Sex"]]
y = data["Survived"]
# print(x)
print(y)
# 缺失值处理
x['Age'].fillna(x['Age'].mean(),inplace=True)
# 分割数据集到训练集和测试集
x_train,x_test,y_train,y_test = train_test_split(x,y,test_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"))
print(dict.get_feature_names())
print(x_train)
# 用决策树进行预测
dec = DecisionTreeClassifier(max_depth=4)
dec.fit(x_train,y_train)
print(dec.score(x_test,y_test))
# 导出决策树的结构
export_graphviz(dec,out_file="tree.dot",feature_names=['Age', 'Pclass', 'Sex=female', 'Sex=male'])
return None
def randonf():
"""
决策树对泰坦尼克号进行预测生死
:return:
"""
# 获取数据
data = pd.read_csv("titanic_data.csv")
# print(data.head())
# 处理数据,找出特征值和目标值
x = data[["Pclass", "Age", "Sex"]]
y = data["Survived"]
# print(x)
print(y)
# 缺失值处理
x['Age'].fillna(x['Age'].mean(), inplace=True)
# 分割数据集到训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_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"))
# 随机森林进行预测(超参数调优)
rf = RandomForestClassifier()
param = {"n_estimators":[120,200,300,500,800,1200],"max_depth":[5,8,15,25,30]}
# 网格搜索与交叉验证
gc = GridSearchCV(rf,param_grid=param,cv=2)
gc.fit(x_train,y_train)
print("准确率",gc.score(x_test,y_test))
print("查看选择的参数模型",gc.best_params_)
# decision()
randonf()