Python:逻辑回归 实现

  • 批量梯度下降法

使用矩阵的形式实现的批量梯度下降法 

"""
Logistic Regression
by batch gradient descent
auther:Daniel He
date:2021-06-26
"""
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt

class LogisticRegression(object):
    def __init__(self, learning_rate=0.01, max_iter=100):
        self.learning_rate = learning_rate
        self.max_iter = max_iter

    def sigmoid(self,z):
        return 1.0 / (1.0 + np.exp(-z))

    def fit(self,X,y):
        self.X = X
        self.y = y
        self.row, self.col = self.X.shape
        self.theta = np.ones(self.col)
        self.lr = np.ones(self.col) * self.learning_rate
        """参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
        for i in range(self.max_iter):
            gradient = (self.sigmoid(self.X @ self.theta) - self.y) @ self.X
            self.theta -= self.learning_rate * gradient


if __name__ == '__main__':
    X,y = datasets.make_blobs(n_features=2,n_samples=500,centers=2,cluster_std=[3,3],random_state=1)
    X = np.hstack((np.ones(X.shape[0]).reshape((-1,1)),X))
    print(X)

    lr = LogisticRegression()
    lr.fit(X=X,y=y)
    theta = lr.theta
    print(theta)
    plt.scatter(X[:,1],X[:,2],c=y)
    a = np.arange(-15,4,0.2)
    b = (-theta[0]-theta[1]*a)/theta[2]
    plt.plot(a,b)
    plt.show()

 

  • miniBatch Gradient Descent
"""
Logistic Regression
by nimibatch gradient descent
auther:Daniel He
date:2021-06-26
"""
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
import time

class LogisticRegression(object):
    def __init__(self, learning_rate=0.01, max_iter=100):
        self.learning_rate = learning_rate
        self.max_iter = max_iter

    def sigmoid(self,z):
        return 1.0 / (1.0 + np.exp(-z))

    def fit(self,X,y):
        self.X = X
        self.y = y
        self.row, self.col = self.X.shape
        self.theta = np.ones(self.col)
        # self.lr = np.ones(self.col) * self.learning_rate
        """参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
        index = [_ for _ in range(self.row)]
        batch_size = int(self.row / 10)
        for i in range(self.max_iter):
            """这里的梯度没有做平均处理(标准情况是要做平均处理的)"""
            # ids_batch = np.random.choice(index,size=batch_size,replace=False)
            gradient = (self.sigmoid(self.X @ self.theta) - self.y) @ self.X
            self.theta -= self.learning_rate * gradient


class LogisticRegression_minibatch(object):
    def __init__(self, learning_rate=0.3, max_iter=200):
        self.learning_rate = learning_rate
        self.max_iter = max_iter

    def sigmoid(self,z):
        return 1.0 / (1.0 + np.exp(-z))

    def fit(self,X,y):
        self.X = X
        self.y = y
        self.row, self.col = self.X.shape
        self.theta = np.ones(self.col)
        # self.lr = np.ones(self.col) * self.learning_rate
        """参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
        index = [_ for _ in range(self.row)]
        batch_size = int(self.row / 20)
        for i in range(self.max_iter):
            """这里的梯度没有做平均处理(标准情况是要做平均处理的)"""
            ids_batch = np.random.choice(index,size=batch_size,replace=False)
            gradient = (self.sigmoid(self.X[ids_batch] @ self.theta) - self.y[ids_batch]) @ self.X[ids_batch]
            self.theta -= self.learning_rate * gradient


if __name__ == '__main__':
    X,y = datasets.make_blobs(n_features=2,n_samples=5000,centers=2,cluster_std=[3,3],random_state=1)
    X = np.hstack((np.ones(X.shape[0]).reshape((-1,1)),X))
    # print(X)
    #########################
    Start_time = time.time()
    # lr = LogisticRegression()
    lr = LogisticRegression_minibatch()
    lr.fit(X=X,y=y)
    theta = lr.theta
    End_time = time.time()
    print("消耗时间==",End_time-Start_time)
    ##########################


    plt.scatter(X[:,1],X[:,2],c=y)
    a = np.arange(-15,4,0.2)
    b = (-theta[0]-theta[1]*a)/theta[2]
    plt.plot(a,b)
    plt.show()

 

minibatch 模式并没有比批量形式快!而且效果也下降了。

参考:

https://zhuanlan.zhihu.com/p/74874291 

https://www.cnblogs.com/BYRans/p/4713624.html

https://www.cnblogs.com/pinard/p/6029432.html

https://blog.csdn.net/zouxy09/article/details/20319673

https://www.cnblogs.com/luozeng/p/8605140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeniuHe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值