GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 创建树的函数代码 · fciasth/pythonMLAction@086d404

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 03
    • tree.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎03/tree.py‎

Copy file name to clipboardExpand all lines: 03/tree.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,22 @@ def majorityCnt(classList):
8181
return sortedClassCount[0][0]
8282

8383
#创建树的函数代码
84+
def createTree(dataSet,labels):
85+
classList = [example[-1] for example in dataSet]
86+
if classList.count(classList[0] == len(classList)):
87+
return classList[0][0]
88+
if len(dataSet[0])==1:
89+
return majorityCnt(dataSet)
90+
bestFeat = chooseBestFeatureToSplit(dataSet)
91+
#print(bestFeat)
92+
bestFeatLabel = labels[bestFeat] #最好的特征的列对应的标签
93+
myTree = {bestFeatLabel:{}}
94+
del(labels[bestFeat])
95+
featValues = [example[bestFeat]for example in dataSet]
96+
uniqueVals = set(featValues)
97+
for value in uniqueVals:
98+
subLabels = labels[:]
99+
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet,bestFeat,value),subLabels)
100+
return myTree
101+
myTree = createTree(myDat,labels)
102+

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.