您的位置:首页 > 编程语言 > Python开发

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

2017-08-24 15:14 549 查看

逻辑回归数学推导

对于一些数据(离散点),我们找到一条直线拟合这些离散点,就叫回归。

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()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐