Skip to content

Commit 342db46

Browse files
committed
comment help understand
1 parent ea0c9b1 commit 342db46

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

Decision Tree/Decision Tree.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ def createTree(dataSet, labels, featLabels):
179179
del(labels[bestFeat]) #删除已经使用特征标签
180180
featValues = [example[bestFeat] for example in dataSet] #得到训练集中所有最优特征的属性值
181181
uniqueVals = set(featValues) #去掉重复的属性值
182-
for value in uniqueVals: #遍历特征,创建决策树。
183-
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), labels, featLabels)
182+
for value in uniqueVals: #遍历特征,创建决策树。
183+
subLabels = labels[:]
184+
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels, featLabels)
185+
184186
return myTree
185187

186188
"""
@@ -251,7 +253,7 @@ def getTreeDepth(myTree):
251253
"""
252254
def plotNode(nodeTxt, centerPt, parentPt, nodeType):
253255
arrow_args = dict(arrowstyle="<-") #定义箭头格式
254-
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) #设置中文字体
256+
font = FontProperties(fname=r"c:\windows\fonts\simsunb.ttf", size=14) #设置中文字体
255257
createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction', #绘制结点
256258
xytext=centerPt, textcoords='axes fraction',
257259
va="center", ha="center", bbox=nodeType, arrowprops=arrow_args, FontProperties=font)
@@ -406,6 +408,7 @@ def grabTree(filename):
406408
dataSet, labels = createDataSet()
407409
featLabels = []
408410
myTree = createTree(dataSet, labels, featLabels)
411+
createPlot(myTree)
409412
testVec = [0,1] #测试数据
410413
result = classify(myTree, featLabels, testVec)
411414
if result == 'yes':

Naive Bayes/bayes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def trainNB0(trainMatrix,trainCategory):
9797
for i in range(numTrainDocs):
9898
if trainCategory[i] == 1: #统计属于侮辱类的条件概率所需的数据,即P(w0|1),P(w1|1),P(w2|1)···
9999
p1Num += trainMatrix[i]
100-
p1Denom += sum(trainMatrix[i])
100+
p1Denom += sum(trainMatrix[i]) ## 该词条的总的词数目 这压样求得每个词条出现的概率 P(w1),P(w2), P(w3)...
101101
else: #统计属于非侮辱类的条件概率所需的数据,即P(w0|0),P(w1|0),P(w2|0)···
102102
p0Num += trainMatrix[i]
103103
p0Denom += sum(trainMatrix[i])
@@ -124,7 +124,7 @@ def trainNB0(trainMatrix,trainCategory):
124124
2017-08-12
125125
"""
126126
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
127-
p1 = reduce(lambda x,y:x*y, vec2Classify * p1Vec) * pClass1 #对应元素相乘
127+
p1 = reduce(lambda x,y:x*y, vec2Classify * p1Vec) * pClass1 #对应元素相乘 这里需要好好理解一下
128128
p0 = reduce(lambda x,y:x*y, vec2Classify * p0Vec) * (1.0 - pClass1)
129129
print('p0:',p0)
130130
print('p1:',p1)

kNN/2.海伦约会/kNN_test02.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,38 @@ def classify0(inX, dataSet, labels, k):
6666
"""
6767
def file2matrix(filename):
6868
#打开文件,此次应指定编码,
69-
fr = open(filename,'r',encoding = 'utf-8')
69+
70+
fr = open(filename,'r',encoding = 'utf-8')
7071
#读取文件所有内容
71-
arrayOLines = fr.readlines()
72-
#针对有BOM的UTF-8文本,应该去掉BOM,否则后面会引发错误。
73-
arrayOLines[0]=arrayOLines[0].lstrip('\ufeff')
72+
arrayOLines = fr.readlines()
73+
#针对有BOM的UTF-8文本,应该去掉BOM,否则后面会引发错误。
74+
arrayOLines[0]=arrayOLines[0].lstrip('\ufeff')
7475
#得到文件行数
75-
numberOfLines = len(arrayOLines)
76+
numberOfLines = len(arrayOLines)
7677
#返回的NumPy矩阵,解析完成的数据:numberOfLines行,3列
77-
returnMat = np.zeros((numberOfLines,3))
78+
returnMat = np.zeros((numberOfLines,3))
7879
#返回的分类标签向量
79-
classLabelVector = []
80+
classLabelVector = []
8081
#行的索引值
81-
index = 0
82-
for line in arrayOLines:
82+
index = 0
83+
84+
for line in arrayOLines:
8385
#s.strip(rm),当rm空时,默认删除空白符(包括'\n','\r','\t',' ')
84-
line = line.strip()
86+
line = line.strip()
8587
#使用s.split(str="",num=string,cout(str))将字符串根据'\t'分隔符进行切片。
86-
listFromLine = line.split('\t')
88+
listFromLine = line.split('\t')
8789
#将数据前三列提取出来,存放到returnMat的NumPy矩阵中,也就是特征矩阵
88-
returnMat[index,:] = listFromLine[0:3]
89-
#根据文本中标记的喜欢的程度进行分类,1代表不喜欢,2代表魅力一般,3代表极具魅力
90-
if listFromLine[-1] == 'didntLike':
91-
classLabelVector.append(1)
92-
elif listFromLine[-1] == 'smallDoses':
93-
classLabelVector.append(2)
94-
elif listFromLine[-1] == 'largeDoses':
95-
classLabelVector.append(3)
96-
index += 1
97-
return returnMat, classLabelVector
90+
returnMat[index,:] = listFromLine[0:3]
91+
#根据文本中标记的喜欢的程度进行分类,1代表不喜欢,2代表魅力一般,3代表极具魅力
92+
# 对于datingTestSet2.txt 最后的标签是已经经过处理的 标签已经改为了1, 2, 3
93+
if listFromLine[-1] == 'didntLike':
94+
classLabelVector.append(1)
95+
elif listFromLine[-1] == 'smallDoses':
96+
classLabelVector.append(2)
97+
elif listFromLine[-1] == 'largeDoses':
98+
classLabelVector.append(3)
99+
index += 1
100+
return returnMat, classLabelVector
98101

99102
"""
100103
函数说明:可视化数据
@@ -109,7 +112,7 @@ def file2matrix(filename):
109112
"""
110113
def showdatas(datingDataMat, datingLabels):
111114
#设置汉字格式
112-
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
115+
font = FontProperties(fname=r"c:\windows\fonts\simsunb.ttf", size=14) ##需要查看自己的电脑是否会包含该字体
113116
#将fig画布分隔成1行1列,不共享x轴和y轴,fig画布的大小为(13,8)
114117
#当nrow=2,nclos=2时,代表fig画布被分为四个区域,axs[0][0]表示第一行第一个区域
115118
fig, axs = plt.subplots(nrows=2, ncols=2,sharex=False, sharey=False, figsize=(13,8))
@@ -200,13 +203,12 @@ def autoNorm(dataSet):
200203

201204
"""
202205
函数说明:分类器测试函数
206+
取百分之十的数据作为测试数据,检测分类器的正确性
203207
204208
Parameters:
205209
206210
Returns:
207-
normDataSet - 归一化后的特征矩阵
208-
ranges - 数据范围
209-
minVals - 数据最小值
211+
210212
211213
Modify:
212214
2017-03-24

myREADME.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
这是一个很不错的关于《机器学习实战》项目,之前已经将《机器学习实战》书籍看过几遍。现在【2019-2-26】作为简单的回顾配合着本项目的代码和书中的内容,将机器学习实战中的几个常见的算法认真回顾复习一遍。并做一些必要的记录。
2+
3+
引用该项目的作者的同样也是高中激励我努力进步的座右铭
4+
5+
* 贵有恒,何必三更起五更眠;最无益,只怕一日暴十寒。
6+
7+
现在的想法是在复习的过程中着重记录一下自己认为需要着重关注的点。
8+
9+
# 第一章
10+
机器学习相关的术语、主要任务、选择合适的算法、开发机器学习应用的步骤。
11+
12+
numpy 中 array 和 matrix的不同,矩阵有更为特殊的操作。
13+
14+
# 第二章 K近邻算法
15+
该项目的代码解释很全面,KNN算法本身也比较简单。其中的分析数据,使用matplotlib进行数据展示很规整。不同特征数据之间的差异较大可以进行数据归一化操作。
16+
17+
本章回顾了开发机器学习应用程序的步骤:
18+
1. 收集数据
19+
2. 准备数据
20+
3. 分析数据
21+
4. 训练算法
22+
5. 测试算法
23+
6. 使用算法
24+
25+
在今后的不管是工程任务还是类似竞赛科研的比赛任务都应该按照这个流程去考虑问题。
26+
27+
# 第三章 决策树
28+
只适用于标称型数据,数值型数据必须离散化。
29+
30+
信息、信息熵、 数据的一致性与数据的混乱程度可以描述数据的无序程度。
31+
32+
这里决策树的创建,以及决策树的分类执行都涉及到了递归调用。
33+
34+
# 第四章 朴素贝叶斯
35+
36+

0 commit comments

Comments
 (0)