Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- list_to_vec = lambda x: np.reshape(x, (len(x), 1))
- def h(x, theta):
- return x @ theta
- def error(x, y, theta):
- return h(x, theta) - list_to_vec(y)
- def cost(x, y, theta):
- diff = error(x, y, theta)
- return np.sum(diff.T @ diff) / 2
- class LearningAlgorithms:
- alpha = 0.01
- threshold = 1e-4
- @staticmethod
- def analytic_solution(x, y):
- return list_to_vec(np.linalg.inv(x.T @ x) @ x.T @ y)
- @staticmethod
- def batch_gradient_descent(x, y, theta):
- while True:
- for j in range(theta.shape[0]):
- summand = -error(x, y, theta).T @ list_to_vec(x[:, j])
- diff = LearningAlgorithms.alpha * np.sum(summand)
- theta[j] += diff
- if diff < LearningAlgorithms.threshold:
- return theta
- @staticmethod
- def stochastic_gradient_descent(x, y, theta):
- while True:
- for i in range(x.shape[0]):
- for j in range(theta.shape[0]):
- diff = LearningAlgorithms.alpha * (y[i] - h(x[i], theta))*x[i][j]
- theta[j] += LearningAlgorithms.alpha * diff
- if diff < LearningAlgorithms.threshold:
- return theta
- # load the data from a text file
- # imagine data as a m x n matrix (m examples, n columns)
- data = np.loadtxt('data.txt')
- # in every row, the input variables are the first n - 1 columns
- x = data[:,:-1]
- # we're going to need an intercept/bias term for a complete linear parameterization, independent of x
- # we call that x0 identically equal to unity
- x0 = np.ones((x.shape[0],1))
- x = np.hstack((x0, x))
- # in every row, the output variable is the last column
- y = data[:,-1]
- # theta are the weights/parameters
- theta = np.ones((x.shape[1], 1))
- print("Initial Cost: {}".format(cost(x, y, theta)))
- theta = LearningAlgorithms.stochastic_gradient_descent(x, y, theta)
- print("Cost after learning: {}".format(cost(x, y, theta)))
- plt.scatter(x[:,1:], y)
- plt.plot(x[:,1:], h(x, theta))
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement