詳細解釋《機器學習實戰》第5章logistic迴歸 第5-1程序(梯度下降法推導)和梯度下降法實現對率迴歸

從別人的參考過來的推導過程

這裏寫圖片描述

這裏寫圖片描述

梯度下降法實現對率迴歸

數據集

這裏寫圖片描述

代碼

# -*- coding: utf-8 -*-
from log import *
import pandas as pd
from pandas import *
import matplotlib.pyplot as plt
import numpy as np
from numpy import *
#讀取數據到X,y
dataset = np.loadtxt(r'C:\Users\zmy\Desktop\titanic\watermelon.csv',delimiter=",")
X = dataset[0:2, :]
y = dataset[3,:]
X = X.transpose()
y = y.transpose()
df = pd.DataFrame(X, columns = ['density', 'ratio_sugar'])
m,n = shape(df.values)
df['norm'] = ones((m,1))
dataMat = array(df[['norm', 'density', 'ratio_sugar']].values[:,:])
labelMat = array(y).transpose()

def sigmoid(inX):
    return 1.0 / (1+exp(-inX))
#梯度下降法
def gradAscend(dataMatIn, classLabels):
    dataMatrix = dataMatIn
    # labelMat = mat(classLabels).transpose()
    m,n = shape(dataMatrix)
    alpha = 0.1
    maxCycle = 500
    weights = ones((n,1))
    for i in range(maxCycle):
        a = dot(dataMatrix, weights)
        # print a
        h = sigmoid(a)
        error = (labelMat - h)
        weights = weights + alpha * dataMatrix.transpose()*error
    return weights
# 隨機梯度下降法
def stocGradAscend1(dataMat, labelMat, numIter =50):
    print dataMat
    print labelMat
    m, n = shape(dataMat)
    weights = ones(n)
    weights = array(weights)
    for j in range(numIter):
        dataIndex = range(m)
        for i in range(m):
            # dataIndex = range(m)
            alpha = 40 / (1.0+j+i) + 0.2
            randIndex_temp = int(random.uniform(0, len(dataIndex)))
            randIndex = dataIndex[randIndex_temp]
            h = sigmoid(sum(dataMat[randIndex]*weights))
            error = labelMat[randIndex] - h
            weights = weights + alpha * error * dataMat[randIndex]
            del(dataIndex[randIndex_temp])
    return weights

def plotBestFit(weights):
    import matplotlib.pyplot as plt
    # dataMat, labelMat = loadDataSet()
    dataArr = array(dataMat)
    n = shape(dataArr)[0]
    xcord1 = []
    ycord1 = []
    xcord2 = []
    ycord2 = []
    for i in range(n):
        if int(labelMat[i]) == 0:
            xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i, 2])
        else:
            xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i, 2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    #畫出散點圖
    ax.scatter(xcord1, ycord1, s = 30, c = 'red', marker='s',label = '1')
    ax.scatter(xcord2, ycord2, s=30, c = 'g', label = '0')
    # 設置直線的x,y上的點
    x = arange(0.2, 0.8, 0.1)
    y = array((-weights[0] - weights[1]*x)/weights[2])
    y=y.transpose()
    ax.plot(x, y)
    plt.xlabel('density')
    plt.ylabel('ratio_sugar')
    plt.legend(loc = 'upper right')
    plt.title("random gradAscent logistic regression")
    plt.show()
# 函數調用
weights = stocGradAscend1(dataMat, labelMat)
plotBestFit(weights)

梯度下降法的結果:

這裏寫圖片描述

隨機梯度下降法的結果:

這裏寫圖片描述

發佈了36 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章