
import matplotlib.pyplot as plt
import numpy as np
#假设
def logistic(x):
return 1/(1+np.exp(-x))
#随机梯度下降
def SGD(X,Y,a=0.001):
theta = np.mat([0, 0], dtype='float64')
for time in range(3000):
#用m个样本分别对theta进行更新
for i in range(30):
theta += a*( ( Y[i]-logistic(theta*X[i].T) ) * X[i] )
return theta
#生成30个x
X=np.mat([ [1,x] for x in np.linspace(-5,5,30)])
Y=np.mat(np.append(np.zeros(10),np.ones(20))).T
theta=SGD(X,Y)
# 画出拟合图像
plt.scatter(np.asarray(X[:, 1]), np.asarray(Y))
plt.plot(X[:,1],logistic(theta*X.T).T)
plt.show()