Python編程實(shí)現(xiàn)線(xiàn)性回歸和批量梯度下降法代碼實(shí)例
通過(guò)學(xué)習(xí)斯坦福公開(kāi)課的線(xiàn)性規(guī)劃和梯度下降,參考他人代碼自己做了測(cè)試,寫(xiě)了個(gè)類(lèi)以后有時(shí)間再去擴(kuò)展,代碼注釋以后再加,作業(yè)好多:
import numpy as np
import matplotlib.pyplot as plt
import random
class dataMinning:
datasets = []
labelsets = []
addressD = '' #Data folder
addressL = '' #Label folder
npDatasets = np.zeros(1)
npLabelsets = np.zeros(1)
cost = []
numIterations = 0
alpha = 0
theta = np.ones(2)
#pCols = 0
#dRows = 0
def __init__(self,addressD,addressL,theta,numIterations,alpha,datasets=None):
if datasets is None:
self.datasets = []
else:
self.datasets = datasets
self.addressD = addressD
self.addressL = addressL
self.theta = theta
self.numIterations = numIterations
self.alpha = alpha
def readFrom(self):
fd = open(self.addressD,'r')
for line in fd:
tmp = line[:-1].split()
self.datasets.append([int(i) for i in tmp])
fd.close()
self.npDatasets = np.array(self.datasets)
fl = open(self.addressL,'r')
for line in fl:
tmp = line[:-1].split()
self.labelsets.append([int(i) for i in tmp])
fl.close()
tm = []
for item in self.labelsets:
tm = tm + item
self.npLabelsets = np.array(tm)
def genData(self,numPoints,bias,variance):
self.genx = np.zeros(shape = (numPoints,2))
self.geny = np.zeros(shape = numPoints)
for i in range(0,numPoints):
self.genx[i][0] = 1
self.genx[i][1] = i
self.geny[i] = (i + bias) + random.uniform(0,1) * variance
def gradientDescent(self):
xTrans = self.genx.transpose() #
i = 0
while i < self.numIterations:
hypothesis = np.dot(self.genx,self.theta)
loss = hypothesis - self.geny
#record the cost
self.cost.append(np.sum(loss ** 2))
#calculate the gradient
gradient = np.dot(xTrans,loss)
#updata, gradientDescent
self.theta = self.theta - self.alpha * gradient
i = i + 1
def show(self):
print 'yes'
if __name__ == "__main__":
c = dataMinning('c:\\city.txt','c:\\st.txt',np.ones(2),100000,0.000005)
c.genData(100,25,10)
c.gradientDescent()
cx = range(len(c.cost))
plt.figure(1)
plt.plot(cx,c.cost)
plt.ylim(0,25000)
plt.figure(2)
plt.plot(c.genx[:,1],c.geny,'b.')
x = np.arange(0,100,0.1)
y = x * c.theta[1] + c.theta[0]
plt.plot(x,y)
plt.margins(0.2)
plt.show()

圖1. 迭代過(guò)程中的誤差cost

圖2. 數(shù)據(jù)散點(diǎn)圖和解直線(xiàn)
總結(jié)
以上就是本文關(guān)于Python編程實(shí)現(xiàn)線(xiàn)性回歸和批量梯度下降法代碼實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式
python中實(shí)現(xiàn)k-means聚類(lèi)算法詳解
Python編程實(shí)現(xiàn)粒子群算法(PSO)詳解
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- python實(shí)現(xiàn)梯度下降法
- python梯度下降算法的實(shí)現(xiàn)
- python梯度下降法的簡(jiǎn)單示例
- 基于隨機(jī)梯度下降的矩陣分解推薦算法(python)
- python實(shí)現(xiàn)梯度下降算法
- python+numpy+matplotalib實(shí)現(xiàn)梯度下降法
- python實(shí)現(xiàn)隨機(jī)梯度下降法
- Python語(yǔ)言描述隨機(jī)梯度下降法
- python實(shí)現(xiàn)隨機(jī)梯度下降(SGD)
- python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線(xiàn)性回歸
相關(guān)文章
Python程序設(shè)計(jì)入門(mén)(5)類(lèi)的使用簡(jiǎn)介
這篇文章主要介紹了Python類(lèi)的使用,需要的朋友可以參考下2014-06-06
python 通過(guò)類(lèi)中一個(gè)方法獲取另一個(gè)方法變量的實(shí)例
今天小編就為大家分享一篇python 通過(guò)類(lèi)中一個(gè)方法獲取另一個(gè)方法變量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError
這篇文章主要介紹了Python使用QQ郵箱發(fā)送郵件報(bào)錯(cuò)smtplib.SMTPAuthenticationError,簡(jiǎn)單介紹了python 發(fā)送郵件的步驟,需要的朋友可以參考下2019-12-12
使用graphics.py實(shí)現(xiàn)2048小游戲
本文給大家分享的是使用Python實(shí)現(xiàn)2048小游戲的源碼,非QT實(shí)現(xiàn)的哦,推薦給大家,有需要的小伙伴參考下吧。2015-03-03
Python實(shí)現(xiàn)byte轉(zhuǎn)integer
這篇文章主要介紹了Python實(shí)現(xiàn)byte轉(zhuǎn)integer操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
對(duì)Python中TKinter模塊中的Label組件實(shí)例詳解
今天小編就為大家分享一篇對(duì)Python中TKinter模塊中的Label組件實(shí)例詳解,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Pandas中KeyError: 'Column_Name' not
在使用Pandas進(jìn)行數(shù)據(jù)處理時(shí),KeyError: 'Column_Name' not in index是一種常見(jiàn)的錯(cuò)誤,它通常發(fā)生在嘗試訪(fǎng)問(wèn)DataFrame中不存在的列名時(shí),本文將深入分析這一錯(cuò)誤的原因、提供解決辦法,需要的朋友可以參考下2024-07-07
Python移動(dòng)測(cè)試開(kāi)發(fā)subprocess模塊項(xiàng)目實(shí)戰(zhàn)
這篇文章主要為大家介紹了Python移動(dòng)測(cè)試開(kāi)發(fā)subprocess模塊項(xiàng)目實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

