【机器学习实战-1章】运行本书代码时出现的错误集合

Numpy数组相关错误

from numpy.ma import exp

  1. TypeError:only size-1 arrays can be converted to Python scalars

1)def sigmoid(self,inX):
return 1.0/(1+exp(-inX))
numpy数组最好使用Numpy的数学函数进行处理
def sigmoid(self,inX):
return 1.0/(1+numpy.exp(-inX))
2)D = np.mat(np.ones((m,1))/m)
D = np.multiply(D,exp(expon))
print(type(D)) #<class ‘numpy.ma.core.MaskedArray’>
D = np.mat(D/D.sum())
print(type(D)) #<class ‘numpy.matrix’>
在对numpy数组进行操作后,如果需要再次使用数组,要及时将数组的类型转换回去,否则报错

  1. TypeError:unsupported operand type(s) for *: ‘float’ and
    ‘builtin_function_or_method’

    prob = self.sigmoid(sum(inX*weights))
    weights和inX都为Numpy数组,如要相乘,weights需要经过转置
    weights.transpose()

  2. TypeError:data type not understood

self.alphas = np.mat(np.zeros(self.m,1))
错误出现在np.zeros((self.m,1)),多维数组要加上括号,有几个维度用几个括号

  1. ValueError:The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    if np.sign(predict) != np.sign(labelArr[i]):
    numPy对这个逻辑表达式判断不清醒,可用a.any() or a.all() if (np.sign(predict) -
    np.sign(labelArr[i])).any(): print(“True”)

  2. ValueError:data type must provide an itemsize

    for line in fr.readlines(): lineArr.append(float(curline[i]))
    在读取文件时,如果数据需要转换为数组,需要使用float()或int()等。因为readlines()的结果是一个个字符串

  3. TypeError:unhashable type: ‘matrix’ for splitVal in set(dataSet[:,featIndex]): set()方法需要使用可迭代对象 更改为 for splitVal in
    set(dataSet[:,featIndex].T.tolist()[0])

  4. 提示错误ValueError: matrix must be 2-dimensional 或者TypeError: list indices must be integers or slices, not tuple
    centList[bestCentToSplit] = bestNewCents[0,:]
    centList.append(bestNewCents[1,:] return mat(centList)
    》》》[matrix([[-122.54868607, 45.51882187]]),
    matrix([[-122.65589505, 45.49371211]]),
    matrix([[-122.842918, 45.646831]]),
    matrix([[-122.7680632, 45.4665528]]),
    matrix([[-122.68216889, 45.56573522]])] 通过tolist()方法将其进行转换
    centList[bestCentToSplit] = bestNewCents[0,:].tolist()[0]
    # print(“Numpy.matrix>>>list====”)
    # print(bestNewCents[0,:])
    # print(bestNewCents[0,:].tolist())
    # print(bestNewCents[0,:].tolist()[0])
    # print(“Numpy.matrix>>>list====”)
    centList.append(bestNewCents[1,:].tolist()[0])

读取文件时发生错误

1.write() argument must be str, not bytes
写入文件时发生错误,write() argument must be str, not bytes
with open(filename,‘w’) as fw:
fw.write(content)
在写入二进制文件时,python2支持上述表达式;使用python3运行上述表达式时则会报错
Python3给open函数添加了名为encoding的新参数,而这个新参数的默认值却是‘utf-8’。
这样在文件句柄上进行read和write操作时,系统就要求开发者必须传入包含Unicode字符的实例,而不接受包含二进制数据的bytes实例

解决方法:
使用二进制写入模式(‘wb’)来开启待操作文件,而不能像原来那样,采用字符写入模式(‘w’)。
文件读取数据的时候也有类似的问题。用’rb’模式(二进制模式)打开文件,而不要使用’r’模式。

其他

trainingSet =range(50)
traindel(trainingSet[randIndex])

“”"python3.x , 出现错误 ‘range’ object doesn’t support item deletion

原因:python3.x range返回的是range对象,不返回数组对象

解决方法:

把 trainingSet = range(50) 改为 trainingSet = list(range(50))"""

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值