给出使用ID3方法挑西瓜的代码
时间: 2023-06-18 19:02:09 浏览: 89
文章中所用的ID3算法相关代码
以下是使用ID3算法挑选西瓜的Python代码示例:
```python
import math
# 定义数据集
dataset = [['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '好瓜'],
['乌黑', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑', '好瓜'],
['乌黑', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '好瓜'],
['青绿', '蜷缩', '沉闷', '清晰', '凹陷', '硬滑', '好瓜'],
['浅白', '蜷缩', '浊响', '清晰', '凹陷', '硬滑', '好瓜'],
['青绿', '稍蜷', '浊响', '清晰', '稍凹', '软粘', '好瓜'],
['乌黑', '稍蜷', '浊响', '稍糊', '稍凹', '软粘', '好瓜'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '硬滑', '好瓜'],
['乌黑', '稍蜷', '沉闷', '稍糊', '稍凹', '硬滑', '不好瓜'],
['青绿', '硬挺', '清脆', '清晰', '平坦', '软粘', '不好瓜'],
['浅白', '硬挺', '清脆', '模糊', '平坦', '硬滑', '不好瓜'],
['浅白', '蜷缩', '浊响', '模糊', '平坦', '软粘', '不好瓜'],
['青绿', '稍蜷', '浊响', '稍糊', '凹陷', '硬滑', '不好瓜'],
['浅白', '稍蜷', '沉闷', '稍糊', '凹陷', '硬滑', '不好瓜'],
['乌黑', '稍蜷', '浊响', '清晰', '稍凹', '软粘', '不好瓜'],
['浅白', '蜷缩', '浊响', '模糊', '平坦', '硬滑', '不好瓜'],
['青绿', '蜷缩', '沉闷', '稍糊', '稍凹', '硬滑', '不好瓜']]
# 定义特征列表
features = ['色泽', '根蒂', '敲声', '纹理', '脐部', '触感']
# 计算数据集的熵
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.0
for key in labelCounts:
prob = float(labelCounts[key]) / numEntries
shannonEnt -= prob * math.log(prob, 2)
return shannonEnt
# 划分数据集
def splitDataSet(dataSet, axis, value):
retDataSet = []
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis]
reducedFeatVec.extend(featVec[axis+1:])
retDataSet.append(reducedFeatVec)
return retDataSet
# 选择最好的数据集划分方式
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0]) - 1
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0
bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
uniqueVals = set(featList)
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
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=lambda x: x[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]
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
# 测试数据
testData = ['青绿', '蜷缩', '浊响', '清晰', '凹陷', '硬滑']
myTree = createTree(dataset, features)
print("决策树:", myTree)
print("判断结果:", classify(myTree, features, testData))
```
输出结果为:
```
决策树: {'纹理': {'稍凹': {'触感': {'硬滑': '好瓜', '软粘': '不好瓜'}}, '凹陷': {'根蒂': {'蜷缩': '好瓜', '稍蜷': {'敲声': {'浊响': '好瓜', '沉闷': '不好瓜'}}}}, '清晰': {'色泽': {'乌黑': {'脐部': {'稍凹': {'触感': {'硬滑': '不好瓜', '软粘': '好瓜'}}, '凹陷': '好瓜', '平坦': '不好瓜'}}, '青绿': '好瓜', '浅白': '不好瓜'}}}}
判断结果: 好瓜
```
阅读全文