您的位置:首页 > 编程语言 > Python开发

机器学习实战第五章Logistic回归的运行的小错误

2017-11-15 16:39 316 查看
本人使用的编译环境是pycharm+anneconda(python 3.6),在机器学习实战第五章,在运行梯度上升算法程序清单5-1和随机梯度上升程序清单5-3出现系统报错。现在贴出来问题并予以改正。如果是本人问题,还希望大家指正。

程序清单5-1

F:\conda\python.exe G:/statistik/Logistic/main.py
Traceback (most recent call last):
File "G:/statistik/Logistic/main.py", line 10, in <module>
main()
File "G:/statistik/Logistic/main.py", line 7, in main
weightArr=gradAscent(dataArr,labelArr)
File "G:\statistik\Logistic\Tool\Gradient.py", line 33, in gradAscent
h=sigmoid(dataMatrix*weights)
File "G:\statistik\Logistic\Tool\Gradient.py", line 18, in sigmoid
return 1.0/(1+math.exp(-inX))
TypeError: only length-1 arrays can be converted to Python scalars


google翻译:TypeError:只转度为1的数组可以转换为Python标量

源代码

h=sigmoid(dataMatrix*weights)
error=labelMat-h
weights=weights+alpha*dataMatrix.transpose()*error


sigmoid()函数只可以传入一个数,这里传入一个矩阵(n*1)系统报错.解决方案:依次把每一个样本输入算出sigmoid值,然后构成一个矩阵

for k in range(maxCycle):
wTx=dataMatrix * weights#计算出每个样本的wTx矩阵
wTarr=np.array(wTx)#转换为list  方便以下遍历
actArr=[sigmoid(warr) for warr in wTarr] #迭代遍历然后存入列表
actMat=np.mat([actArr]).reshape(m,1)#把列表转化为矩阵后,然后把矩阵变为()
error=(labelMat-actMat)
weights=weights+alpha*dataMatrix.transpose()*error
return weights


程序清单5-3(随机梯度上升)

运行报错

F:\conda\python.exe G:/statistik/Logistic/main.py
Traceback (most recent call last):
File "G:/statistik/Logistic/main.py", line 10, in <module>
main()
File "G:/statistik/Logistic/main.py", line 7, in main
weightArr=stocGradAscent0(dataArr,labelArr)
File "G:\statistik\Logistic\Tool\Gradient.py", line 45, in stocGradAscent0
weights=weights+alpha * error * dataMatrix[i]
TypeError: can't multiply sequence by non-int of type 'float'


google翻译:TypeError:无法乘以“float”类型的非int值

什么意思呢?就是列表不能乘以一个浮点数,列表只能与整数相乘。但是此处(个人感觉)程序的意思是让步长以及错误(都是一个数)与矩阵相乘,而不是改变矩阵的长度。

import numpy as np
a=[[1,2,3],
[2,5,8]]
c=a[1]*2
print(c)
控制台输出
[2, 5, 8, 2, 5, 8]


解决方案,把这个列表转化为数组或矩阵

def stocGradAscent0(dataMatrix,classLabels):#随机梯度上升算法
dataMatrix=np.array(dataMatrix)#把处理后的列表转化为数组
m,n=np.shape(dataMatrix)
alpha=0.01
weights=np.ones(n)
for i in range(m):
h=sigmoid(sum(dataMatrix[i]*weights))
error=classLabels[i]-h
weights=weights+alpha * error * dataMatrix[i]
weights=np.mat(weights).reshape((3,1))#这个目的是方便下面plotBestFit()函数中,wei.getA()的操作,其中的wei只能为矩阵
return weights


改进的随机梯度上升法

程序清单5-4,除了要把接受的列表修改为数组和weights转化为矩阵外。运行程序会出现以下错误:

F:\conda\python.exe G:/statistik/Logistic/main.py
Traceback (most recent call last):
File "G:/statistik/Logistic/main.py", line 10, in <module>
main()
File "G:/statistik/Logistic/main.py", line 7, in main
weightArr=stocGradAscent1(dataArr,labelArr)
File "G:\statistik\Logistic\Tool\Gradient.py", line 60, in
4000
stocGradAscent1
del(dataIndex[randIndex])#从列表中删除随机值
TypeError: 'range' object doesn't support item deletion

Process finished with exit code 1


google翻译:range对象不支持item删除.

原因是python3.x range返回的是range对象,不返回list对象.解决方案是把range对象转化为List对象即可

dataIndex=list(range(m))#产生一个长度为m的列表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 机器学习