单层决策树分类函数 Parameters: dataMatrix - 数据矩阵 dimen - 第dimen列,也就是第几个特征 threshVal - 阈值 threshIneq - 标志 Returns: retArray - 分类结果
(dataMatrix,dimen,threshVal,threshIneq)
| 29 | return dataMat, labelMat |
| 30 | |
| 31 | def stumpClassify(dataMatrix,dimen,threshVal,threshIneq): |
| 32 | """ |
| 33 | 单层决策树分类函数 |
| 34 | Parameters: |
| 35 | dataMatrix - 数据矩阵 |
| 36 | dimen - 第dimen列,也就是第几个特征 |
| 37 | threshVal - 阈值 |
| 38 | threshIneq - 标志 |
| 39 | Returns: |
| 40 | retArray - 分类结果 |
| 41 | """ |
| 42 | retArray = np.ones((np.shape(dataMatrix)[0],1)) #初始化retArray为1 |
| 43 | if threshIneq == 'lt': |
| 44 | retArray[dataMatrix[:,dimen] <= threshVal] = -1.0 #如果小于阈值,则赋值为-1 |
| 45 | else: |
| 46 | retArray[dataMatrix[:,dimen] > threshVal] = -1.0 #如果大于阈值,则赋值为-1 |
| 47 | return retArray |
| 48 | |
| 49 | |
| 50 | def buildStump(dataArr,classLabels,D): |