Demo entry 6734476

py

   

Submitted by anonymous on Apr 20, 2018 at 15:42
Language: Python 3. Code size: 835 Bytes.

def createTree(dataSet,labels):
	classList=[example[-1] for example in dataSet] #classList表示树,树的结点值为类别
	if classList.count(classList[0])==len(classList): #情形(1):
		return classList[0]
	if len(dataSet[0])==1: #情形(2):特征集为空
		return majorityCnt(classList)
	bestFeat=chooseBestFeatureToSplit(dataSet) 
	bestFeatLabel=labesl[bestFeat] #选择的特征
	myTree={bestFeatLabel:{}} #字典的嵌套
	del(labels[bestFeat])
	featValues=[example[bestFeat]for example in dataSet]
	uniqueVals=set(featValues)
	for value in uniqueVals:
		subLabels = labels[:]
		myTree[bestFeatLabel][value]=createTree(splitDataSet(dataSet,bestFeat,value),subLabels) #
	return myTree
'''模拟建树的过程:
s={'no sur':{}}
s['no sur'][0]='no'
ss={'fli':{}}
ss['fli'][0]='no'
ss['fli'][1]='yes'
s['no sur'][1]=ss
>>>s
{'no sur':{0:'no',1:{'fli':{0:'no',1:'yes'}}}}
决策树建成'''

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).