机器学习实战——决策树Python实现问题记录

问题:NameError: name 'reload' is not defined

import imp
import trees
imp.reload(trees)

结论:已经导入过的模块才能用reload, reload的参数应该是模块名,而不是文件名。在pyhton3.x中要先导入文件和imp模块,才能用imp.reload()

问题:关于Plot图像不显示中文文字问题

原因是标题默认输出英文,如果输出中文,要对字体进行调整。需要在程序定义前输入:

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

问题:TypeError: ‘dict_keys’ object does not support indexing

这个问题是python版本的问题

#如果使用的是python2
firstStr = myTree.keys()[0]
#LZ使用的是python3
firstSides = list(myTree.keys())
firstStr = firstSides[0]

解决办法就是先转换成list,再把需要的索引提取出来。

问题:Python3文件读写错误 TypeError: write() argument must be str, not bytes

Traceback (most recent call last):
  File "<pyshell#121>", line 1, in <module>
    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()

继续运行

出现错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

Traceback (most recent call last):
  File "<pyshell#126>", line 1, in <module>
    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)

问题:AttributeError: 'dict' object has no attribute 'iteritems'

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 "<pyshell#0>", line 1, in <module>
    import treePlotter
ModuleNotFoundError: No module named 'treePlotter'
>>> 
=============== RESTART: C:\Users\34856\Desktop\treePlotter.py ===============
>>> import treePlotter
>>> treePlotter.createPlot()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    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 "<pyshell#4>", line 1, in <module>
    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 <listcomp>
    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\348
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值