Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python/trees.py at master · fengzi2016/python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
fengzi2016
python
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
python
/
trees.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
105 lines (95 loc) · 4.04 KB
master
Breadcrumbs
python
/
trees.py
Copy path
Top
File metadata and controls
Code
Blame
105 lines (95 loc) · 4.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
Created on Oct 12, 2010
Decision Tree Source Code for Machine Learning in Action Ch. 3
@author: Peter Harrington
'''
from
math
import
log
#导入math模块的log函数
import
operator
#operator 操作符函数
def
createDataSet
():
dataSet
=
[[
1
,
1
,
'yes'
],
[
1
,
1
,
'yes'
],
[
1
,
0
,
'no'
],
[
0
,
1
,
'no'
],
[
0
,
1
,
'no'
]]
labels
=
[
'no surfacing'
,
'flippers'
]
#change to discrete values
return
dataSet
,
labels
def
calcShannonEnt
(
dataSet
):
numEntries
=
len
(
dataSet
)
labelCounts
=
{}
for
featVec
in
dataSet
:
#the the number of unique elements and their occurance
currentLabel
=
featVec
[
-
1
]
if
currentLabel
not
in
labelCounts
.
keys
():
labelCounts
[
currentLabel
]
=
0
labelCounts
[
currentLabel
]
+=
1
shannonEnt
=
0.0
for
key
in
labelCounts
:
prob
=
float
(
labelCounts
[
key
])
/
numEntries
shannonEnt
-=
prob
*
log
(
prob
,
2
)
#log base 2
return
shannonEnt
def
splitDataSet
(
dataSet
,
axis
,
value
):
retDataSet
=
[]
for
featVec
in
dataSet
:
if
featVec
[
axis
]
==
value
:
reducedFeatVec
=
featVec
[:
axis
]
#chop out axis used for splitting
reducedFeatVec
.
extend
(
featVec
[
axis
+
1
:])
retDataSet
.
append
(
reducedFeatVec
)
return
retDataSet
def
chooseBestFeatureToSplit
(
dataSet
):
numFeatures
=
len
(
dataSet
[
0
])
-
1
#the last column is used for the labels
baseEntropy
=
calcShannonEnt
(
dataSet
)
bestInfoGain
=
0.0
;
bestFeature
=
-
1
for
i
in
range
(
numFeatures
):
#iterate over all the features
featList
=
[
example
[
i
]
for
example
in
dataSet
]
#create a list of all the examples of this feature
uniqueVals
=
set
(
featList
)
#get a set of unique values
newEntropy
=
0.0
for
value
in
uniqueVals
:
subDataSet
=
splitDataSet
(
dataSet
,
i
,
value
)
prob
=
len
(
subDataSet
)
/
float
(
len
(
dataSet
))
newEntropy
+=
prob
*
calcShannonEnt
(
subDataSet
)
infoGain
=
baseEntropy
-
newEntropy
#calculate the info gain; ie reduction in entropy
if
(
infoGain
>
bestInfoGain
):
#compare this to the best gain so far
bestInfoGain
=
infoGain
#if better than current best, set to best
bestFeature
=
i
return
bestFeature
#returns an integer
def
majorityCnt
(
classList
):
classCount
=
{}
for
vote
in
classList
:
if
vote
not
in
classCount
.
keys
():
classCount
[
vote
]
=
0
classCount
[
vote
]
+=
1
sortedClassCount
=
sorted
(
classCount
.
iteritems
(),
key
=
operator
.
itemgetter
(
1
),
reverse
=
True
)
return
sortedClassCount
[
0
][
0
]
def
createTree
(
dataSet
,
labels
):
classList
=
[
example
[
-
1
]
for
example
in
dataSet
]
if
classList
.
count
(
classList
[
0
])
==
len
(
classList
):
return
classList
[
0
]
#stop splitting when all of the classes are equal
if
len
(
dataSet
[
0
])
==
1
:
#stop splitting when there are no more features in dataSet
return
majorityCnt
(
classList
)
bestFeat
=
chooseBestFeatureToSplit
(
dataSet
)
bestFeatLabel
=
labels
[
bestFeat
]
myTree
=
{
bestFeatLabel
:{}}
del
(
labels
[
bestFeat
])
featValues
=
[
example
[
bestFeat
]
for
example
in
dataSet
]
uniqueVals
=
set
(
featValues
)
for
value
in
uniqueVals
:
subLabels
=
labels
[:]
#copy all of labels, so trees don't mess up existing labels
myTree
[
bestFeatLabel
][
value
]
=
createTree
(
splitDataSet
(
dataSet
,
bestFeat
,
value
),
subLabels
)
return
myTree
def
classify
(
inputTree
,
featLabels
,
testVec
):
firstStr
=
inputTree
.
keys
()[
0
]
secondDict
=
inputTree
[
firstStr
]
featIndex
=
featLabels
.
index
(
firstStr
)
key
=
testVec
[
featIndex
]
valueOfFeat
=
secondDict
[
key
]
if
isinstance
(
valueOfFeat
,
dict
):
classLabel
=
classify
(
valueOfFeat
,
featLabels
,
testVec
)
else
:
classLabel
=
valueOfFeat
return
classLabel
def
storeTree
(
inputTree
,
filename
):
import
pickle
fw
=
open
(
filename
,
'w'
)
pickle
.
dump
(
inputTree
,
fw
)
fw
.
close
()
def
grabTree
(
filename
):
import
pickle
fr
=
open
(
filename
)
return
pickle
.
load
(
fr
)
You can’t perform that action at this time.