手写梯度下降法实现逻辑回归

导包

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

数据

data, target = make_blobs(centers=2)
plt.scatter(data[:,0], data[:,1], c=target)

逻辑斯蒂函数

# 目标函数
def aim(X,theta):
    return np.dot(X,theta.T)


# 逻辑斯蒂函数
def logistic(X,theta):
    return 1/(1+np.exp(-aim(X,theta)))

损失函数

# 损失函数
def loss(y,X,theta):
    p = logistic(X,theta)
    return np.sum(np.multiply(y,np.log(p)) + np.multiply(1-y,np.log(1-p))) / -len(X)

损失导函数

# 损失导函数
def dloss(y,X,theta):
    p = logistic(X,theta)
    derivative = np.zeros(theta.shape)
    for i in range(len(theta.ravel())):
        temp = np.multiply((p-y).ravel(),X[:,i])
        derivative[0,i] = np.sum(temp) / len(X)
    return derivative

梯度下降法

# 梯度下降法
def gradient_descent(X,y,theta,learning_rate=0.1,gradient=1e-4,mmax=100000):
    """
    X_C:自变量, y:函数, theta:b_w的初始值, learning_rate:学习力,gradient:梯度值,mmax:最大运行次数,防止死循环
    """
    while mmax:
        theta = theta - learning_rate * (dloss(y,X,theta))
        mmax -= 1
        if abs(loss(y,X,theta)) < gradient:
            return theta
    return theta

测试数据

X = np.hstack((np.ones(shape=(len(data),1)),data))
y = target.reshape(-1,1)
theta = np.array([0,np.random.randn()*0.01,np.random.randn()*0.01]).reshape(1,3)
X.shape,y.shape,theta.shape

求出theta(θ)最小值

θ = gradient_descent(X,y,theta)
b,w1,w2= θ[0]

画图

plt.scatter(data[:,0], data[:,1], c=target)
x_test = np.linspace(4, 12, 100)
y = -w1/w2 * x_test - b/w2
plt.plot(x_test, y, c='r')

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值