机器学习-逻辑回归

科技资讯 投稿 6400 0 评论

机器学习-逻辑回归

前言

逻辑回归 = 线性回归 + sigmoid 函数

回顾线性回归
    表达式:\(y = wx + b\
sigmoid 函数

    以0.5为分界线的激活函数,主要用于将结果输入sigmoid 函数中sigmoid函数会输出一个[0,1] 区间的概率值0.5以上为一类0.5以下为一类,这样完成二分类任务

逻辑回归的公式

    \(z = wx+b\

  • 所以可以写成 \(y =\frac{1}{1+e^{-wx+b}}\

逻辑回归的损失
    \(J = -[ylna+(1-yln(1-a]\
  • 逻辑回归损失函数体现在“预测值” 与 “实际值” 相似程度上
  • 损失值越小,模型会越好,但是过于小也要考虑过拟合的原因
梯度下降与参数更新
deltatheta = (1.0 / m * X.T.dot(h - y

更新参数:\(\theta_j = \theta_j - \alpha\Delta\theta_j\

theta = theta - alpha * deltatheta
代码
import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('ex2data1.txt',delimiter=','

x =  data[:,:-1]
y = data[:,-1]

x -= np.mean(x,axis=0
x /= np.std(x,axis=0

X =  np.c_[np.ones(len(x,x]

def mov(theta:
    z =  np.dot(X,theta
    h = 1/(1+np.exp(-z
    return h

def cos(h:
    j = -np.mean(y*np.log(h+(1-y*np.log(1-h
    return j

def tidu(sus=10000,aphe=0.1:
    m,n = X.shape
    theta = np.zeros(n
    j = np.zeros(sus
    for i in range(sus:
        h = mov(theta
        j[i] = cos(h
        te = (1/m*X.T.dot(h-y
        theta -= te * aphe
    return h,j,theta

if __name__ == '__main__':
    h,j,theta = tidu(
    print(j
    plt.plot(j
    plt.show(

编程笔记 » 机器学习-逻辑回归

赞同 (28) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽