(转载请注明出处:http://blog.csdn.net/buptgshengod)
ef loadSimpData(): datMat = matrix([[ 1. , 2.1], [ 2. , 1.1], [ 1.3, 1. ], [ 1. , 1. ], [ 2. , 1. ]]) classLabels = [1.0, 1.0, -1.0, -1.0, 1.0] return datMat,classLabels
def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):#just classify the data retArray = ones((shape(dataMatrix)[0],1)) if threshIneq == ‘lt‘: retArray[dataMatrix[:,dimen] <= threshVal] = -1.0 else: retArray[dataMatrix[:,dimen] > threshVal] = -1.0 return retArray
def buildStump(dataArr,classLabels,D): dataMatrix = mat(dataArr); labelMat = mat(classLabels).T m,n = shape(dataMatrix) numSteps = 10.0; bestStump = {}; bestClasEst = mat(zeros((m,1))) minError = inf #init error sum, to +infinity for i in range(n):#loop over all dimensions rangeMin = dataMatrix[:,i].min(); rangeMax = dataMatrix[:,i].max(); stepSize = (rangeMax-rangeMin)/numSteps for j in range(-1,int(numSteps)+1):#loop over all range in current dimension for inequal in [‘lt‘, ‘gt‘]: #go over less than and greater than threshVal = (rangeMin + float(j) * stepSize) predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)#call stump classify with i, j, lessThan errArr = mat(ones((m,1))) errArr[predictedVals == labelMat] = 0 weightedError = D.T*errArr #calc total error multiplied by D #print "split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError) if weightedError < minError: minError = weightedError bestClasEst = predictedVals.copy() bestStump[‘dim‘] = i bestStump[‘thresh‘] = threshVal bestStump[‘ineq‘] = inequal return bestStump,minError,bestClasEst
{‘dim‘: 0, ‘ineq‘: ‘lt‘, ‘thresh‘: 1.3}——第一个特征值权重最大,阈值是1.3
[[ 0.2]]——错误率0.2,也就是五个错一个
[[-1.]————判断结果,第一个数据错误
[ 1.]
[-1.]
[-1.]
[ 1.]]
[1]
machine learning in action,Peter Harrington
【机器学习算法-python实现】Adaboost的实现(1)-单层决策树(decision stump),布布扣,bubuko.com
【机器学习算法-python实现】Adaboost的实现(1)-单层决策树(decision stump)
原文:http://blog.csdn.net/buptgshengod/article/details/25049305