机器学习---Logistic回归数学推导以及python实现

逻辑回归数学推导

对于一些数据(离散点),我们找到一条直线拟合这些离散点,就叫 回归。
Logistic回归主要用于 二分类问题。

假设 X(x0,x1,x2...xn) 是我们的测试数据,θ(θ0,θ1,θ2....θn)是我们权重,由线性回归模型产生的预测值是 Z = x0*θ0+x1*θ1...+xn*θn。二分类问题输出标记非 0 即 1。我们需要把 Z 转化为 0/1 值。

我们利用 sigmoid函数可以完成上面的转化过程。

(1)

sigmoid函数图形以及转化过程,在 Z 大于0 时候,转化为 1 ,否则就转化为 0.


公式(1)可以理解为 给定输入 X ,Y = 1 的概率。 因此 hθ(x) 可以理解为 结果取 1 的概率。因此,类别 1 和类别 0 的概率是:


和线性方程类似,我们需要找到 θ,使得 Cost函数 最小。Logistic 回归的 Cost 函数:

(两个函数一致)


找到合适的 θ ,使的 J 最小。使用梯度下降法

(alpha是学习率)

通过迭代对 θ 进行更新,达到我们期望的精度或者达到了迭代次数


逻辑回归Python实现

使用 python3

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 24 09:45:47 2017

@author: yqq
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def getData():  //获得数据
    gre = pd.read_csv("C:/Users/yqq/Desktop/testSet.csv",sep=',')
    Data = gre[[0,1]]
    Label = gre[[2]]
    m,n=np.shape(Data)
    coulumn = np.ones((m,1))
    Data=np.column_stack((coulumn,Data))
    dataMat=np.mat(Data)
    LabelMat=np.mat(Label)
    return dataMat,LabelMat
def sigmoid(inx):
    return 1.0/(1+np.exp(-inx))
def gradAscent(dataIn,LabelIn,alpha,maxCycles):
    dataMat = np.mat(dataIn) 
    LabelMat=np.mat(LabelIn).transpose()
    m,n=np.shape(dataMat)
    weight=np.ones((n,1))
    #print(dataMat*weight)
    for k in range(maxCycles):
         h=sigmoid(dataMat*weight) 
         error=LabelMat-h
         weight=weight+alpha*dataMat.transpose()*error        
    return weight  
def display(dataMat,LabelMat,w,):
    xcord1=[];xcord2=[]
    ycord1=[];ycord2=[]
    m,n=np.shape(dataMat)
    w=w.getA()
    for i in range(m):
        if int(LabelMat[i])==1:
            xcord1.append(dataMat[i][1])
            ycord1.append(dataMat[i][2])
        else:
            xcord2.append(dataMat[i][1])
            ycord2.append(dataMat[i][2])
    x=np.linspace(-3.0,3.0,100)
    y=(-w[0]-w[1]*x)/w[2]
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    ax.scatter(xcord1,ycord1,s=30,c='red',marker='s')
    ax.scatter(xcord2,ycord2,s=10,c='green',marker='s')
    ax.plot(x,y)
    plt.xlabel("x");plt.ylabel("y")
    plt.show()


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值