《机器学习实战》复现python语法不兼容遇到的问题

共性问题(经常遇到的错误)

1. TypeError: unhashable type: 'list’
解决办法参考:TypeError: unhashable type: 'list’解决办法。
2. 模块冲突
使用函数时加上模块名。
解决模块冲突的经验如下:

import os 
import numpy
os.listdir()
np.tile()

Chapter 3 决策树

  1. ’dict_keys’ object is not subscriptable
     - firstStr = myTree.keys()[0] # 根结点 
     + firstStr = list(myTree.keys())[0] # 根结点  
    
  2. UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x80 in position 0: invalid start byte
    《机器学习实战》P50
     # 决策树的存储
    def storeTree(inputTree,filename):
    	import pickle
        fw = open(filename,'w')
        pickle.dump(inputTree,fw)
       	fw.close()
    def grabTree(filename):
         import pickle
      	fr = open(filename)
      	return pickle.load(fr)
    
    改为:
     # 决策树的存储
    def storeTree(inputTree,filename):
    	import pickle
        fw = open(filename,'wb')
        pickle.dump(inputTree,fw)
       	fw.close()
    def grabTree(filename):
        import pickle
      	fr = open(filename,'rb')
      	return pickle.load(fr)
    

Chapter 9 树回归

  1. 9.2中二元划分数据集的函数binSplitDataSet() 出现小错误,返回的只有第一个数据,而不是划分后的全部数据
    def binSplitDataSet(dataSet,featIndex,value):
    mat0 = dataSet[np.nonzero(dataSet[:,featIndex]>value)[0],:][0] # 返回大于value的下标
    mat1 = dataSet[np.nonzero(dataSet[:,featIndex]<=value)[0],:][0]
    return mat0,mat1;
    
    改为:
    def binSplitDataSet(dataSet,featIndex,value):
    mat0 = dataSet[np.nonzero(dataSet[:,featIndex]>value)[0],:] # 返回大于value的下标
    mat1 = dataSet[np.nonzero(dataSet[:,featIndex]<=value)[0],:]
    return mat0,mat1;
    
    2. TypeError: unsupported operand type(s) for /: ‘map’ and 'int’
    P161的loadDataSet()函数里面的map方法和int不兼容,请对应做修改。
    def loadDataSet(fileName):
    dataMat = []
    fr = open(fileName)
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        floatLine = []
        for num in curLine:
            floatLine.append(float(num))  
        #floatLine = map(float,curLine) 
        # map() 会根据提供的函数对指定序列做映射。
        dataMat.append(floatLine)
    return np.mat(dataMat)
    
    3.TypeError: unhashable type: 'matrix’解决方法
    P164的回归树切分函数里面有一行代码for splitVal in set(dataSet[:,featIndex]): 会报unhashable type错误。
    修改成:
    for splitVal in set(dataSet[:,featIndex].T.A.tolist()[0]):
    解释: dataSet[:,featIndex]是一个列向量,将其转置变为行向量,然后将其变为数组,尽管此时变为数组了,这时候还会报错TypeError: unhashable type: 'numpy.ndarray'的错误,然后使用tolist()方法,将其变为列表,然后还需要加个下标[0]也还是本身。
发布了79 篇原创文章 · 获赞 37 · 访问量 8882

猜你喜欢

转载自blog.csdn.net/SinclairWang/article/details/104139559