Author: Not specified Language: python
Description: (v8) Timestamp: 2018-05-21 03:19:17 +0000
View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply
  1. def buildStump(dataArr,classLabels,D):
  2.     dataMatrix = np.mat(dataArr)
  3.     labelMat = np.mat(classLabels).T
  4.     m,n = np.shape(dataMatrix)
  5.    
  6.     numSteps = 500.0
  7.     bestStump = {}
  8.     bestClasEst = np.mat(np.zeros((m,1)))
  9.    
  10.     minError = np.inf #init error sum, to +infinity
  11.    
  12.     for i in range(n):#loop over all dimensions
  13.        
  14.         rangeMin = dataMatrix[:,i].min()
  15.         rangeMax = dataMatrix[:,i].max()
  16.         stepSize = (rangeMax-rangeMin)/numSteps
  17.        
  18.         for j in range(-1,int(numSteps)+1):  #loop over all range in current dimension
  19.            
  20.             for inequal in ['lt', 'gt']:     #go over less than and greater than
  21.                
  22.                 threshVal = (rangeMin + float(j) * stepSize)
  23.                 predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)#call stump classify with i, j, lessThan
  24.                
  25.                 errArr = np.mat(np.ones((m,1)))
  26.                
  27.                 errArr[predictedVals == labelMat] = 0
  28.                
  29.                 #Line where AdaBoost interacts with the classifier.
  30.                 weightedError = D.T*errArr  #calc total error multiplied by D
  31.                
  32.                 #print ("split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError))
  33.                 if weightedError < minError:
  34.                     minError = weightedError
  35.                     bestClasEst = predictedVals.copy()
  36.                     bestStump['dim'] = i
  37.                     bestStump['thresh'] = threshVal
  38.                     bestStump['ineq'] = inequal
  39.     return bestStump,minError,bestClasEst
View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply