通过学习斯坦福公开课的线性规划和梯度下降,参考他人代码自己做了测试,写了个类以后有时间再去扩展,代码注释以后再加,作业好多:
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. 迭代过程中的误差cost
图2. 数据散点图和解直线
总结
以上就是本文关于Python编程实现线性回归和批量梯度下降法代码实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
Python算法输出1-9数组形成的结果为100的所有运算式
python中实现k-means聚类算法详解
Python编程实现粒子群算法(PSO)详解
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“Python编程实现线性回归和批量梯度下降法代码实例”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月12日
2024年10月12日
- 群星《说唱梦工厂 第1期》[FLAC/分轨][274.94MB]
- 群星.2024-舍不得星星影视原声带【TME】【FLAC分轨】
- 许志豪.2024-袂记的青春【发现梦想】【FLAC分轨】
- 黄义达.2011-微光(EP)【橙天娱乐】【WAV+CUE】
- 小虎队.1989-逍遥游【飞碟】【WAV+CUE】
- 范丽丝.1977-爱慕(2016复刻版)【丰荣】【WAV+CUE】
- 群星.1990-华纳夏日情【华纳】【WAV+CUE】
- 《黄土地民歌精选辑 陕北民歌》[WAV/分轨][480MB]
- 群星《金吾卫之风起金陵 动漫原声带》[320K/MP3][36.65MB]
- 群星《金吾卫之风起金陵 动漫原声带》[FLAC/分轨][98.08MB]
- JessicaMolaskey-SittinginLimbo--2024[wav]
- 中国音乐地图之听见江苏扬州小调扬剧经典唱段2020[WAV分轨]
- 中国音乐地图之听见国乐阮柳琴音乐集2021[WAV+分轨]
- 常石磊.2010-自己【风向乐动】【WAV+CUE】
- TWINS.2008-桐话妍语(引进版)【英皇娱乐】【WAV+CUE】