栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

第十七章 逻辑回归(Logistic Regression)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第十七章 逻辑回归(Logistic Regression)

17.1 相关概念

        逻辑回归更多的是从概率的层面进行建模,但是我们在实际应用中,更多的是用在二元分类型的问题。

        思考:如何算“更好的拟合”,类比一元线性回归的最小二乘法。

        sigmoid函数:

 

        逻辑回归公式:

  

from math import exp


def prediction(row, coefficients):
    y_hat = coefficients[0]
    for i in range(len(row) - 1):
        y_hat += coefficients[i + 1] * row[i]
    return 1 / (1 + exp(-y_hat))


if __name__ == '__main__':
    dataset = [[2, 2, 0],
               [2, 4, 0],
               [3, 3, 0],
               [4, 5, 0],
               [8, 1, 1],
               [8.5, 3.5, 1],
               [9, 1, 1],
               [10, 4, 1]]

    coef = [-0.5, 0.9, -1.5]
    for row in dataset:
        y_hat = prediction(row, coef)
        print("our Expected Value is %.3f,The Predicted Value is %.3f [%d]" % (row[-1], y_hat, round(y_hat)))

17.2 梯度下降法求解逻辑回归系数

from math import exp


def prediction(row, coefficients):
    y_hat = coefficients[0]
    for i in range(len(row) - 1):
        y_hat += coefficients[i + 1] * row[i]
    return 1 / (1 + exp(-y_hat))


# using stochastic gradient descent method to calculate logistic regression coefficients
# 用随机梯度下降算法求解逻辑回归系数
def using_sgd_method_to_calculate_coefficients(training_dataset, learning_rate, n_times_epoch):
    coefficients = [0.0 for i in range(len(training_dataset[0]))]
    for epoch in range(n_times_epoch):
        the_sum_of_error = 0
        for row in training_dataset:
            y_hat = prediction(row, coefficients)
            error = row[-1] - y_hat  # 真实值-预测值
            the_sum_of_error += error ** 2
            coefficients[0] = coefficients[0] + learning_rate * error * y_hat * (1.0 - y_hat)
            for i in range(len(row) - 1):
                coefficients[i + 1] = coefficients[i + 1] + learning_rate * error * y_hat * (1.0 - y_hat) * row[i]
        print("This is epoch [%d],the learning rate we are using is [%.3f],the error is [%.3f]" % (
            epoch, learning_rate, the_sum_of_error))
    return coefficients


if __name__ == '__main__':
    learning_rate = 0.1
    n_times_epoch = 1000

    dataset = [[2, 2, 0],
               [2, 4, 0],
               [3, 3, 0],
               [4, 5, 0],
               [8, 1, 1],
               [8.5, 3.5, 1],
               [9, 1, 1],
               [10, 4, 1]]
    coef = using_sgd_method_to_calculate_coefficients(dataset, learning_rate, n_times_epoch)
    print(coef)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号