Skip to content

Commit

Permalink
comment help understand
Browse files Browse the repository at this point in the history
  • Loading branch information
eecn committed Mar 1, 2019
1 parent ea0c9b1 commit 342db46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 30 deletions.
9 changes: 6 additions & 3 deletions Decision Tree/Decision Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ def createTree(dataSet, labels, featLabels):
del(labels[bestFeat]) #删除已经使用特征标签
featValues = [example[bestFeat] for example in dataSet] #得到训练集中所有最优特征的属性值
uniqueVals = set(featValues) #去掉重复的属性值
for value in uniqueVals: #遍历特征,创建决策树。
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), labels, featLabels)
for value in uniqueVals: #遍历特征,创建决策树。
subLabels = labels[:]
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels, featLabels)

return myTree

"""
Expand Down Expand Up @@ -251,7 +253,7 @@ def getTreeDepth(myTree):
"""
def plotNode(nodeTxt, centerPt, parentPt, nodeType):
arrow_args = dict(arrowstyle="<-") #定义箭头格式
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) #设置中文字体
font = FontProperties(fname=r"c:\windows\fonts\simsunb.ttf", size=14) #设置中文字体
createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction', #绘制结点
xytext=centerPt, textcoords='axes fraction',
va="center", ha="center", bbox=nodeType, arrowprops=arrow_args, FontProperties=font)
Expand Down Expand Up @@ -406,6 +408,7 @@ def grabTree(filename):
dataSet, labels = createDataSet()
featLabels = []
myTree = createTree(dataSet, labels, featLabels)
createPlot(myTree)
testVec = [0,1] #测试数据
result = classify(myTree, featLabels, testVec)
if result == 'yes':
Expand Down
4 changes: 2 additions & 2 deletions Naive Bayes/bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def trainNB0(trainMatrix,trainCategory):
for i in range(numTrainDocs):
if trainCategory[i] == 1: #统计属于侮辱类的条件概率所需的数据,即P(w0|1),P(w1|1),P(w2|1)···
p1Num += trainMatrix[i]
p1Denom += sum(trainMatrix[i])
p1Denom += sum(trainMatrix[i]) ## 该词条的总的词数目 这压样求得每个词条出现的概率 P(w1),P(w2), P(w3)...
else: #统计属于非侮辱类的条件概率所需的数据,即P(w0|0),P(w1|0),P(w2|0)···
p0Num += trainMatrix[i]
p0Denom += sum(trainMatrix[i])
Expand All @@ -124,7 +124,7 @@ def trainNB0(trainMatrix,trainCategory):
2017-08-12
"""
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
p1 = reduce(lambda x,y:x*y, vec2Classify * p1Vec) * pClass1 #对应元素相乘
p1 = reduce(lambda x,y:x*y, vec2Classify * p1Vec) * pClass1 #对应元素相乘 这里需要好好理解一下
p0 = reduce(lambda x,y:x*y, vec2Classify * p0Vec) * (1.0 - pClass1)
print('p0:',p0)
print('p1:',p1)
Expand Down
52 changes: 27 additions & 25 deletions kNN/2.海伦约会/kNN_test02.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,38 @@ def classify0(inX, dataSet, labels, k):
"""
def file2matrix(filename):
#打开文件,此次应指定编码,
fr = open(filename,'r',encoding = 'utf-8')

fr = open(filename,'r',encoding = 'utf-8')
#读取文件所有内容
arrayOLines = fr.readlines()
#针对有BOM的UTF-8文本,应该去掉BOM,否则后面会引发错误。
arrayOLines[0]=arrayOLines[0].lstrip('\ufeff')
arrayOLines = fr.readlines()
#针对有BOM的UTF-8文本,应该去掉BOM,否则后面会引发错误。
arrayOLines[0]=arrayOLines[0].lstrip('\ufeff')
#得到文件行数
numberOfLines = len(arrayOLines)
numberOfLines = len(arrayOLines)
#返回的NumPy矩阵,解析完成的数据:numberOfLines行,3列
returnMat = np.zeros((numberOfLines,3))
returnMat = np.zeros((numberOfLines,3))
#返回的分类标签向量
classLabelVector = []
classLabelVector = []
#行的索引值
index = 0
for line in arrayOLines:
index = 0

for line in arrayOLines:
#s.strip(rm),当rm空时,默认删除空白符(包括'\n','\r','\t',' ')
line = line.strip()
line = line.strip()
#使用s.split(str="",num=string,cout(str))将字符串根据'\t'分隔符进行切片。
listFromLine = line.split('\t')
listFromLine = line.split('\t')
#将数据前三列提取出来,存放到returnMat的NumPy矩阵中,也就是特征矩阵
returnMat[index,:] = listFromLine[0:3]
#根据文本中标记的喜欢的程度进行分类,1代表不喜欢,2代表魅力一般,3代表极具魅力
if listFromLine[-1] == 'didntLike':
classLabelVector.append(1)
elif listFromLine[-1] == 'smallDoses':
classLabelVector.append(2)
elif listFromLine[-1] == 'largeDoses':
classLabelVector.append(3)
index += 1
return returnMat, classLabelVector
returnMat[index,:] = listFromLine[0:3]
#根据文本中标记的喜欢的程度进行分类,1代表不喜欢,2代表魅力一般,3代表极具魅力
# 对于datingTestSet2.txt 最后的标签是已经经过处理的 标签已经改为了1, 2, 3
if listFromLine[-1] == 'didntLike':
classLabelVector.append(1)
elif listFromLine[-1] == 'smallDoses':
classLabelVector.append(2)
elif listFromLine[-1] == 'largeDoses':
classLabelVector.append(3)
index += 1
return returnMat, classLabelVector

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

"""
函数说明:分类器测试函数
取百分之十的数据作为测试数据,检测分类器的正确性
Parameters:
Returns:
normDataSet - 归一化后的特征矩阵
ranges - 数据范围
minVals - 数据最小值
Modify:
2017-03-24
Expand Down
36 changes: 36 additions & 0 deletions myREADME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
这是一个很不错的关于《机器学习实战》项目,之前已经将《机器学习实战》书籍看过几遍。现在【2019-2-26】作为简单的回顾配合着本项目的代码和书中的内容,将机器学习实战中的几个常见的算法认真回顾复习一遍。并做一些必要的记录。

引用该项目的作者的同样也是高中激励我努力进步的座右铭

* 贵有恒,何必三更起五更眠;最无益,只怕一日暴十寒。

现在的想法是在复习的过程中着重记录一下自己认为需要着重关注的点。

# 第一章
机器学习相关的术语、主要任务、选择合适的算法、开发机器学习应用的步骤。

numpy 中 array 和 matrix的不同,矩阵有更为特殊的操作。

# 第二章 K近邻算法
该项目的代码解释很全面,KNN算法本身也比较简单。其中的分析数据,使用matplotlib进行数据展示很规整。不同特征数据之间的差异较大可以进行数据归一化操作。

本章回顾了开发机器学习应用程序的步骤:
1. 收集数据
2. 准备数据
3. 分析数据
4. 训练算法
5. 测试算法
6. 使用算法

在今后的不管是工程任务还是类似竞赛科研的比赛任务都应该按照这个流程去考虑问题。

# 第三章 决策树
只适用于标称型数据,数值型数据必须离散化。

信息、信息熵、 数据的一致性与数据的混乱程度可以描述数据的无序程度。

这里决策树的创建,以及决策树的分类执行都涉及到了递归调用。

# 第四章 朴素贝叶斯


0 comments on commit 342db46

Please sign in to comment.