手写LogisticRegression

import numpy as np
from sklearn import datasets

class LogisticRegression:
    def __init__(self):
        self._theta = None

    def sigmoid(self, t):
        return 1 / (1 + np.exp(-t))

    def fit(self, X_train, y_train, eta=0.01, n_iters=100000):
        """
        训练函数  设置学习率和最大迭代次数
        :param X_train: 训练集
        :param y_train:
        :param eta: 学习率
        :param n_iters: 最大迭代次数
        :return:
        """

        def loss(theta, X_b, y):
            """
            loss function
            交叉熵
            """
            y_hat = self.sigmoid(X_b.dot(theta))
            return np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))

        def dloss(theta, X_b, y):
            
            return X_b.T.dot(self.sigmoid(X_b.dot(theta) - y)) / len(y)

        def grandient_descent(X_b, y, init_theta, eta, n_iters, threshold=0.000001):
            theta = init_theta
            now_iter = 0
            while now_iter < n_iters:
                gradient = dloss(theta, X_b, y)
                last_theta = theta
                theta = theta - eta * gradient
                if (abs(loss(theta, X_b, y) - loss(last_theta, X_b, y)) < threshold):
                    break
                now_iter += 1
            return theta

        X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
        # X_b = X_train
        init_theta = np.zeros(X_b.shape[1])
        self._theta = grandient_descent(X_b, y_train, init_theta, eta, n_iters)
        return self

    def predict_prob(self, X_pre):
        X_b = np.hstack([np.ones((len(X_pre), 1)), X_pre])
        return self.sigmoid(X_b.dot(self._theta))

    def predict(self, X_test):
        prob = self.predict_prob(X_test)
        return np.array(prob >= 0.5, dtype='int')

    def score(self, X_test, y_test):
        y_pre = self.predict(X_test)

        correct = [1 if (a == b) else 0 for (a, b) in zip(y_pre, y_test)]
        rate = (sum(correct) / len(correct))
        return int(rate * 100)


if __name__ == '__main__':
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target
    X = X[y < 2, :2]
    y = y[y < 2]
    lr = LogisticRegression()
    lr.fit(X, y)
    
    print(X.shape)
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值