Machine Learning in Action学习笔记(1)----python2与python3的一些区别

下面是本人正在学习Python时遇到的一些错误,记录下来
<<Machine Learning in Action>>


1.TypeError: ‘dict_keys’ object does not support indexing
(错误地点:程序清单3-6)
此问题为Python版本问题
#在Python2中:
firstStr = myTree.keys()[0]
#在Python3中:
firstTem = list(myTree.keys())
firstStr = firstTem[0]


2.AttributeError: 'str' object has no attribute '_name_'
(错误地点:程序清单3-6)
源代码使用Python2编写,自己使用编译器为Python3
错误语句:if type(secondDict[key]._name_=="dict"):
#test to see if the nodes are dictonaires, if not they are leaf nodes
修改后:
if type(secondDict[key]) == dict:


3.TypeError: 'builtin_function_or_method' object is not subscriptable
(错误地点:程序清单4-1)
错误语句:
returnVec[vocabList.index[word]] = 1
内建函数或者方法对象不能subscriptable
错误在index[key],原因是:index后应当是()
改正:
returnVec[vocabList.index(word)] = 1


4.TypeError: 'range' object doesn't support item deletion
(错误地点:程序清单4-5)
错误语句:
for i in range(10):
   randIndex = int(random.uniform(0,len(trainingSet)))
   testSet.append(trainingSet[randIndex])
   del(trainingSet[randIndex])  
修改后:
for i in range(10):
   randIndex = int(random.uniform(0,len(trainingSet)))
   testSet.append(trainingSet[randIndex])
   del(list(trainingSet)[randIndex])
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值