MCPcopy Index your code
hub / github.com/Dod-o/Statistical-Learning-Method_Code / predict

Function predict

DecisionTree/DecisionTree.py:244–277  ·  view source on GitHub ↗

预测标签 :param testDataList:样本 :param tree: 决策树 :return: 预测结果

(testDataList, tree)

Source from the content-addressed store, hash-verified

242 return treeDict
243
244def predict(testDataList, tree):
245 '''
246 预测标签
247 :param testDataList:样本
248 :param tree: 决策树
249 :return: 预测结果
250 '''
251 # treeDict = copy.deepcopy(tree)
252
253 #死循环,直到找到一个有效地分类
254 while True:
255 #因为有时候当前字典只有一个节点
256 #例如{73: {0: {74:6}}}看起来节点很多,但是对于字典的最顶层来说,只有73一个key,其余都是value
257 #若还是采用for来读取的话不太合适,所以使用下行这种方式读取key和value
258 (key, value), = tree.items()
259 #如果当前的value是字典,说明还需要遍历下去
260 if type(tree[key]).__name__ == 'dict':
261 #获取目前所在节点的feature值,需要在样本中删除该feature
262 #因为在创建树的过程中,feature的索引值永远是对于当时剩余的feature来设置的
263 #所以需要不断地删除已经用掉的特征,保证索引相对位置的一致性
264 dataVal = testDataList[key]
265 del testDataList[key]
266 #将tree更新为其子节点的字典
267 tree = value[dataVal]
268 #如果当前节点的子节点的值是int,就直接返回该int值
269 #例如{403: {0: 7, 1: {297:7}},dataVal=0
270 #此时上一行tree = value[dataVal],将tree定位到了7,而7不再是一个字典了,
271 #这里就可以直接返回7了,如果tree = value[1],那就是一个新的子节点,需要继续遍历下去
272 if type(tree).__name__ == 'int':
273 #返回该节点值,也就是分类值
274 return tree
275 else:
276 #如果当前value不是字典,那就返回分类值
277 return value
278
279def model_test(testDataList, testLabelList, tree):
280 '''

Callers 1

model_testFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected