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 — 1 · fciasth/pythonMLAction@46b28fc

File tree

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

‎03/trees.py‎

Copy file name to clipboardExpand all lines: 03/trees.py
+68-16Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,71 @@
33

44
# 预定义的树,用来测试
55
def retrieveTree(i):
6-
listOfTree = [{'no surfacing':{ 0:'no',1:{'flippers': \
7-
{0:'no',1:'yes'}}}},
8-
{'no surfacing':{ 0:'no',1:{'flippers': \
9-
{0:{'head':{0:'no',1:'yes'}},1:'no'}}}}
6+
listOfTree = [{'no surfacing':{ 0:'no',1:{'flippers': {0:'no',1:'yes'}}}},
7+
{'no surfacing':{ 0:'no',1:{'flippers': {0:{'head':{0:'no',1:'yes'}},1:'no'}}}}
108
]
119
return listOfTree[i]
10+
11+
def createDataSet():
12+
dataSet = [[1,1,'maybe'],
13+
[1,1,'yes'],
14+
[1,0,'no'],
15+
[0,1,'no'],
16+
[0,1,'no']]
17+
labels = ['no Surfacing','flippers']
18+
return dataSet,labels
19+
myDat,labels = createDataSet()
20+
#计算给定数据集的熵
21+
def calcShannonEnt(dataSet):
22+
#数据集中所给定的实例数目
23+
dataSetLen = len(dataSet)#5
24+
#数据集中特征数
25+
dataSetClassCount = len(dataSet[0])#3
26+
27+
labelCounts = {}#定义一个空字典
28+
#以列作为循环条件
29+
for vec in dataSet:
30+
#将最后一个值存起来,并判断是否存在空字典中 若不存在 则初始化创建
31+
countLabel = vec[-1]
32+
if countLabel not in labelCounts.keys():
33+
labelCounts[countLabel] =0
34+
labelCounts[countLabel] += 1
35+
shannontEnt =0.0
36+
for key in labelCounts:
37+
prob = float(labelCounts[key])/dataSetLen
38+
shannontEnt -= prob*log(prob,2)
39+
return shannontEnt
40+
41+
a = calcShannonEnt(myDat)
42+
print(a)
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
1271

1372
def calcShannonEnt(dataSet):
1473
#实例总数
@@ -27,15 +86,8 @@ def calcShannonEnt(dataSet):
2786
shannonEnt -= prob * log(prob,2)
2887
return shannonEnt
2988

30-
def createDataSet():
31-
dataSet = [[1,1,'maybe'],
32-
[1,1,'yes'],
33-
[1,0,'no'],
34-
[0,1,'no'],
35-
[0,1,'no']]
36-
labels = ['no Surfacing','flippers']
37-
return dataSet,labels
38-
myDat,labels = createDataSet()
89+
90+
3991
# a= calcShannonEnt(myDat)
4092
# print(a)
4193

@@ -132,6 +184,6 @@ def classify(inputTree,featLabels,testVec):
132184
# 返回类标签
133185
return classLabel
134186

135-
myTree = retrieveTree(1)
136-
a = classify(myTree,labels,[1,1])
137-
print(a)
187+
# myTree = retrieveTree(1)
188+
# a = classify(myTree,labels,[1,1])
189+
# print(a)

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.