import imp
import trees
imp.reload(trees)
结论:已经导入过的模块才能用reload, reload的参数应该是模块名,而不是文件名。在pyhton3.x中要先导入文件和imp模块,才能用imp.reload()
原因是标题默认输出英文,如果输出中文,要对字体进行调整。需要在程序定义前输入:
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
这个问题是python版本的问题
#如果使用的是python2
firstStr = myTree.keys()[0]
#LZ使用的是python3
firstSides = list(myTree.keys())
firstStr = firstSides[0]
解决办法就是先转换成list,再把需要的索引提取出来。
Traceback (most recent call last):
File "
trees.storeTree(myTree,'classifierStorage.txt')
File "C:\Users\34856\Desktop\trees.py", line 97, in storeTree
pickle.dump(inputTree,fw)
TypeError: write() argument must be str, not bytes
解决方法:
fw = open(filename, 'w')
改为
fw = open(filename, 'wb')
完整代码如下
# 在磁盘上存储 决策树对象 用于节省时间
def storeTree(inputTree, filename):
import pickle
fw = open(filename, 'wb')
pickle.dump(inputTree, fw)
fw.close()
继续运行
Traceback (most recent call last):
File "
trees.grabTree('classifierStorage.txt')
File "C:\Users\34856\Desktop\trees.py", line 103, in grabTree
return pickle.load(fr)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
解决方法:
fr = open(filename)
改为
fr = open(filename, 'rb')
完整代码:
# 加载存储在磁盘上的 决策树
def grabTree(filename):
import pickle
fr = open(filename, 'rb')
return pickle.load(fr)
Python3.x中:iteritems变为items
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import treePlotter
Traceback (most recent call last):
File "", line 1, in
import treePlotter
ModuleNotFoundError: No module named 'treePlotter'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot()
File "C:\Users\34856\Desktop\treePlotter.py", line 16, in createPlot
createPlot.ax1 = plt.subplot(111,frmaeon = Flase)
NameError: name 'Flase' is not defined
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot()
File "C:\Users\34856\Desktop\treePlotter.py", line 16, in createPlot
createPlot.ax1 = plt.subplot(111,frmaeon = False)
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 1103, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\figure.py", line 1370, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_subplots.py", line 70, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_base.py", line 498, in __init__
self.update(kwargs)
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 916, in update
ret = [_update_property(self, k, v) for k, v in props.items()]
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 916, in
ret = [_update_property(self, k, v) for k, v in props.items()]
File "C:\Users\34856\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 912, in _update_property
raise AttributeError('Unknown property %s' % k)
AttributeError: Unknown property frmaeon
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>> treePlotter.createPlot()
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
>>> import imp
>>> imp.reload(treePlotter)
>>> treePlotterr.retrieveTree(1)
Traceback (most recent call last):
File "", line 1, in
treePlotterr.retrieveTree(1)
NameError: name 'treePlotterr' is not defined
>>> treePlotter.retrieveTree(1)
Traceback (most recent call last):
File "", line 1, in
treePlotter.retrieveTree(1)
AttributeError: module 'treePlotter' has no attribute 'retrieveTree'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.retrieveTree(1)
{'no surfacing': {0: 'no', 1: {'flippers': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}}
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.getNumLeafs(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.getNumLeafs(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 26, in getNumLeafs
firstStr = myTree.keys()[0]
TypeError: 'dict_keys' object does not support indexing
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> mport treePlotter
SyntaxError: invalid syntax
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.getNumLeafs(myTree)
3
>>> treePlotter.getTreeDepth(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.getTreeDepth(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 37, in getTreeDepth
firstStr = myTree.keys()[0]
TypeError: 'dict_keys' object does not support indexing
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.getNumLeafs(myTree)
3
>>> treePlotter.getTreeDepth(myTree)
2
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree =treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
TypeError: createPlot() takes 0 positional arguments but 1 was given
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> firstSides = list(myTree.keys())
firstStr = firstSides[0]
SyntaxError: multiple statements found while compiling a single statement
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 86, in createPlot
plotTree.totalD = float(getTreeDepth(inThree))
NameError: name 'inThree' is not defined
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 88, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 66, in plotTree
plotMidText(cntrPt,parenPt,nodeTxt)
NameError: name 'parenPt' is not defined
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 88, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 66, in plotTree
plotMidText(cntrPt,parentPt,nodeTxt)
File "C:\Users\34856\Desktop\treePlotter.py", line 57, in plotMidText
createPlot.ax1.test(xMid,yMid,txtString)
AttributeError: 'function' object has no attribute 'ax1'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 88, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 66, in plotTree
plotMidText(cntrPt,parentPt,nodeTxt)
File "C:\Users\34856\Desktop\treePlotter.py", line 57, in plotMidText
createPlot.ax1.text(xMid,yMid,txtString)
AttributeError: 'function' object has no attribute 'ax1'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 88, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 66, in plotTree
plotMidText(cntrPt,parentPt,nodeTxt)
File "C:\Users\34856\Desktop\treePlotter.py", line 57, in plotMidText
createPlot.ax1.text(xMid,yMid,txtString)
AttributeError: 'function' object has no attribute 'ax1'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 88, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 66, in plotTree
plotMidText(cntrPt,parentPt,nodeTxt)
File "C:\Users\34856\Desktop\treePlotter.py", line 57, in plotMidText
createPlot.ax1.text(xMid,yMid,txtString)
AttributeError: 'function' object has no attribute 'ax1'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(myTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 81, in createPlot
plotTree(inTree,(0.5,1.0),'')
File "C:\Users\34856\Desktop\treePlotter.py", line 59, in plotTree
plotMidText(cntrPt,parentPt,nodeTxt)
File "C:\Users\34856\Desktop\treePlotter.py", line 50, in plotMidText
createPlot.ax1.text(xMid,yMid,txtString)
AttributeError: 'function' object has no attribute 'ax1'
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> treePlotter.createPlot(myTree)
>>> myTree['no surfacing'][3] = 'maybe'
>>> myTree
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}, 3: 'maybe'}}
>>> treePlotter.createPlot(myTree)
>>>
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> myTree= treePlotter.retrieveTree(0)
>>> myTree
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
NameError: name 'trees' is not defined
>>> myDat,labels = trees.creatDataSet()
Traceback (most recent call last):
File "", line 1, in
myDat,labels = trees.creatDataSet()
NameError: name 'trees' is not defined
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> myDat,labels=trees.creatDataSet()
Traceback (most recent call last):
File "", line 1, in
myDat,labels=trees.creatDataSet()
AttributeError: module 'trees' has no attribute 'creatDataSet'
>>> myDat,labels=trees.createDataSet()
>>> labels
['no surfacing', 'flippers']
>>> myTree = treePlotter.retrieveTree(0)
Traceback (most recent call last):
File "", line 1, in
myTree = treePlotter.retrieveTree(0)
NameError: name 'treePlotter' is not defined
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
>>> myTree
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}
>>> myTree = treePlotter.retrieveTree(1)
>>> myTree
{'no surfacing': {0: 'no', 1: {'flippers': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}}
>>> treePlotter.createPlot(myTree)
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
File "C:\Users\34856\Desktop\trees.py", line 83, in classify
firstSides = list(myTree.keys())
NameError: name 'myTree' is not defined
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
SyntaxError: unexpected indent
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
File "C:\Users\34856\Desktop\trees.py", line 83, in classify
firstSides = list(myTree.keys())
NameError: name 'myTree' is not defined
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> myTree = treePlotter.retrieveTree(0)
Traceback (most recent call last):
File "", line 1, in
myTree = treePlotter.retrieveTree(0)
NameError: name 'treePlotter' is not defined
>>> import treePlotter
>>> myDat,labels=trees.createDataSet()
>>> myTree = treePlotter.retrieveTree(1)
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
File "C:\Users\34856\Desktop\trees.py", line 92, in classify
return classLabel
UnboundLocalError: local variable 'classLabel' referenced before assignment
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> import treePlotter
>>> myDat,labels=trees.createDataSet()
>>> myTree = treePlotter.retrieveTree(1)
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
File "C:\Users\34856\Desktop\trees.py", line 92, in classify
return classLabel
UnboundLocalError: local variable 'classLabel' referenced before assignment
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> import treePlotter
>>> myDat,labels=trees.createDataSet()
>>> myTree = treePlotter.retrieveTree(1)
>>>
>>> trees.classify(myTree,labels,[1,0])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,0])
File "C:\Users\34856\Desktop\trees.py", line 90, in classify
classLabel = classify(secondDict[key],featLabels,testVec)
File "C:\Users\34856\Desktop\trees.py", line 90, in classify
classLabel = classify(secondDict[key],featLabels,testVec)
File "C:\Users\34856\Desktop\trees.py", line 86, in classify
featIndex = featLabels.index(firstStr)
ValueError: 'head' is not in list
>>> myTree = treePlotter.retrieveTree(1)
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.classify(myTree,labels,[1,0])
'no'
>>> trees.classify(myTree,labels,[1,1])
'yes'
>>> trees.classify(myTree,labels,[1,2])
Traceback (most recent call last):
File "", line 1, in
trees.classify(myTree,labels,[1,2])
File "C:\Users\34856\Desktop\trees.py", line 90, in classify
classLabel = classify(secondDict[key],featLabels,testVec)
File "C:\Users\34856\Desktop\trees.py", line 92, in classify
return classLabel
UnboundLocalError: local variable 'classLabel' referenced before assignment
>>> trees.classify(myTree,labels,[0,0])
'no'
>>> trees.classify(myTree,labels,[0,1])
'no'
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> trees.storeTree(myTree,'classifierStorage.txt')
Traceback (most recent call last):
File "", line 1, in
trees.storeTree(myTree,'classifierStorage.txt')
NameError: name 'trees' is not defined
>>> import trees
>>> trees.storeTree(myTree,'classifierStorage.txt')
Traceback (most recent call last):
File "", line 1, in
trees.storeTree(myTree,'classifierStorage.txt')
NameError: name 'myTree' is not defined
>>> myTree = treePlotter.retrieveTree(0)
Traceback (most recent call last):
File "", line 1, in
myTree = treePlotter.retrieveTree(0)
NameError: name 'treePlotter' is not defined
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.storeTree(myTree,'classifierStorage.txt')
Traceback (most recent call last):
File "", line 1, in
trees.storeTree(myTree,'classifierStorage.txt')
File "C:\Users\34856\Desktop\trees.py", line 97, in storeTree
pickle.dump(inputTree,fw)
TypeError: write() argument must be str, not bytes
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.storeTree(myTree,'classifierStorage.txt')
>>> trees.grabTree('classifierStorage.txt')
Traceback (most recent call last):
File "", line 1, in
trees.grabTree('classifierStorage.txt')
File "C:\Users\34856\Desktop\trees.py", line 103, in grabTree
return pickle.load(fr)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.storeTree(myTree,'classifierStorage.txt')
>>> trees.grabTree('classifierStorage.txt')
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}
>>>
>>> fr = open('lenses.txt')
>>> lenses = [list.strip().split('\t')for inst in fr.readline()]
Traceback (most recent call last):
File "", line 1, in
lenses = [list.strip().split('\t')for inst in fr.readline()]
File "", line 1, in
lenses = [list.strip().split('\t')for inst in fr.readline()]
AttributeError: type object 'list' has no attribute 'strip'
>>> lenses = [inst.strip().split('\t')for inst in fr.readline()]
>>> lensesLabels = ['age','perscript','astigmatic','tearRate']
>>> lensesTree = trees.creatrTree(lenses,lensesLabels)
Traceback (most recent call last):
File "", line 1, in
lensesTree = trees.creatrTree(lenses,lensesLabels)
AttributeError: module 'trees' has no attribute 'creatrTree'
>>> lensesTree = trees.createTree(lenses,lensesLabels)
Traceback (most recent call last):
File "", line 1, in
lensesTree = trees.createTree(lenses,lensesLabels)
AttributeError: module 'trees' has no attribute 'createTree'
>>> lensesTree = trees.createTrees(lenses,lensesLabels)
Traceback (most recent call last):
File "", line 1, in
lensesTree = trees.createTrees(lenses,lensesLabels)
File "C:\Users\34856\Desktop\trees.py", line 69, in createTrees
return majorityCnt(classList)
File "C:\Users\34856\Desktop\trees.py", line 60, in majorityCnt
sortedClassCount = sorted(classCount.iteritems(),\
AttributeError: 'dict' object has no attribute 'iteritems'
>>>
================== RESTART: C:\Users\34856\Desktop\trees.py ==================
>>> import trees
>>> import treePlotter
>>> myTree = treePlotter.retrieveTree(0)
>>> trees.storeTree(myTree,'classifierStorage.txt')
>>> trees.grabTree('classifierStorage.txt')
{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}
>>> fr = open('lenses.txt')
>>> lenses = [list.strip().split('\t')for inst in fr.readline()]
Traceback (most recent call last):
File "", line 1, in
lenses = [list.strip().split('\t')for inst in fr.readline()]
File "", line 1, in
lenses = [list.strip().split('\t')for inst in fr.readline()]
AttributeError: type object 'list' has no attribute 'strip'
>>> lenses = [inst.strip().split('\t')for inst in fr.readline()]
>>> lensesLabels = ['age','perscript','astigmatic','tearRate']
>>> lensesTree = trees.creatrTree(lenses,lensesLabels)
Traceback (most recent call last):
File "", line 1, in
lensesTree = trees.creatrTree(lenses,lensesLabels)
AttributeError: module 'trees' has no attribute 'creatrTree'
>>> lensesTree = trees.createTrees(lenses,lensesLabels)
>>> lensesTree
'o'
>>> treePlotter.createPlot(lensesTree)
Traceback (most recent call last):
File "", line 1, in
treePlotter.createPlot(lensesTree)
File "C:\Users\34856\Desktop\treePlotter.py", line 77, in createPlot
plotTree.totalW = float(getNumLeafs(inTree))
File "C:\Users\34856\Desktop\treePlotter.py", line 18, in getNumLeafs
firstSides = list(myTree.keys())
AttributeError: 'str' object has no attribute 'keys'
>>> myDat,labels=trees.createDataSet()
>>>