• 大小: 5KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2023-07-12
  • 语言: Python
  • 标签: 决策树  

资源简介

决策树(Decision Tree)是在已知各种情况发生概率的基础上,通过构成决策树来求取净现值的期望值大于等于零的概率,评价项目风险,判断其可行性的决策分析方法,是直观运用概率分析的一种图解法。由于这种决策分支画成图形很像一棵树的枝干,故称决策树。在机器学习中,决策树是一个预测模型,他代表的是对象属性与对象值之间的一种映射关系。Entropy = 系统的凌乱程度,使用算法ID3, C4.5和C5.0生成树算法使用熵。这一度量是基于信息学理论中熵的概念。

资源截图

代码片段和文件信息

import numpy as np	
import math
from sklearn.datasets import load_iris

#找出list中匹配字符的最大index
def maxIndex(numnumList):
    max_index = 0
    for i in range(len(numList)):
        if(num==numList[i]):
            max_index = i
    return max_index
#找出片段序列中的最大值和最小值
def maxAndMinValue(numberListindexList):
    max_value = numberList[indexList[0]]
    min_value = numberList[indexList[0]]
    for i in indexList:
        if(numberList[i]>max_value):
            max_value = numberList[i]
        if(numberList[i]            min_value = numberList[i]
    return max_valuemin_value

#当对数输入为0是,输出为0
def log(mn):
    if(m==0):
        return 0
    else:
        return math.log(mn)
#熵的计算m为某种结果出现的次数,n为所有结果出现的次数
def entropy(nm_0m_1m_2):
#结果出现概率 p = m/n
    p_0 = float(m_0/n) 
    p_1 = float(m_1/n)
    p_2 = float(m_2/n)
    e = - (p_0 * log(p_02) + p_1 * log(p_12) + p_2 * log(p_22))
    return e

#计算出某一类中最小的熵,从而得到最大的information gain
def computing_min_entropy(label_nameinclude_indexstep_width):
    order = labels.index(label_name)
    classed_attributes = attributes[:order]
    min_entropy = 1000 #存储最小的熵
    max_valuemin_value = maxAndMinValue(classed_attributesinclude_index)
    ar_1 = np.arange(min_value+0.01max_value-0.01step_width)
    #存储分成两类的索引值
    branch_1_index = [];
    branch_2_index = [];
    #划分的临界值
    threshold = 0
    #分成两类的索引值
    branch1 = []
    branch2 = []
    matrix = [[0000][0000]] 
    for i in ar_1:
        #存储分类后各项指标
        computing_matrix = [[0000][0000]] 
        
        #存储分成两类的索引值
        branch_1_index = [];
        branch_2_index = [];
        for j in include_index:
            if(classed_attributes[j]<=i):
                branch_1_index.append(j)
                computing_matrix[0][0] +=1
                if(target[j]==0):
                 computing_matrix[0][1] +=1
                elif(target[j]==1):
                 computing_matrix[0][2] +=1
                else:
                 computing_matrix[0][3] +=1
            else:
                branch_2_index.append(j)
                computing_matrix[1][0] +=1
                if(target[j]==0):
                 computing_matrix[1][1] +=1
                elif(target[j]==1):
                 computing_matrix[1][2] +=1
                else:
                 computing_matrix[1][3] +=1
        #属于第一类与第二类的个数
        class1_num = computing_matrix[0][0]
        class2_num = computing_matrix[1][0]

        e1 = class1_num * entropy(computing_matrix[

评论

共有 条评论