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@84877c6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 03
    • trees.py
  • 04
    • bayes.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
+27-7Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ def createDataSet():
3939
# a= calcShannonEnt(myDat)
4040
# print(a)
4141

42+
4243
#划分数据集
43-
#三个输入参数:待划分的数据集、划分数据集的特征、需要返回的特征的值
44+
#dataSet 数据集 axis 划分特征为第几列 value 划分特征的值
45+
4446
def splitDataSet(dataSet,axis,value):
4547
# Python语言不用考虑内存分配问题。 Python语言在函数中传递的是列表的引用,
4648
# 在函数内部对列表对象的修改,将会影响该列表对象的整个生存周期。为了消除这
@@ -56,8 +58,25 @@ def splitDataSet(dataSet,axis,value):
5658
#上面两行就是把作为特征的一列去掉了
5759
retDataSet.append(reducedFeatVec)
5860
return retDataSet
59-
a= splitDataSet(myDat,2,"no")
60-
#print(a)
61+
# a= splitDataSet(myDat,2,"no")
62+
# print(a)
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
6180

6281
#选择最好的数据集划分方式
6382
def chooseBestFeatureToSplit(dataSet):
@@ -71,13 +90,14 @@ def chooseBestFeatureToSplit(dataSet):
7190
for value in uniqueVals:
7291
subDataSet = splitDataSet(dataSet,i,value)
7392
prob = len(subDataSet)/float(len(dataSet))
93+
7494
newEntropy += prob*calcShannonEnt(subDataSet)
7595
infoGain = baseEntropy -newEntropy
7696
if(infoGain>bestInfoGain):
7797
bestInfoGain = infoGain
7898
bestFeature = i
7999
return bestFeature
80-
b = chooseBestFeatureToSplit(myDat)
100+
b = chooseBestFeatureToList(myDat)
81101
# print(b)
82102
#采用多数表决的方法决定该叶子节点的分类。
83103
def majorityCnt(classList):
@@ -132,6 +152,6 @@ def classify(inputTree,featLabels,testVec):
132152
# 返回类标签
133153
return classLabel
134154

135-
myTree = retrieveTree(1)
136-
a = classify(myTree,labels,[1,1])
137-
print(a)
155+
# myTree = retrieveTree(1)
156+
# a = classify(myTree,labels,[1,1])
157+
# print(a)
Collapse file

‎04/bayes.py‎

Copy file name to clipboardExpand all lines: 04/bayes.py
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,15 @@ def setOfWords2Vec(vocabList,inputSet):#输入参数为词汇表及某个文档
5050

5151
# 条件概率的计算
5252
def trainNB0(trainMatrix,trainCategory): #输入参数为文档矩阵trainMatrix,文档类别所构成的向量trainCategory
53-
#
54-
numTrainDocs = len()
53+
#计算文档的数目
54+
numTrainDocs = len(trainMatrix)
55+
print(numTrainDocs)
56+
# 计算单词的数目
57+
numWords = len(trainMatrix[0])
58+
59+
trainMat = []
60+
for postinDoc in listOPosts:
61+
trainMat.append(setOfWords2Vec(myVocabList, postinDoc))
62+
print(trainMat)
63+
trainNB0(array(trainMat),array(listClasses))
64+

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.