梯度下降法Python代碼

前一篇已經總結了梯度下降法,今天嘗試將代碼用Python實現,之所以選擇Python是因爲用python寫的代碼可以短一些=。=

如果哪裏不對了,希望可以幫我糾正~~

首先是批量隨機梯度法,適用於訓練樣本數目不是特別多的情況,而且可以用於樣本特徵數目n更多的情況:

# -*- coding: cp936 -*-
#Training data set
#-----by plz
import numpy as np

def linear_regression(data_x,data_y,m,n,alpha):
    ones=np.ones(m)
    data_x=np.column_stack((data_x,ones))

    theta_guess=np.ones(n+1)   #有n個特徵,theta就是n+1維
    x_trans=data_x.transpose()   #data_x的轉置矩陣
    theta_last=np.zeros(n+1)

    theta_loss=theta_guess-theta_last
    theta_losssq=np.sum(theta_loss**2)
    while(np.sum((theta_guess-theta_last)**2)>0.0000000001):
        print "np.sum((theta_guess-theta_last)**2) is:" , np.sum((theta_guess-theta_last)**2)
                                                          
        theta_last=theta_guess
        hypothesis=np.dot(data_x,theta_guess)
        loss=hypothesis-data_y       
        cost = np.sum(loss ** 2)
        print "cost is" , cost
        gradient=np.dot(x_trans,loss)
        theta_guess=theta_guess-alpha*gradient       
    return theta_guess

data_x=np.array([(0,1),(1,2),(2,3)])
data_y=np.array([4.9,7.8,11.3])
m=data_x.shape[0]   #訓練樣本數
n=data_x.shape[1]   #樣本特徵數
alpha=0.001

theta=linear_regression(data_x,data_y,m,n,alpha)
print theta

其次是隨機梯度下降法,適用於樣本數目成千上萬的情況,相比批量梯度下降法,隨機梯度下降法只能在最優值附近徘徊,並不能收斂到最優,不過由於它的速度比較快,效果也還不錯~

#------by plz
import numpy as np

def linear_regression(data_x,data_y,m,n,alpha):
    theta=np.ones(n+1)
    for j in range(1,m):
        hypothesis=np.dot(data_x[j],theta)
        loss=hypothesis-data_y[j]
        gradient=np.dot(loss,data_x[j])
        theta=theta-alpha*gradient
    return theta
#這裏只是用11個樣本的數據集舉個例子,實際上需要大量樣本
data_x=np.array([0,1,2,3,4,5,6,7,8,9,10])
data_y=np.array([95.3,97.2,60.1,49.34,37.4,51.05,25.5,5.25,0.63,-9.4,-4.38])

m=11
n=1

theta=linear_regression(data_x,data_y,m,n,0.001)
print theta

哪裏不對了一定要提出來~一起進步~



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