1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
from math import log import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split matplotlib.rcParams['font.family'] = 'SimHei'
class DecisionTree: """ DecisionTree 决策树类 详情见API文档
"""
label_name = [] raw_data = [] raw_label = [] train_data = [] train_label = [] test_data = [] test_label = [] tree = []
def __init__(self, input_data, input_label, input_name): """ __init__ 构造函数初始化决策树对象
:param self: 类方法自带 :param input_data: 输入数据集 :param input_label: 输入标签集 :param input_name: 输入属性名称 """ self.raw_data = np.array(input_data, dtype=object) self.raw_label = np.array(input_label, dtype=object) self.label_name = np.array(input_name, dtype=object)
def loadTrainData(self, type=0): """ loadTrainData 构建训练集
:param self: 类方法自带 :param type: 加载方法 """ if(type == 0): self.train_data = np.array(self.raw_data, dtype=object) self.train_label = np.array(self.raw_label, dtype=object)
def calcGini(self, labels): """ calcGini 计算给定数据集的基尼指数Gini(D)
:param self: 类方法自带 :param labels: 输入的标签集或属性集 :return: 计算的基尼指数 """ label_values = set(labels) ans = 1.0
for value in label_values: p = labels[labels == value].size / labels.size ans -= p**2 return ans
def calcEntropy(self, labels): """ calcEntropy 计算给定数据集的经验熵Ent(D)
:param self: 类方法自带 :param labels: 输入的标签集或属性集 :return: 计算的Ent熵 """ label_values = set(labels) ans = 0.0 for value in label_values: p = labels[labels == value].size / labels.size ans -= p*log(p, 2) return ans
def calcSumEnt(self, data, labels): """ calcSumEnt 计算给定数据集的经验熵Ent(D)
:param self: 类方法自带 :param data: 输入的数据集 :param labels: 输入的标签集 :return: sum_ent: 计算的Ent熵 :return: max_point: 对于连续型属性 最佳分割点 """ features = np.array(data, dtype=object) labels = np.array(labels, dtype=object)
feature_values = list(set(features))
num = features.size max_point = -float('inf') sum_ent = 0.0 if type(features[0]) != float and type(features[0]) != int: for value in feature_values: p = features[features == value].size/num sum_ent += p*self.calcEntropy(labels[features == value]) else: sum_ent = float('inf') feature_values.sort() cnt = len(feature_values) for j in range(cnt-1): point_ent = 0 point = float(feature_values[j] + feature_values[j+1])/2 p1 = features[features < point].size / num p2 = features[features >= point].size / num point_ent += p1*self.calcEntropy(labels[features < point]) point_ent += p2*self.calcEntropy(labels[features >= point]) if point_ent < sum_ent: sum_ent = point_ent max_point = point return sum_ent, max_point
def calcInfoGain(self, data, labels): """ calcInfoGain 计算指定数据集选定属性的信息熵增益(ID3)
:param self: 类方法自带 :param data: 输入的数据集(选定指定属性) :param labels: 输入的标签集或属性集 :return: 信息熵增益 """ sum_ent, max_point = self.calcSumEnt(data, labels) return self.calcEntropy(labels) - sum_ent, max_point
def calcInfoGainRatio(self, data, labels): """ calcInfoGainRatio 计算指定数据集选定属性的信息熵增益率(C4.5)
:param self: 类方法自带 :param data: 输入的数据集(选定指定属性) :param labels: 输入的标签集或属性集 :return: 信息熵增益率 """ iv = self.calcEntropy(data) if iv == 0: return 0, 0 else: info_gain, max_point = self.calcInfoGain(data, labels) return info_gain/iv, max_point
def calcInfoGini(self, data, labels): """ calcInfoGini 计算指定数据集选定属性的基尼指数(CART)
:param self: 类方法自带 :param data: 输入的数据集(选定指定属性) :param labels: 输入的标签集或属性集 :return: sum_gini: 指定属性的基尼指数和 :return: 最佳分割点 """
features = np.array(data, dtype=object) labels = np.array(labels, dtype=object) feature_values = list(set(features))
num = features.size max_point = -float('inf') best_gini = float('inf') best_value = ""
if type(features[0]) != float and type(features[0]) != int: for value in feature_values: p1 = features[features == value].size / num p2 = features[features != value].size / num tmp_gini = p1 * \ self.calcGini(labels[features == value]) + \ p2*self.calcGini(labels[features != value]) if tmp_gini < best_gini: best_gini = tmp_gini best_value = value else: best_gini = float('inf') feature_values.sort() cnt = len(feature_values) for j in range(cnt-1): point_gini = 0 point = float(feature_values[j] + feature_values[j+1])/2 p1 = features[features < point].size / num p2 = features[features >= point].size / num point_gini += p1*self.calcGini(labels[features < point]) point_gini += p2*self.calcGini(labels[features >= point]) if point_gini < best_gini: best_gini = point_gini max_point = point
return best_gini, best_value, max_point
def chooseBest(self, data, labels, names, method='id3'): """ chooseBest 选择最佳分割属性
:param self: 类方法自带 :param data: 当前数据集 :param labels: 当前标签集合 :param names: 当前数据集的属性名集合 :param method: 信息增益计算方法(ID3/C4.5) :return: best_feature_index 最佳分割属性索引 :return: best_feature_name: 最佳分割属性名 :return: best_info_improve: 最佳分割后信息增益值 :return: best_point: 对于连续值最佳分割点 """ data = np.array(data, dtype=object) labels = np.array(labels, dtype=object) names = np.array(names, dtype=object) feature_num = data.shape[1]
if method == 'id3' or method == 'c4.5': best_info_improve = -float('inf') elif method == 'cart': best_info_improve = float('inf') best_feature_index = -1 best_point = -float('inf') best_value = ""
for feature_index in range(feature_num): if method == 'id3': now_info_improve, now_point = self.calcInfoGain( data[:, feature_index], labels) if now_info_improve > best_info_improve: best_info_improve = now_info_improve best_point = now_point best_feature_index = feature_index
elif method == 'c4.5': now_info_improve, now_point = self.calcInfoGainRatio( data[:, feature_index], labels) if now_info_improve > best_info_improve: best_info_improve = now_info_improve best_point = now_point best_feature_index = feature_index
elif method == 'cart': now_info_improve, now_value, now_point = self.calcInfoGini( data[:, feature_index], labels) if now_info_improve < best_info_improve: best_info_improve = now_info_improve best_point = now_point best_feature_index = feature_index best_value = now_value best_feature_name = names[best_feature_index] return best_feature_index, best_feature_name, best_value, best_info_improve, best_point
def splitData(self, data, labels, names, feature_index, feature_name, cart_value, point, method): """ splitData 根据最佳分割,讲数据集标签集划分为不同子集
:param self: 类方法自带 :param data: 当前数据集 :param labels: 当前标签集合 :param names: 当前数据集的属性名集合 :param feature_index: 分割属性索引 :param point: 对于连续属性的分割点 :return: data_set: 不同属性对应的子数据集的集合 :return: label_set: 不同属性对应的子标签集的集合 :return: name_set: 子集属性名集合 """ data = np.array(data, dtype=object) labels = np.array(labels, dtype=object) names = np.array(names, dtype=object)
features_col = data[:, feature_index]
data_set = {} label_set = {} if method == 'id3' or method == 'c4.5': data = np.delete(data, feature_index, 1) name_set = np.delete(names, feature_index) if(type(features_col[0]) != float and type(features_col[0] != int)):
features_values = set(features_col) for value in features_values: data_set[value] = data[features_col == value] label_set[value] = labels[features_col == value] else: data_set[('<', point)] = data[features_col < point] label_set[('<', point)] = labels[features_col < point] data_set[('>=', point)] = data[features_col >= point] label_set[('>=', point)] = labels[features_col >= point] elif method == 'cart': name_set = names if(type(features_col[0]) != float and type(features_col[0] != int)): data_set[('=', cart_value)] = data[features_col == cart_value] label_set[('=', cart_value) ] = labels[features_col == cart_value] data_set[('!=', cart_value)] = data[features_col != cart_value] label_set[('!=', cart_value) ] = labels[features_col != cart_value] else: data_set[('<', point)] = data[features_col < point] label_set[('<', point)] = labels[features_col < point] data_set[('>=', point)] = data[features_col >= point] label_set[('>=', point)] = labels[features_col >= point]
return data_set, label_set, name_set
def startCreateTree(self, method='id3', min_sample=1): """ startCreateTree 根据设定的信息增益标准以及阈值限制,构建决策树
:param self: 类方法自带 :param method: 选定方法,默认为ID3 :param min_sample: 最少样本数阈值,默认为1 """ self.tree = self.createTree( self.train_data, self.train_label, self.label_name, method, min_sample)
def majLabel(self, labels): """ majLabel 当前标签集的选择主要类别
:param self: 类方法自带 :param labels: 输入标签集 :return: 主要类别名 """ labels = np.array(labels, dtype=object) label_values = set(labels) label_map = {} for value in label_values: label_map[value] = labels[labels == value].size return max(label_map, key=label_map.get)
def createTree(self, data, labels, names, method, min_sample): """ createTree 构建当前树结点
:param self: 类方法自带 :param data: 输入数据集 :param labels: 输入标签集 :param names: 输入属性名集合 :param min_sample: 最小样本数量阈值 :return: 返回建立的节点 """ data = np.array(data, dtype=object) labels = np.array(labels, dtype=object) names = np.array(names, dtype=object) if len(set(labels)) == 1: return labels[0] if data.size == 0 or labels.size <= min_sample: return self.majLabel(labels) best_feature_index, best_feature_name, best_value, best_ent, best_point = self.chooseBest( data, labels, names, method)
node = {"feature_name": best_feature_name} child_data_set, child_label_set, child_name_set = self.splitData( data, labels, names, best_feature_index, best_feature_name, best_value, best_point, method)
for feature_value in child_data_set.keys(): node[feature_value] = self.createTree( child_data_set[feature_value], child_label_set[feature_value], child_name_set, method, min_sample) return node
def createDataSet(type=0): """ createTree 构建数据集
:param type: 选择数据集类型(默认为0) :return: data: 数据集 :return: label: 标签集 :return: label_names: 属性名集合 """ if type == 0: data = [ ['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['乌黑', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'], ['乌黑', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['青绿', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'], ['浅白', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['青绿', '稍蜷', '浊响', '清晰', '稍凹', '软粘'], ['乌黑', '稍蜷', '浊响', '稍糊', '稍凹', '软粘'], ['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '硬滑'], ['乌黑', '稍蜷', '沉闷', '稍糊', '稍凹', '硬滑'], ['青绿', '硬挺', '清脆', '清晰', '平坦', '软粘'], ['浅白', '硬挺', '清脆', '模糊', '平坦', '硬滑'], ['浅白', '蜷缩', '浊响', '模糊', '平坦', '软粘'], ['青绿', '稍蜷', '浊响', '稍糊', '凹陷', '硬滑'], ['浅白', '稍蜷', '沉闷', '稍糊', '凹陷', '硬滑'], ['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '软粘'], ['浅白', '蜷缩', '浊响', '模糊', '平坦', '硬滑'], ['青绿', '蜷缩', '沉闷', '稍糊', '稍凹', '硬滑'] ] labels = ['是', '是', '是', '是', '是', '是', '是', '是', '否', '否', '否', '否', '否', '否', '否', '否', '否'] label_names = ['色泽', '根蒂', '敲声', '纹理', '脐部', '触感']
elif type == 1: data = np.array([[0.697, 0.46], [0.774, 0.376], [0.634, 0.264], [0.608, 0.318], [0.556, 0.215], [0.403, 0.237], [0.481, 0.149], [0.437, 0.211], [0.666, 0.091], [0.243, 0.267], [0.245, 0.057], [0.343, 0.099], [0.639, 0.161], [0.657, 0.198], [0.36, 0.37], [0.593, 0.042], [0.719, 0.103, ]], dtype=object) labels = np.array(['是', '是', '是', '是', '是', '是', '是', '是', '否', '否', '否', '否', '否', '否', '否', '否', '否'], dtype=object) label_names = np.array(["密度", "含糖率"], dtype=object) if type == 2: data = np.array([['1', '青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['2', '乌黑', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'], ['3', '乌黑', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['4', '青绿', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑'], ['5', '浅白', '蜷缩', '浊响', '清晰', '凹陷', '硬滑'], ['6', '青绿', '稍蜷', '浊响', '清晰', '稍凹', '软粘'], ['7', '乌黑', '稍蜷', '浊响', '稍糊', '稍凹', '软粘'], ['8', '乌黑', '稍蜷', '浊响', '清晰', '稍凹', '硬滑'], ['9', '乌黑', '稍蜷', '沉闷', '稍糊', '稍凹', '硬滑'], ['10', '青绿', '硬挺', '清脆', '清晰', '平坦', '软粘'], ['11', '浅白', '硬挺', '清脆', '模糊', '平坦', '硬滑'], ['12', '浅白', '蜷缩', '浊响', '模糊', '平坦', '软粘'], ['13', '青绿', '稍蜷', '浊响', '稍糊', '凹陷', '硬滑'], ['14', '浅白', '稍蜷', '沉闷', '稍糊', '凹陷', '硬滑'], ['15', '乌黑', '稍蜷', '浊响', '清晰', '稍凹', '软粘'], ['16', '浅白', '蜷缩', '浊响', '模糊', '平坦', '硬滑'], ['17', '青绿', '蜷缩', '沉闷', '稍糊', '稍凹', '硬滑']], dtype=object) labels = np.array(['是', '是', '是', '是', '是', '是', '是', '是', '否', '否', '否', '否', '否', '否', '否', '否', '否'], dtype=object) label_names = np.array( ['ID', '色泽', '根蒂', '敲声', '纹理', '脐部', '触感'], dtype=object) return data, labels, label_names
decisionNode = dict(boxstyle="round4", color='r', fc='0.9')
leafNode = dict(boxstyle="circle", color='m')
arrow_args = dict(arrowstyle="<-", color='g')
def plot_node(node_txt, center_point, parent_point, node_style): """ plot_node 绘制父子节点,节点间的箭头,并填充箭头中间上的文本
:param node_txt: 文本内容 :param center_point: 文本中心点 :param parent_point: 指向文本中心的点 """ createPlot.ax1.annotate(node_txt, xy=parent_point, xycoords='axes fraction', xytext=center_point, textcoords='axes fraction', va="center", ha="center", bbox=node_style, arrowprops=arrow_args, weight='demi')
def get_leafs_num(tree_dict): """ get_leafs_num 递归计算叶节点的个数
:param tree_dict: 决策树的字典形式 :return: tree_dict: 叶节点总个数 """ leafs_num = 0
for key, value in tree_dict.items(): if key == 'feature_name': continue elif type(value).__name__ == 'dict': leafs_num += get_leafs_num(value) else: leafs_num += 1
return leafs_num
def get_tree_max_depth(tree_dict): """ get_tree_max_depth 求树的最深层数
:param tree_dict: 树的字典存储 :return: tree_dict: 最深层数 """ max_depth = 0 for key, value in tree_dict.items(): this_path_depth = 0 if type(value).__name__ == 'dict': this_path_depth = 1 + get_tree_max_depth(value) else: this_path_depth = 1 if this_path_depth > max_depth: max_depth = this_path_depth return max_depth
def plot_mid_text(center_point, parent_point, txt_str): """ plot_mid_text 计算父节点和子节点的中间位置,并在父子节点间填充文本信息
:param center_point: 文本中心点 :param parent_point: 指向文本中心点的点 """
x_mid = (parent_point[0] - center_point[0])/2.0 + center_point[0] y_mid = (parent_point[1] - center_point[1])/2.0 + center_point[1] createPlot.ax1.text(x_mid, y_mid, txt_str) return
def plotTree(tree_dict, parent_point, node_txt): """ plotTree 绘制树
:param tree_dict: 树 :param parent_point: 父节点位置 :param node_txt: 节点内容 """ leafs_num = get_leafs_num(tree_dict) root = tree_dict['feature_name'] center_point = (plotTree.xOff+(1.0+float(leafs_num)) / 2.0/plotTree.totalW, plotTree.yOff) plot_mid_text(center_point, parent_point, node_txt) plot_node(root, center_point, parent_point, decisionNode)
plotTree.yOff = plotTree.yOff-1.0/plotTree.totalD for key, value in tree_dict.items(): if key == 'feature_name': continue elif type(value).__name__ == 'dict': plotTree(value, center_point, str(key)) else: plotTree.xOff = plotTree.xOff+1.0/plotTree.totalW plot_node(value, (plotTree.xOff, plotTree.yOff), center_point, leafNode) plot_mid_text((plotTree.xOff, plotTree.yOff), center_point, str(key)) plotTree.yOff = plotTree.yOff+1.0/plotTree.totalD
return
def createPlot(tree_dict): """ createPlot 绘制决策树图形
:param tree_dict: 决策树的字典形式 """ fig = plt.figure(figsize=(8, 8), facecolor='white') fig.clf() axprops = dict(xticks=[], yticks=[]) createPlot.ax1 = plt.subplot(111, frameon=False, **axprops) plotTree.totalW = float(get_leafs_num(tree_dict)) plotTree.totalD = float(get_tree_max_depth(tree_dict)) plotTree.xOff = -0.5/plotTree.totalW plotTree.yOff = 1.0 plotTree(tree_dict, (0.5, 1.0), '') plt.show()
|