MCPcopy Create free account
hub / github.com/Jack-Cherish/Machine-Learning / buildStump

Function buildStump

AdaBoost/adaboost.py:74–107  ·  view source on GitHub ↗

找到数据集上最佳的单层决策树 Parameters: dataArr - 数据矩阵 classLabels - 数据标签 D - 样本权重 Returns: bestStump - 最佳单层决策树信息 minError - 最小误差 bestClasEst - 最佳的分类结果

(dataArr,classLabels,D)

Source from the content-addressed store, hash-verified

72
73
74def buildStump(dataArr,classLabels,D):
75 """
76 找到数据集上最佳的单层决策树
77 Parameters:
78 dataArr - 数据矩阵
79 classLabels - 数据标签
80 D - 样本权重
81 Returns:
82 bestStump - 最佳单层决策树信息
83 minError - 最小误差
84 bestClasEst - 最佳的分类结果
85 """
86 dataMatrix = np.mat(dataArr); labelMat = np.mat(classLabels).T
87 m,n = np.shape(dataMatrix)
88 numSteps = 10.0; bestStump = {}; bestClasEst = np.mat(np.zeros((m,1)))
89 minError = float('inf') #最小误差初始化为正无穷大
90 for i in range(n): #遍历所有特征
91 rangeMin = dataMatrix[:,i].min(); rangeMax = dataMatrix[:,i].max() #找到特征中最小的值和最大值
92 stepSize = (rangeMax - rangeMin) / numSteps #计算步长
93 for j in range(-1, int(numSteps) + 1):
94 for inequal in ['lt', 'gt']: #大于和小于的情况,均遍历。lt:less than,gt:greater than
95 threshVal = (rangeMin + float(j) * stepSize) #计算阈值
96 predictedVals = stumpClassify(dataMatrix, i, threshVal, inequal)#计算分类结果
97 errArr = np.mat(np.ones((m,1))) #初始化误差矩阵
98 errArr[predictedVals == labelMat] = 0 #分类正确的,赋值为0
99 weightedError = D.T * errArr #计算误差
100 # print("split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError))
101 if weightedError < minError: #找到误差最小的分类方式
102 minError = weightedError
103 bestClasEst = predictedVals.copy()
104 bestStump['dim'] = i
105 bestStump['thresh'] = threshVal
106 bestStump['ineq'] = inequal
107 return bestStump, minError, bestClasEst
108
109def adaBoostTrainDS(dataArr, classLabels, numIt = 40):
110 """

Callers 1

adaBoostTrainDSFunction · 0.70

Calls 1

stumpClassifyFunction · 0.70

Tested by

no test coverage detected