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

Function buildStump

AdaBoost/ROC.py:50–83  ·  view source on GitHub ↗

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

(dataArr,classLabels,D)

Source from the content-addressed store, hash-verified

48
49
50def buildStump(dataArr,classLabels,D):
51 """
52 找到数据集上最佳的单层决策树
53 Parameters:
54 dataArr - 数据矩阵
55 classLabels - 数据标签
56 D - 样本权重
57 Returns:
58 bestStump - 最佳单层决策树信息
59 minError - 最小误差
60 bestClasEst - 最佳的分类结果
61 """
62 dataMatrix = np.mat(dataArr); labelMat = np.mat(classLabels).T
63 m,n = np.shape(dataMatrix)
64 numSteps = 10.0; bestStump = {}; bestClasEst = np.mat(np.zeros((m,1)))
65 minError = float('inf') #最小误差初始化为正无穷大
66 for i in range(n): #遍历所有特征
67 rangeMin = dataMatrix[:,i].min(); rangeMax = dataMatrix[:,i].max() #找到特征中最小的值和最大值
68 stepSize = (rangeMax - rangeMin) / numSteps #计算步长
69 for j in range(-1, int(numSteps) + 1):
70 for inequal in ['lt', 'gt']: #大于和小于的情况,均遍历。lt:less than,gt:greater than
71 threshVal = (rangeMin + float(j) * stepSize) #计算阈值
72 predictedVals = stumpClassify(dataMatrix, i, threshVal, inequal)#计算分类结果
73 errArr = np.mat(np.ones((m,1))) #初始化误差矩阵
74 errArr[predictedVals == labelMat] = 0 #分类正确的,赋值为0
75 weightedError = D.T * errArr #计算误差
76 # print("split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError))
77 if weightedError < minError: #找到误差最小的分类方式
78 minError = weightedError
79 bestClasEst = predictedVals.copy()
80 bestStump['dim'] = i
81 bestStump['thresh'] = threshVal
82 bestStump['ineq'] = inequal
83 return bestStump, minError, bestClasEst
84
85def adaBoostTrainDS(dataArr, classLabels, numIt = 40):
86 """

Callers 1

adaBoostTrainDSFunction · 0.70

Calls 1

stumpClassifyFunction · 0.70

Tested by

no test coverage detected