# -*- coding:UTF-8 -*-
import numpy
def loadDataSet():
return dataMat,labelMat
def sigmoid(inX):
return 1.0/(1+numpy.exp(-inX))
def gradAscent(dataMatIn,classLabels):
dataMatrix=numpy.mat(damaMatIn)
labelMat=numpy.mat(classLabels).transpose()
#上升梯度
alpha=0.01
#迭代次数
maxCycles=500
#初始回归向量
m,n=numpy.shape(dataMatrix)
weights=numpy.ones((n,1))
for k in range(maxCycles):
h=sigmoid(dataMatrix*weights)
error=(labelMat-h)
weights=weights+alpha*dataMatrix.transpose()*error
pass
return weights
def test():
dataArr,labelMat=loadDataSet()
print gradAscent(dataArr,labelMat)
if __name__ == ‘__main__‘:
test()
时间: 2024-10-05 06:12:27