手动实现决策树算法
- 算法使用如下数据集(来自统计学习方法):
https://github.com/xu1995yong/ml/tree/master/python%E5%AE%9E%E7%8E%B0
- 代码:
from pandas import Series import numpy as npclass DecisionTree: def __init__(self): self.tree = None def entropy(self,data,y): ''' 求经验熵 ''' m = data.shape[0] yType = Series(y).unique() count = [] for v in yType: sub_data = https://www.it610.com/article/data[np.where(y == v)] count.append(sub_data.shape[0]) p = np.array(count) / m s = np.dot(-1 * p,np.log2(p)) return sdef conditionalEntropy(self,xi,y):''' 计算特征A对数据集D的经验条件熵 ''' m = xi.shape[0] s = 0 for i in Series(xi).value_counts().index: sub_xi = xi[np.where(xi == i)] sub_y = y[np.where(xi == i)] p = self.entropy(sub_xi,sub_y) #计算特征A的某个取值的经验熵 s += 1.0 * sub_xi.shape[0] / m * p return sdef infoGain(self,x,y): n = x.shape[1] index = None g = None for i in range(n): t = self.entropy(y,y) - self.conditionalEntropy(x[:,i],y)#计算每个特征的信息增益 if i == 0: g = t index = 0 if t > g: g = t index = i return index,gdef train(self,x,y,theta): if np.sum(y == y[0]) == y.shape[0]:#y中只有同一类样本 return y[0] [i,g] = self.infoGain(x,y)#返回信息增益最大的特征的索引、信息增益值tree = {str(i):{}}if g < theta:#特征x的信息增益小于阈值theta,则返回y中数量最多的类 returnVal = Series(y).value_counts().index[0] return returnVal else : #根据(信息增益最大的)特征的取值,划分数据集,并递归求每一部分数据集的信息增益 uniqueVal = Series(x[:,i]).unique() for val in uniqueVal: xi = x[np.where(x[:,i] == val)] yi = y[np.where(x[:,i] == val)] xi = np.hstack((xi[:,0:i],xi[:,i+1:xi.shape[1]+1])) returnVal = self.train(xi,yi,theta) tree[str(i)][val] = returnVal self.tree = tree return tree if __name__ == '__main__': x = np.loadtxt('x.txt') y = np.loadtxt('y.txt') dt = DecisionTree() dt.train(x,y,0) print(dt.tree)
- 输出结果:
{‘2’: {0.0: {‘1’: {0.0: 0.0, 1.0: 1.0}}, 1.0: 1.0}}
- 树的可视化:
【机器学习|决策树算法(DecisionTree)】
推荐阅读
- paddle|动手从头实现LSTM
- 人工智能|干货!人体姿态估计与运动预测
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- 读书笔记|《白话大数据和机器学习》学习笔记1
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件