(dataSet, labels, featLabels)
| 167 | 2017-07-25 |
| 168 | """ |
| 169 | def createTree(dataSet, labels, featLabels): |
| 170 | classList = [example[-1] for example in dataSet] #取分类标签(是否放贷:yes or no) |
| 171 | if classList.count(classList[0]) == len(classList): #如果类别完全相同则停止继续划分 |
| 172 | return classList[0] |
| 173 | if len(dataSet[0]) == 1 or len(labels) == 0: #遍历完所有特征时返回出现次数最多的类标签 |
| 174 | return majorityCnt(classList) |
| 175 | bestFeat = chooseBestFeatureToSplit(dataSet) #选择最优特征 |
| 176 | bestFeatLabel = labels[bestFeat] #最优特征的标签 |
| 177 | featLabels.append(bestFeatLabel) |
| 178 | myTree = {bestFeatLabel:{}} #根据最优特征的标签生成树 |
| 179 | del(labels[bestFeat]) #删除已经使用特征标签 |
| 180 | featValues = [example[bestFeat] for example in dataSet] #得到训练集中所有最优特征的属性值 |
| 181 | uniqueVals = set(featValues) #去掉重复的属性值 |
| 182 | for value in uniqueVals: #遍历特征,创建决策树。 |
| 183 | subLabels = labels[:] |
| 184 | myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels, featLabels) |
| 185 | |
| 186 | return myTree |
| 187 | |
| 188 | """ |
| 189 | 函数说明:获取决策树叶子结点的数目 |
no test coverage detected