斯坦福机器学习by吴恩达-logistic回归 python实现

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()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值