import numpy as np
import matplotlib.pyplot as plt
def load_data(filename):
X=[]
y=[]
file=open(filename)
for line in file.readlines():
lineArr=line.strip().split('\t')
X.append(lineArr[0:2])
y.append(lineArr[-1])
X = np.array(X, dtype=np.float64)
y = np.array(y, dtype=np.int).reshape(-1, 1)
return X,y
def sigmoid(x):
return 1.0/(1+np.exp(-x))
def batch_gradient_descent(X,y,learning_rate,iters):
weights=np.zeros((X.shape[1],1))
weights_update=np.zeros((iters,X.shape[1]))
for i in range(iters):
weights-=learning_rate*(sigmoid(X.dot(weights))-y).T.dot(X).T
weights_update[i,:]=weights.ravel().T
return weights, weights_update
[X,y]=load_data('/Users/yuewang/Desktop/研究生学习/机器学习/吴恩达斯坦福机器学习课程/Machine-learning-in-action-master/logistic/testSet.txt')
temp=np.ones((X.shape[0],1))
X=np.hstack((temp,X))
weights, weights_update=batch_gradient_descent(X,y,0.001,1000)
label0=np.where(y.ravel()==0)
label1=np.where(y.ravel()==1)
plt.scatter(X[label0,1],X[label0,2],marker='x',color='r',label='0')
plt.scatter(X[label1,1],X[label1,2],marker='o',color='b',label='1')
plt.xlabel("x1")
plt.ylabel("x2")
plt.legend(loc='upper left')
x_plot=np.arange(-4,3,0.1)
y_plot=(-weights[0]-weights[1]*x_plot)/weights[2]
plt.plot(x_plot,y_plot)
plt.show()
plt.plot(weights_update)
plt.xlabel("iteration")
plt.ylabel("weight")
plt.show()
def stochastic_gradient_descent(X,y,learning_rate,iters):
weights=np.zeros((X.shape[1],1))
weights_update=np.zeros((iters,X.shape[1]))
for i in range(iters):
for j in range(X.shape[0]):
weights-=learning_rate*(sigmoid(X[j,:].dot(weights))-y[j,:]).reshape(1,-1)*(X[j,:].reshape(1,-1).T)
weights_update[i,:]=weights.ravel().T
return weights,weights_update
sgd_weights,sgd_weights_update=stochastic_gradient_descent(X,y,0.01,200)
print(sgd_weights)
plt.subplot(311)
print(sgd_weights_update[:,0])
plt.plot(sgd_weights_update[:,0],'b')
plt.ylabel("weight_0")
plt.subplot(312)
plt.plot(sgd_weights_update[:,1],'r')
plt.ylabel("weight_1")
plt.subplot(313)
plt.plot(sgd_weights_update[:,2],'g')
plt.ylabel("weight_2")
plt.xlabel("iteration")
plt.show()
斯坦福机器学习by吴恩达-logistic回归 python实现
最新推荐文章于 2024-04-20 21:16:34 发布