LeiGaoCoder的个人博客分享 http://blog.sciencenet.cn/u/LeiGaoCoder

博文

Linear regression

已有 2557 次阅读 2013-12-2 19:48 |个人分类:MachineLearning|系统分类:科研笔记

I try to write andrew ML ex1  with pyton to learn, as the saying goes "Practice Makes good".Linear regression.


from numpy import *

def loadData(fileName):

   fr = open(fileName)

   numFeat = len(open(fileName).readline().split(',')) - 1

   x = [];

   y = [];

   for line in fr.readlines():

       lineArr = []

       curLine = line.strip().split(',')

       for i in range(numFeat):

           lineArr.append(float(curLine[i]))

       x.append(lineArr)

       y.append(float(curLine[-1]))

   return x, y

def computeCost(xmat,ymat,thetamat):

   m = ymat.shape[0]

   tempmat  = xmat*thetamat-ymat

   J = tempmat.T*tempmat/2./m

   return J[0][0]

def gradientDescent(xmat, ymat, thetamat, alpha, num_iters):

  m = ymat.shape[0]

  J_history = zeros((num_iters, 1))

  for iter in range(num_iters):

      #g = dot(X.T,(dot(X,theta)-array(y).T))/m

 

      thetamat = thetamat -alpha*(xmat.T*(xmat*thetamat-ymat))/m

      J_history[iter] = computeCost(xmat, ymat, thetamat);


  return thetamat, J_history




if __name__=="__main__":

   X,y = loadData("ex1data1.txt");


   theta = zeros((2, 1))

   iterations = 1500;

   alpha = 0.01;

   m = len(y)

   new_col = ones((m,1))

   xmat = mat(X)

   xmat = concatenate((new_col,xmat),1)

   ymat = mat(y).T

   computeCost(xmat, ymat, theta)

   restheta,res2 = gradientDescent(xmat, ymat, theta, alpha, iterations);

   print restheta

   import matplotlib.pyplot as plt

   fig = plt.figure()

   ax = fig.add_subplot(111)

   ax.scatter(X,y)

   myx  = range(30)

   myy  = restheta[0,0]+restheta[1,0]*myx

   ax.plot(myx,myy,s=2,c='red')

   plt.show()

   


   

   plt.show()

   pause()





https://blog.sciencenet.cn/blog-1013787-746521.html

上一篇:SVM笔记
下一篇:Amessage alooper ahandler ALooperRoster
收藏 IP: 111.161.9.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-21 09:34

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部