import pandas as pd import matplotlib.pyplot as plt import numpy as np
data=pd.read_csv(‘ex1data1.txt’,names=[‘population’,‘profit’])
data.head()
data.insert(0,‘one’,1) data.head()
cols=data.shape[1] X=data.iloc[:,0:cols-1] y=data.iloc[:,cols-1:cols]
X.head() y.head()
X=np.matrix(X.values) y=np.matrix(y.values) theta=np.matrix(np.array([0,0]))
X.shape y.shape theta.shape
def computeCost(X,y,theta): inner=np.power(((Xtheta.T)-y),2) return np.sum(inner)/(2len(X))
computeCost(X,y,theta)
alpha=0.01 iters=1000
def gradientDescent(X,y,theta,alpha,iters): temp=np.matrix(np.zeros(theta.shape)) parameters=int(theta.ravel().shape[1]) cost=np.zeros(iters) for i in range(iters): error=(X*theta.T)-y for j in range(parameters): term=np.multiply(error,X[:,j]) temp[0,j]=theta[0,j]-((alpha/len(X))*np.sum(term)) theta=temp cost[i]=computeCost(X,y,theta) return theta,cost
g,cost=gradientDescent(X,y,theta,alpha,iters) computeCost(X,y,g)
def normalEpn(X,y): theta=np.linalg.inv(X.T@X)@X.T@y return theta
final_theta2=normalEpn(X,y)
computeCost(X,y,final_theta2.T)