Stanford Machine Learning ex1.1 (python)

Source: Internet
Author: User

Tools used: NumPy and Matplotlib

NumPy is the most basic Python programming library in the book. In addition to providing some advanced mathematical algorithms, it also has a very efficient vector and matrix operations function. These are particularly important for computational tasks for machine learning. Because both the characteristics of the data, or the batch design of parameters, are inseparable from the more efficient matrix and vector computing. And NumPy more prominent is its unique internal design, so that the processing of these matrices and vector computing than the average programmer to write their own, or even Python's own library run more efficient operation is much higher.

Matplotlib is a free-to-Test drawing toolkit in a python programming environment that works in almost the same way as a matlab drawing command.

Operation Steps:

1. Initialize the data and store the data in X, Y.

    Print("plotting data...\n") Fr=open ('Ex1data1.txt') Arraylines=fr.readlines () numberoflines=Len (arraylines) x=np.zeros ((numberoflines,1)) Y=np.zeros ((numberoflines,1)) Index=0 forLineinchArraylines:line=Line.strip () listformline= Line.split (",") X[index,:]= Listformline[:1] Y[index]= Listformline[-1] Index+ = 1

2. The cost function is obtained

def Computecost (X,y,theta):    m=x.shape[0]    xmatrix=Np.mat (X)    Ymatrix=  Np.mat (y)    thetamatrix=Np.mat (theta)    J=1/(2*float (m)) *sum (Np.array (xmatrix* Thetamatrix-ymatrix)) **2)    return J

3. Take the gradient descent algorithm to calculate, first of all theta0 and theta1 are initialized to 0, and then the Alpha 0.01, the calculation

 def   Gradientdescent (X,y,theta,alpha, iterations): M  =len (y) j_history  =np.zeros ((Iterations,1 =theta.copy ()   For  i in   range (iterations): Theta[0]=theta[0]-(alpha/m) *np.sum (Np.mat (X) *np.mat (theta_s)-np.mat (y)) P1  =np.mat ( X) *np.mat (theta_s)-np.mat (y) p2  =x[:,1]*p1 t heta[ 1]=theta[1]-(alpha/m) *p2 theta_s  = Theta.copy () J_history[i,:]  =computecost (x,y,theta)  return  Theta 

4. Visualize the data

Detail code: https://github.com/xingxiaoyun/StanfordMachineLearning/blob/master/ex1.py

Stanford Machine Learning ex1.1 (python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.