通过学习斯坦福公开课的线性规划和梯度下降,参考他人代码自己做了测试,写了个类以后有时间再去扩展,代码注释以后再加,作业好多:

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()

Python编程实现线性回归和批量梯度下降法代码实例

图1. 迭代过程中的误差cost

Python编程实现线性回归和批量梯度下降法代码实例

图2. 数据散点图和解直线

总结

以上就是本文关于Python编程实现线性回归和批量梯度下降法代码实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

Python算法输出1-9数组形成的结果为100的所有运算式

python中实现k-means聚类算法详解

Python编程实现粒子群算法(PSO)详解

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。