Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import numpy as np
- import pandas as pd
- from matplotlib import pyplot
- import matplotlib.pyplot as plt
- path = os.path.dirname(os.path.realpath(__file__)) + '\..\data\ex1data2.txt'
- data = pd.read_csv(path, header=None, names=['Size', 'Bedrooms', 'Price'])
- print('Data before normalizing:')
- print(data.head(n=5))
- #data.plot.bar()
- # Feature normalization
- data = (data - data.mean())/data.std()
- print('Data after normalizing:')
- print(data.head(n=5))
- #data.plot.bar()
- # Add a column of 1
- data.insert(0, 'Ones', 1)
- #print(data.head(n=5))
- # Number of columns
- cols = data.shape[1]
- # some parameters
- theta = np.mat(np.zeros(3))
- iteration = 1500
- alpha = 0.01
- # inputs and outputs
- X = data.iloc[:,:cols-1]
- y = data.iloc[:,-1:]
- print('Input:')
- print(X.head(n=5))
- print('Output:')
- print(y.head(n=5))
- X = np.mat(X)
- y = np.mat(y)
- m = len(X)
- def computeCost(X, y, theta):
- inner = np.power((X * theta.T - y), 2)
- return np.sum(inner)/(2 * len(X))
- # print cost before training
- print('Initial cost')
- print(computeCost(X, y, theta))
- def gradientDescent(X, y, theta, alpha, iters):
- cost = np.zeros(iters)
- param = int(theta.ravel().shape[1])
- his_theta = np.zeros((iters, 3)) # to store all thetas
- for i in range(iters):
- error = X * theta.T - y
- for j in range(param):
- temp = np.multiply(error, X[:,j])
- theta[0,j] = theta[0,j] - (alpha / m) * np.sum(temp)
- his_theta[i,j] = theta[0,j]
- cost[i] = computeCost(X, y, theta)
- return theta, cost, his_theta
- # perform gradient descent to "fit" the model parameters
- g, cost, history = gradientDescent(X, y, theta, alpha, iteration)
- print('The cost after training:')
- print(computeCost(X, y, g))
- ################################################################
- fig, ax = plt.subplots(figsize=(12, 8))
- ax.plot(np.arange(iteration), cost, 'r')
- ax.set_xlabel('Iterations')
- ax.set_ylabel('Cost')
- ax.set_title('Cost vs. Iterations')
- ax.grid()
- #plt.xticks(np.arange(1000))
- #plt.yticks(np.arange(4, 7, 0.5))
- pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment