|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2020-11-5 16:59 编辑
参考书籍:《机器学习实战》
- import numpy as np
- import operator
- import matplotlib.pyplot as plt
- from math import log
- def calcShannonEnt(dataSet):
- numEntries = len(dataSet)
- labelCounts = {}
- for featVec in dataSet:
- currentLabel = featVec[-1]
- if currentLabel not in labelCounts.keys():
- labelCounts[currentLabel] = 0
- labelCounts[currentLabel] += 1
- shannonEnt = 0
- for key in labelCounts:
- prob = float(labelCounts[key]) / numEntries
- shannonEnt -= prob * log(prob, 2)#以2为底数
- return shannonEnt
- #根据特征划分数据集
- def splitDataSet(dataSet, axis, value):
- retDataSet = []
- for featVec in dataSet:
- if featVec[axis] == value:
- reduceFeatVec = featVec[:axis]#不包括axis
- reduceFeatVec.extend(featVec[axis + 1 :])
- retDataSet.append(reduceFeatVec)
- return retDataSet
- #选择最好的数据集划分方式
- def chooseBestFeatureToSplit(dataSet):
- numFeatures = len(dataSet[0]) - 1
- baseEntropy = calcShannonEnt(dataSet)
- bestInfoGain = 0
- bestFeature = -1
- for i in range(numFeatures):#每个特征单独计算
- featList = [example[i] for example in dataSet]
- uniqueVals = set(featList)
- newEntropy = 0
- for value in uniqueVals:
- subDataSet = splitDataSet(dataSet, i, value)
- prob = len(subDataSet) / float(len(dataSet))
- newEntropy += prob * calcShannonEnt(subDataSet)
- infoGain = baseEntropy - newEntropy
- if(infoGain > bestInfoGain):
- bestInfoGain = infoGain
- bestFeature = i
- return bestFeature
- #数据集已经处理了所有特征,但类标签依然不是唯一的,采用多数表决的方法确定返回的类
- def majorityCnt(classList):
- classCount = {}
- for vote in classList:
- if vote not in classCount.keys():
- classCount[vote] = 0
- classCount[vote] += 1
- sortedClassCount = sorted(classCount.items(), 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):#count()方法用于统计某个元素在列表中出现的次数。
- return classList[0]
- if len(dataSet[0]) == 1:
- 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[:]
- myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value),
- subLabels)
- return myTree
- def classify(inputTree, featLabels, testVec):
- firstStr = list(inputTree.keys())[0]
- secondDict = inputTree[firstStr]
- featIndex = featLabels.index(firstStr)
- for key in secondDict.keys():
- if testVec[featIndex] == key:
- if type(secondDict[key]).__name__ == "dict":#如果是字典的话,接着向下找
- classLabel = classify(secondDict[key], featLabels, testVec)
- else:
- classLabel = secondDict[key]
- return classLabel
- if __name__ == '__main__':
- dataSet = [[1, 1, "yes"],
- [1, 1, "yes"],
- [1, 0, "no"],
- [0, 1, "no"],
- [0, 1, "no"]]
- labels = ["no surfacing", "flippers"]
- tree = createTree(dataSet, labels)
- labels = ["no surfacing", "flippers"]#因为createTree阶段会删除labels中的值
- res = classify(tree, labels, [1, 1])
- print(res)
复制代码 |
|