Python编程实现线性回归和批量梯度下降法代码实例
作者:Key_Ky 时间:2021-10-13 07:33:27
通过学习斯坦福公开课的线性规划和梯度下降,参考他人代码自己做了测试,写了个类以后有时间再去扩展,代码注释以后再加,作业好多:
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算法输出1-9数组形成的结果为100的所有运算式
python中实现k-means聚类算法详解
Python编程实现粒子群算法(PSO)详解
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
来源:https://www.cnblogs.com/Key-Ky/archive/2013/12/10/3468290.html
标签:线性回归,梯度下降,python
0
投稿
猜你喜欢
用python生成与调用cntk模型代码演示方法
2023-04-27 10:55:39
python处理大日志文件
2021-11-09 22:21:14
Django开发RESTful API实现增删改查(入门级)
2022-05-17 02:36:53
JS实现课程表小程序(仿超级课程表)加入自定义背景功能
2024-04-16 09:35:09
PHP基于yii框架实现生成ICO图标
2024-06-05 09:44:14
python检测远程服务器tcp端口的方法
2021-02-18 22:49:44
五分钟学会Python 模块和包、文件
2023-06-01 20:05:38
不用加载Include文件也能生成选择列表吗?
2009-10-29 12:30:00
Python替换月份为英文缩写的实现方法
2023-07-19 10:20:45
pytorch中的embedding词向量的使用方法
2022-03-25 09:05:27
总结分析Python的5个硬核函数
2022-04-12 11:05:42
Go语言调用Shell与可执行文件的实现
2024-02-06 17:03:44
js canvas实现随机粒子特效
2024-06-05 09:10:50
总结归纳python os库常用方法
2023-05-23 19:34:05
MySQL命令行导出与导入数据库
2024-01-14 03:30:14
Go语言实现彩色输出示例详解
2023-09-14 01:19:45
浅谈对pytroch中torch.autograd.backward的思考
2023-10-28 13:21:44
vue.js使用v-model实现表单元素(input) 双向数据绑定功能示例
2023-07-02 16:28:28
MySQL函数一览_MySQL函数全部汇总
2024-01-21 17:43:11
浅谈Python中的全局锁(GIL)问题
2021-10-11 04:16:15