【机器学习算法-python实现】Adaboost的实现(1)-单层决策树(decision stump)

来源:互联网 发布:坑爹淘宝买家秀 编辑:程序博客网 时间:2024/05/21 17:45

转载地址:http://blog.csdn.net/buptgshengod/article/details/25049305

1.背景

     上一节学习支持向量机,感觉公式都太难理解了,弄得我有点头大。不过这一章的Adaboost线比较起来就容易得多。Adaboost是用元算法的思想进行分类的。什么事元算法的思想呢?就是根据数据集的不同的特征在决定结果时所占的比重来划分数据集。一句话来说,就是根据特征的重要性比重来划分数据集。(不同的权值)就是要对每个特征值都构建决策树,并且赋予他们不同的权值,最后集合起来比较。

      比如说我们可以通过是否有胡子和身高的高度这两个特征来来决定一个人的性别,很明显是否有胡子可能在判定性别方面比身高更准确,所以在判定的时候我们就赋予这个特征更大的权重,比如说我们把权重设成0.8:0.2。这样就比0.5:0.5的权重来的更准确些。


2.构建决策树

    接着我们来构建决策树。我们的决策树要实现主要两个功能,一个是找出对结果影响最大的特征值。另外一个功能是找到这个特征值得阈值。阈值就是,比方说阈值是d,当特征值大于d结果为1,当特征值小于d结果为0。

首先看下数据集,是一个两个特征值的矩阵。
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ef loadSimpData():  
  2.     datMat = matrix([[ 1. ,  2.1],  
  3.         [ 2. ,  1.1],  
  4.         [ 1.3,  1. ],  
  5.         [ 1. ,  1. ],  
  6.         [ 2. ,  1. ]])  
  7.     classLabels = [1.01.0, -1.0, -1.01.0]  
  8.     return datMat,classLabels  


接着是树的分类函数。这个函数在下面的循环里要用到,作用很简单,就是比对每一列的特征值和目标函数,返回比对的结果。四个参数分别是(输入矩阵,第几列,阈值,lt或gt)
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):#just classify the data  
  2.     retArray = ones((shape(dataMatrix)[0],1))  
  3.     if threshIneq == 'lt':  
  4.         retArray[dataMatrix[:,dimen] <= threshVal] = -1.0  
  5.     else:  
  6.         retArray[dataMatrix[:,dimen] > threshVal] = -1.0  
  7.       
  8.     return retArray  


最后是构建二叉树函数,通过循环比较得到最佳特征值和它的阈值。D是初始矩阵的权重。
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. def buildStump(dataArr,classLabels,D):  
  2.     dataMatrix = mat(dataArr); labelMat = mat(classLabels).T  
  3.     m,n = shape(dataMatrix)  
  4.     numSteps = 10.0; bestStump = {}; bestClasEst = mat(zeros((m,1)))  
  5.     minError = inf #init error sum, to +infinity  
  6.     for i in range(n):#loop over all dimensions  
  7.         rangeMin = dataMatrix[:,i].min(); rangeMax = dataMatrix[:,i].max();  
  8.           
  9.         stepSize = (rangeMax-rangeMin)/numSteps  
  10.         for j in range(-1,int(numSteps)+1):#loop over all range in current dimension  
  11.             for inequal in ['lt''gt']: #go over less than and greater than  
  12.                 threshVal = (rangeMin + float(j) * stepSize)  
  13.                   
  14.                 predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)#call stump classify with i, j, lessThan  
  15.                 errArr = mat(ones((m,1)))  
  16.                   
  17.                   
  18.                 errArr[predictedVals == labelMat] = 0  
  19.                   
  20.                 weightedError = D.T*errArr  #calc total error multiplied by D  
  21.                 #print "split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError)  
  22.                 if weightedError < minError:  
  23.                     minError = weightedError  
  24.                     bestClasEst = predictedVals.copy()  
  25.                     bestStump['dim'] = i  
  26.                     bestStump['thresh'] = threshVal  
  27.                     bestStump['ineq'] = inequal  
  28.     return bestStump,minError,bestClasEst  

3.结果


                当我们假设初始权重相同(5行数据也就是都是0.2),得到结果


{'dim': 0, 'ineq': 'lt', 'thresh': 1.3}——第一个特征值权重最大,阈值是1.3


 [[ 0.2]]——错误率0.2,也就是五个错一个


[[-1.]————判断结果,第一个数据错误

 [ 1.]

 [-1.]

 [-1.]

 [ 1.]]

0 0
原创粉丝点击