重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了Python如何实现线性回归和批量梯度下降法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联公司成立与2013年,先为万秀等服务建站,万秀等地企业,进行企业商务咨询服务。为万秀企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。示例
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
感谢你能够认真阅读完这篇文章,希望小编分享的“Python如何实现线性回归和批量梯度下降法”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。