# coding=utf-8
import operator
from math import log
import time
import load_bigcsv as ls
# import load_csv as ls
def createDataSet():
dataSet = [[1 1 ‘yes‘]
[1 1 ‘yes‘]
[1 0 ‘no‘]
[0 1 ‘no‘]
[0 1 ‘no‘]]
labels = [‘no surfaceing‘ ‘flippers‘]
return dataSet labels
# 计算香农熵
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
for feaVec in dataSet:
currentLabel = feaVec[-1]
if currentLabel not in labelCounts:
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
shannonEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key]) / numEntries
shannonEnt -= prob * 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
return max(classCount)
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 main():
# data label = createDataSet()
data = ls.data2.tolist()
label = ls.label
# print(type(data))
# print(type(label))
t1 = t
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-07-23 16:47 seventh_homework\
目录 0 2018-12-11 23:19 seventh_homework\7_guanlianguice\
目录 0 2018-12-05 21:42 seventh_homework\7_guanlianguice\Bigmart\
文件 3474 2018-10-10 23:47 seventh_homework\7_guanlianguice\Bigmart\read.txt
文件 85268 2018-10-10 23:24 seventh_homework\7_guanlianguice\Bigmart\SampleSubmission_TmnO39y.csv
文件 527709 2018-10-10 23:18 seventh_homework\7_guanlianguice\Bigmart\Test_u94Q5KV.csv
文件 869537 2018-10-10 23:18 seventh_homework\7_guanlianguice\Bigmart\Train_UWu5bXk.csv
目录 0 2018-12-05 21:42 seventh_homework\7_guanlianguice\black_friday\
文件 2009 2018-10-10 23:39 seventh_homework\7_guanlianguice\black_friday\readme.txt
文件 29 2018-10-10 23:35 seventh_homework\7_guanlianguice\black_friday\Sample_Submission_Tm9Lura.csv
文件 9598228 2015-11-20 00:45 seventh_homework\7_guanlianguice\black_friday\test.csv
文件 25525678 2015-11-20 00:47 seventh_homework\7_guanlianguice\black_friday\train.csv
文件 18939 2018-12-11 18:15 seventh_homework\7_guanlianguice\daima.docx
文件 3478 2018-12-11 18:12 seventh_homework\7_guanlianguice\jueceshu.py
文件 1628 2018-12-11 18:14 seventh_homework\7_guanlianguice\load_bigcsv.py
文件 1690 2018-12-11 18:08 seventh_homework\7_guanlianguice\load_csv.py
目录 0 2018-12-11 21:53 seventh_homework\7_guanlianguice\pict\
文件 80381 2018-12-11 21:01 seventh_homework\7_guanlianguice\pict\bigmart_10data.png
文件 52375 2018-12-11 21:02 seventh_homework\7_guanlianguice\pict\bigmart_all_data.png
文件 327607 2018-12-11 16:30 seventh_homework\7_guanlianguice\pict\black_10.png
文件 34241 2018-12-11 21:03 seventh_homework\7_guanlianguice\pict\black_100.png
文件 68786 2018-12-11 21:05 seventh_homework\7_guanlianguice\pict\black_1000.png
文件 88850 2018-12-11 21:04 seventh_homework\7_guanlianguice\pict\black_10000.png
文件 12070 2018-12-11 21:03 seventh_homework\7_guanlianguice\pict\black关键属性.png
文件 171499 2018-12-11 21:22 seventh_homework\7_guanlianguice\pict\black处理后数据.png
文件 11897 2018-12-11 21:00 seventh_homework\7_guanlianguice\pict\Mart关键属性.png
文件 91050 2018-12-11 16:32 seventh_homework\7_guanlianguice\pict\Screenshot from 2018-12-11 16-32-03.png
文件 85403 2018-12-11 21:15 seventh_homework\7_guanlianguice\pict\数据1.png
文件 58939 2018-12-11 21:21 seventh_homework\7_guanlianguice\pict\数据10.png
文件 79858 2018-12-11 21:17 seventh_homework\7_guanlianguice\pict\数据2.png
文件 17647 2018-12-11 21:17 seventh_homework\7_guanlianguice\pict\数据34.png
............此处省略14个文件信息