from numpy import *
import numpy as np
# 导入数据集
def sigmoid(inX):
return 1.0 / (1 + np.exp(-inX))
def gradAscent(data, Labels,maxCycles,alpha):
m, n = shape(data)
weights = ones((n, 1)) # 权值
Labels.shape=m,1
print(weights)
for k in range(maxCycles):
h = sigmoid(np.dot(data,weights))
error = Labels - h
weights = weights + alpha * np.dot(data.transpose(),error)
print(weights)
return weights
"""example"""
data=np.random.rand(5,2)
labels=np.random.randint(2,size=5)
data=np.array(data)
labels=np.array(labels)
cycle_num=5
aplha=0.001
weights =gradAscent(data, labels,cycle_num,aplha)
#print(weights)