绿色软件联盟:绿色软件下载
Hi,请  登录  或  注册

纯python3.5代码实现逻辑回归的二分类

纯python代码实现逻辑回归,不调机器学习第三方库,仅调用numpy实现矩阵向量计算和matplotlib实现画图

import numpy as np

import matplotlib.pyplot as plt

# sigmod函数,即分类的函数

def sigmod(x):

    return 1/(1+np.exp(-x))

# 代价函数

def cost(hx, y):

    return -y * np.log(hx) - (1-y) * np.log(1-hx)

# 梯度下降函数

def gradient(current_para, x, y, learning_rate):

    m = len(y)

    matrix_gradient = np.zeros(len(x[0]))

    for i in range(m):

        current_x = x[i]

        current_y = y[i]

        current_x = np.asarray(current_x)

        matrix_gradient += (sigmod(np.dot(current_para, current_x)) - current_y) * current_x

    new_para = current_para - learning_rate * matrix_gradient

    return new_para

# 误差计算函数

def error(para, x, y):

    total = len(y)

    error_num = 0

    for i in range(total):

        current_x = x[i]

        current_y = y[i]

        hx = sigmod(np.dot(para, current_x))

        if cost(hx, current_y) > 0.5:

            error_num += 1

    return error_num/total

赞(0)
标题:《纯python3.5代码实现逻辑回归的二分类》
链接:https://www.lvruan.com/app/555554
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

相关推荐

  • 暂无文章

LvRuan.com=绿软=绿盟=绿色软件联盟
情怀第一 18年 老牌 下载站 绿色 安全 无广告 无捆绑

评论 抢沙发

登录

找回密码

注册