I am currently doing Andrew Ng's course on ML using Python, I googled to see if my outputs matched others but I realised just because of using numpy instead of panda my final output shifted
Expected : 2105448288.6292474 My output : 2064911681.618526
is this much discrepancy often observed when using different modules?
Reference Code:
import numpy as np
import pandas as pd
data = pd.read_csv('ex1data2.txt', sep = ',', header = None)
X = data.iloc[:,0:2] # read first two columns into X
y = data.iloc[:,2] # read the third column into y
m = len(y) # no. of training samples
data.head()
X = (X - np.mean(X))/np.std(X)
ones = np.ones((m,1))
X = np.hstack((ones, X))
alpha = 0.01
num_iters = 400
theta = np.zeros((3,1))
y = y[:,np.newaxis]
def computeCostMulti(X, y, theta):
temp = np.dot(X, theta) - y
return np.sum(np.power(temp, 2)) / (2*m)
J = computeCostMulti(X, y, theta)
def gradientDescentMulti(X, y, theta, alpha, iterations):
m = len(y)
for _ in range(iterations):
temp = np.dot(X, theta) - y
temp = np.dot(X.T, temp)
theta = theta - (alpha/m) * temp
return theta
theta = gradientDescentMulti(X, y, theta, alpha, num_iters)
J = computeCostMulti(X, y, theta)
print(J)
My Code:
import numpy as np
from matplotlib import pyplot as plt
data = np.loadtxt("ex1data2.txt", delimiter = ",", dtype = 'int')
x = data[:, 0:2]
y = data[:, 2]
m = len(y)
x = x.reshape(m,2)
y = y.reshape(m,1)
one = np.ones((m,1))
X = np.matrix(x)
X = (X - np.mean(X))/np.std(X)
X = np.concatenate((one,X), axis = 1)
theta = np.zeros((3, 1))
def cc(theta, X, y):
A = np.dot(X,theta)-y
return float(((1/(2*m)) * np.dot(A.T, A)))
def gd(theta, X, y, alpha, iterations):
for i in range(iterations):
h = np.dot(X, theta)-y
h = np.dot(X.T, h)
theta = theta - (alpha/m) * h
return theta
theta = (gd(theta, X, y, 0.01, 400))
print(cc(theta, X, y))