Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- import matplotlib.pyplot as plt
- # load file (housing.data.txt)
- def load():
- data = numpy.loadtxt(open("housing.data.txt", "rb"), dtype="float")
- data = data[:, [5,13]] # get all data for 5th and 13th column as x y
- data.sort()
- return data
- # hypothesis function (theta0 + theta1 * x)
- def hyp(theta, x):
- return theta[0] + theta[1] * x
- # derivative for theta 0
- def der0(theta, data):
- sum = 0
- m = data.shape[0] # get rows data.shape = [r, c]
- for x in range(0, m):
- x_val = data[x][0]
- y_val = data[x][1]
- # xth row, first column (x's column) ; xth row, second column (y's column)
- sum += hyp(theta, x_val) - y_val
- return sum / 2
- # derivative for theta 1
- def der1(theta, data):
- sum = 0
- m = data.shape[0] # get rows data.shape = [r, c]
- for x in range(0, m):
- x_val = data[x][0]
- y_val = data[x][1]
- # xth row, first column (x's column) ; xth row, second column (y's column)
- sum += (hyp(theta, x_val) - y_val) * x_val
- return sum / 2
- # cost function
- def cost(theta, data):
- sum = 0.0
- m = data.shape[0]
- for x in range(0, m):
- x_val = data[x][0]
- y_val = data[x][1]
- sum += (hyp(theta, x_val) - y_val) ** 2
- return sum / (2 * m)
- # alpha value
- alpha = 0.0001
- data = load()
- theta = [0, 0]
- plt.scatter(data[:, 0], data[:, 1])
- # do gradient descent 5000 times
- for x in range(0, 500):
- newtheta0 = theta[0] - alpha * der0(theta, data)
- newtheta1 = theta[1] - alpha * der1(theta, data)
- theta[0] = newtheta0
- theta[1] = newtheta1
- print(theta[0])
- print(theta[1])
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement