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)