决策树

案例




决策树算法
前面四个算法都是有关联的

Hunt算法


A=∅的意思是什么
如何理解从A中选择最优划分属性a*?如何构建最优决策树
Hunt算法没有告诉我们如何选取最优属性(id3)
如何找最优切分点

ID3信息增益算法

  • ID3算法主要是针对属性选择问题
  • 使用信息增益度选择测试属性

    信息熵



    当每支队伍夺冠的可能性不等时,这条信息的信息量可能比5比特少。

    公式



    按照条件进行划分的信息熵

# -*- coding: utf-8 -*-
# @Time    : 2020/12/12 21:36
# @Author  : LUYX
import numpy as npdef createDataSet():"""outlooktemperaturehunmiditywindy"""dataSet = np.array([[0,0,0,0,'N'],[0,0,0,1,'N'],[1,0,0,0,'Y'],[2,1,0,0,'Y'],[2,2,1,0,'Y'],[2,2,1,1,'N'],[1,2,1,1,'Y']])labels = np.array(['outlook','temperature','hunmidity','windy'])return dataSet,labelsdef createTestSet():testSet = np.array([[0,1,0,0],[0,2,1,0],[2,1,1,0],[0,1,1,1],[1,1,0,1],[1,0,1,0],[2,1,0,1]])return testSetdef dataset_entropy(dataset):#算信息熵classLabel = dataset[:,-1]   #取最后一列labelcount = {}for i in range(classLabel.size):label = classLabel[i]labelcount[label] = labelcount.get(label,0) + 1ent = 0for k,v in labelcount.items():ent += -v/classLabel.size * np.log2(v/classLabel.size)return entdef splitDataSet(dataset,featureIndex,value):subdataset = []for example in dataset:if example[featureIndex] == value:subdataset.append(example)return np.delete(subdataset,featureIndex,axis = 1)def chooseBestFeature(dataset,labels):#特征的个数featureNum = labels.size#最小熵值minEntropy,bestFeatureIndex = 1,None#样本的总数n = dataset.shape[0]for i in range(featureNum):#制定特征的条件熵featureEntropy = 0#返回所有子集featureList = dataset[:,i]featureValues = set(featureList)for value in featureValues:subDataSet = splitDataSet(dataset,i,value)featureEntropy += subDataSet.shape[0]/n*dataset_entropy(subDataSet)if minEntropy > featureEntropy:minEntropy = featureEntropybestFeatureIndex = iprint(minEntropy)return bestFeatureIndexdef createTree(dataset,labels):classList = dataset[:,-1]if len(set(classList)) == 1:return dataset[:,-1][0]if labels.size == 0:return mayorClass(classList)bestFeatureIndex = chooseBestFeature(dataset,labels)bestFeature = labels[bestFeatureIndex]dtree = {bestFeature:{}}featureList = dataset[:,bestFeatureIndex]featureValues = set(featureList)for value in featureValues:subdataset = splitDataSet(dataset,bestFeatureIndex,value)sublabels = np.delete(labels,bestFeatureIndex)dtree[bestFeature][value] = createTree(subdataset,sublabels)return dtreedef predict(tree,labels,testData):rootName = list(tree.keys())[0]rootValue = tree[rootName]featureIndex = list(labels).index(rootName)classLabel = Nonefor key in rootValue.keys():if testData[featureIndex] == int(key):if type(rootValue[key]).__name__=="dict":classLabel = predict(rootValue[key],labels,testData)else:classLabel = rootValue[key]return classLabeldef predictAll(tree,labels,testSet):classLabels = []for i in testSet:classLabels.append(predict(tree,labels,i))return classLabelsif __name__ == "__main__":dataset,labels = createDataSet()# print(dataset_entropy(dataset))# print(splitdataset(dataset,0))# print(createTree(dataset,labels))tree = createTree(dataset,labels)testSet = createTestSet()print(predictAll(tree,labels,testSet))

c4.5

  • 信息增益


  • 处理缺失值


  • 处理过渡拟合

CART

CHAID决策树


  • 置信概率相关曲线

决策树总结


  • 基本原理
  • 决策树特点
  • 对数据的要求
  • 决策树的优势

    因为他能够理顺一些可以理解的规则
import os
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import tree
import pydotplus
from sklearn.metrics import confusion_matrix,roc_curve,aucfile = "data.txt"
dataset = pd.read_csv(file,encoding="gbk").values
x_train,x_test,y_train,y_test = train_test_split(dataset[:,:-1],dataset[:,-1],test_size=0.3) #参数的调节
def getCriterion():criterions = ["gini","entropy"]for criterion in criterions:model = DecisionTreeClassifier(criterion=criterion)model.fit(x_train,y_train)print(criterion," training score",model.score(x_train,y_train))print(criterion," testing score",model.score(x_test,y_test))#树的深度调节
def getDepth():max_depths = range(1,70)train_score = []test_score = []for max_depth in max_depths:model = DecisionTreeClassifier(max_depth=max_depth)model.fit(x_train, y_train)train_score.append(model.score(x_train,y_train))test_score.append(model.score(x_test,y_test))plt.plot(max_depths,train_score,label = "train",marker = "*")plt.plot(max_depths,test_score,label = "test", marker = "o")plt.xlabel("max_depth")plt.ylabel("score")plt.legend(loc="best")plt.show()#最小分裂点
def getMinSampleSplit():min_sample_split = range(10,1000,10)train_score = []test_score = []for min_samples in min_sample_split:model = DecisionTreeClassifier(min_samples_split=min_samples)model.fit(x_train, y_train)train_score.append(model.score(x_train,y_train))test_score.append(model.score(x_test,y_test))plt.plot(max_depths,train_score,label = "train",marker = "*")plt.plot(max_depths,test_score,label = "test", marker = "o")plt.xlabel("max_depth")plt.ylabel("score")plt.legend(loc="best")plt.show()#叶子节点最小数
def getMinLeaf():min_sample_leaf = range(1, 500, 10)train_score = []test_score = []for min_samples in min_sample_leaf:model = DecisionTreeClassifier(min_samples_leaf=min_samples)model.fit(x_train, y_train)train_score.append(model.score(x_train, y_train))test_score.append(model.score(x_test, y_test))plt.plot(max_depths, train_score, label="train", marker="*")plt.plot(max_depths, test_score, label="test", marker="o")plt.xlabel("max_depth")plt.ylabel("score")plt.legend(loc="best")plt.show()#输出树图形
def out_image():#模型初始化clf = DecisionTreeClassifier(max_depth=3)   #gini#训练模型clf.fit(x_train,y_train)dot_data = tree.export_grahviz(clf,out_file = None, filled = True, rounded = True)graph = pydotplus.graph_from_dot_data(dot_data)#graph.write_pdf(file.replace('.csv','.pdf'))graph.write_png(file.replace('.csv','.pdf'))#generate ROC cruve
def graph_roc():model = DecisionTreeClassifier(criterion="entropy",max_depth=5,min_samples_split=300)model.fit(x_train,y_train)#输出结果为1的概率proba1 = model.predict_proba(x_test)[:,1]df1 = pd.read_csv(file, encoding="gbk")cols = list(df1.columns)[0:-1]# cols.append("为1的概率")# df = pd.DataFrame(None,columns=cols)df_test = pd.DataFrame(x_test,columns=cols)#生成预测值和预测概率df_test.loc[:,'营销是否成功'] = y_testdf_test.loc[:,'预测为1的概率'] = proba1#如果文件不存在,则生成文件if not os.path.exists("test.csv"):df_test.to_csv("test.csv",encoding='utf-8',index=False)#输出混淆矩阵和ROC曲线
def plot_roc():#构建模型clf = DecisionTreeClassifier(max_depth=3)#训练数据clf.fit(x_train,y_train)#输出混淆矩阵pre = clf.predict(x_test)tn,fp,fn,tp = confusion_matrix(y_test,pre).ravel()#更好的输出(二分类)print('tn={0},fp={1},fn={2},tp={3}'.format(tn,fp,fn,tp))#输出预测测试集的概率y_pro_1 = clf.predict_proba(x_test)[:,1]#得到误判率,命中率,门限fpr,tpr,thresholds = roc_curve(y_test,y_pro_1)print(thresholds)#计算aucroc_auc = auc(fpr,tpr)#对ROC曲线正常显示的参数设置plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = False#绘图plt.scatter(fpr,tpr)plt.plot(fpr,tpr,'g',label = 'AUC=%0.2f'%(roc_auc))plt.title('ROC CURVE')plt.xlim([-0.05,1.05])plt.ylim([-0.05,1.05])plt.legend(loc='lower right')#绘制参考线plt.plot([0,1],[1,0],'r--')plt.ylabel('命中率')plt.xlabel('误判率')plt.show()if __name__ == "__main__":# getCriterion()# getMinSampleSplit()# getMinLeaf()# out_image()graph_roc()

《机器学习》周志华note2相关推荐

  1. 机器学习(周志华) 参考答案 第十四章 概率图模型 14.9

    机器学习(周志华西瓜书) 参考答案 总目录 http://blog.csdn.net/icefire_tyh/article/details/52064910 机器学习(周志华) 参考答案 第十四章 ...

  2. 机器学习 周志华 第一章课后习题

    机器学习 周志华 第一章课后习题 1.1 1.2 1.3 1.4 1.5 1.1 在下面这张图片中若只包含编号为1和4的两个样例,试给出相应的版本空间. 书上实例: 1.表 1.1 对应的假设空间如下 ...

  3. 小吴的《机器学习 周志华》学习笔记 第二章 模型评估与选择

    小吴的<机器学习 周志华>学习笔记 第二章 模型评估与选择 上一周我们介绍了第一章的基础概念,这一次将带来第二章的前三节.后面的2.4 比较检验与2.5 偏差与方差,涉及概率论与数理统计概 ...

  4. 小吴的《机器学习 周志华》学习笔记 第一章 绪论

    小吴的<机器学习 周志华>学习笔记 第一章 绪论 近一年时间接触到不少机器学习的知识,虽然断断续续学了一些,总感觉还没有入门(只学会了疯狂调包).因此,最近开始系统学习Machine Le ...

  5. 小吴的《机器学习 周志华》学习笔记 第二章 2.4 比较检验、2.5 偏差与方差

    小吴的<机器学习 周志华>学习笔记 第二章 2.4 比较检验. 2.5 偏差与方差 2.4 比较检验 上一周提到了实验的评价方法和性能量度,步骤简单可以看成:先使用某种实验评估方法测得学习 ...

  6. 机器学习 周志华 课后习题3.5 线性判别分析LDA

    机器学习 周志华 课后习题3.5 线性判别分析LDA 照着书上敲了敲啥都不会,雀食折磨 python代码 # coding=UTF-8 from numpy import * # 我安装numpy的时 ...

  7. 机器学习-周志华-学习记录-第一章绪论

    文章目录 绪论 一.什么是机器学习 二.基本术语 三.假设空间 四.归纳偏好 总结 参考链接 绪论 为了更早地适应研究生的生活,我决定重新学习周志华老师的机器学习这本书.同时也为了能够养成博客记录的习 ...

  8. 机器学习-周志华教授

    机器学习 南京大学周志华教授网课视频:https://www.xuetangx.com/learn/nju0802bt/nju0802bt/14363483/video/26163027 202210 ...

  9. 西瓜书入门辅助【机器学习 周志华】一些关于机器学习的重要基础概念提炼

    周志华. 机器学习 = Machine Learning. 清华大学出版社, 2016. Print. 文章目录 周志华. 机器学习 = Machine Learning. 清华大学出版社, 2016 ...

  10. 机器学习(周志华) 习题 参考答案 第十三章

    周志华老师的<机器学习>的第13章的习题答案较少,只找到了三篇,分别为链接一和链接二,这两篇文章有几乎所有题目的个人解答.第三个人只对部分题目进行了解答,相关链接将放在题目下方. 以下是个 ...

最新文章

  1. nodejs -- promise的返回
  2. 最先进数据中心都建在哪?
  3. 分布式架构基础:Java RMI详解
  4. 神策 2020 数据驱动用户大会:新愿景 + 新定位 + 新舰队正式亮相!
  5. Leetcode 1. 两数之和 (Python版)
  6. 22岁印度大学生获谷歌天价offer,击败6000人年薪百万
  7. OLAP系统场景中,GaussDB(for MySQL)借助PQ+NDP让性能提升高达百倍
  8. Mac 10.12安装IntelliJ出品的数据库管理工具DataGrip
  9. chmod命令的用法
  10. mysql grant记录信息
  11. 【NOIP 2017】宝藏
  12. 分享个最终幻想勇气启示录脚本,手游上能一键推图自动升级
  13. Java面试中如何介绍自己的项目经验?
  14. 企业管理:无法量化的工作如何进行绩效考核
  15. 模拟电路设计(23)---模数和数模转换器概述
  16. elasticsearch 使用词干提取器处理英语语言
  17. 【经典面试题】css如何画一个三角形?
  18. 要想学好平面设计,需要掌握一定的基本功和使用技巧
  19. Python读取,写入,保存txt文件
  20. Latex使用总结(待完善)

热门文章

  1. matlab中值滤波实现
  2. 【Linux】一步一步学Linux——iconv命令(60)
  3. 多媒体制作技术心得体会_多媒体课件制作的学习心得体会
  4. 音乐网站源码:Spring Boot + MyBatis + Vue 实现的
  5. java 大学考试_大学java期末考试试题
  6. 感受MapXTreme2004
  7. ThinkPHP5旅游管理系统
  8. mysql安装包及驱动下载
  9. 送书 |《Python大数据与机器学习实战》
  10. 计算机中的字体文件夹在哪,字体文件夹,教您怎么找字体文件夹