博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手写LogisticRegression
阅读量:2160 次
发布时间:2019-05-01

本文共 2115 字,大约阅读时间需要 7 分钟。

import numpy as npfrom sklearn import datasetsclass 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)

转载地址:http://owwzb.baihongyu.com/

你可能感兴趣的文章
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>