import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Importing data
print('Importing data...')
data = pd.read_csv('ex1data1.txt', sep=',', names=['population', 'profit'])
X = data['population'].values
y = data['profit'].values
# Plotting data
print('Plotting data...')
plt.scatter(X, y, color='r', marker='x')
plt.title('Profit vs Population')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $ 10,000s')
plt.show()
input("Press Enter to continue...")
# Define cost function
def compute_cost(X, y, theta):
prediction = np.dot(X, theta)
error_vector = (prediction - y)
error_square_sum = np.sum((error_vector * error_vector))
return error_square_sum/(2*y.shape[0])
# Test correctness of cost function
m = X.shape[0] # number of training examples
X = np.column_stack((np.ones(m), X)) # add constant column to X
y = y.reshape(m, 1)
theta = np.zeros((2, 1)) # initial fitting parameter
iteration = 1500
alpha = 0.01
print("Check the accuracy of cost function...\n")
print('The cost with theta = [0 ; 0] should be around 32.07')
print('The calculated value is ', compute_cost(X, y, theta), '\n')
print('The cost with theta = [-1 ; 2] should be around 54.24')
print('The calculated value is ', compute_cost(X, y, np.array([[-1], [2]])), '\n')
input("Press Enter to continue...\n")
# Define gradient decent method to find the fitting parameters
def gradient_decent(X, y, theta, alpha, iteration):
m = y.shape[0]
j_history = {}
for i in range(iteration):
prediction = np.dot(X, theta)
updates = np.dot(X.T, (prediction - y))
theta = theta - alpha * (1.0 / m) * updates
j_history[i] = compute_cost(X, y, theta)
return theta, j_history
# Test correctness of gradient_decent method
theta, j_history = gradient_decent(X, y, theta, alpha, iteration)
print('Theta found by gradient descent:\n')
print(theta)
print('Expected theta values (approx)\n')
print(' -3.6303\n1.1664\n\n')
# Define function to plot cost history
def plot_cost_as_iteration(j_history):
lists = sorted(j_history.items()) # sorted by key, return a list of tuples
x, y = zip(*lists) # unpack a list of pairs into two tuples
plt.plot(x, y, marker='o', markevery=50)
plt.title('Cost History')
plt.xlabel('Number of iteration')
plt.ylabel('Cost')
plt.show()
# Define function to plot fitted data
def plot_linear_fit(x, y, theta):
m = y.shape[0]
plt.scatter(x, y)
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $ 10,000s')
x_modified = np.column_stack((np.ones(m), x))
calculated_y = np.dot(x_modified, theta)
plt.plot(x, calculated_y, color='r')
plt.show()
# Plot cost history
print('Plotting j_history\n')
plot_cost_as_iteration(j_history)
# Plot fitted data
print('Plotting linear fit on the raw data\n')
plot_linear_fit(data['population'], data['profit'], theta)
# Predict values for population sizes of 35,000 and 70,000
predict1 = np.array([1, 3.5]).dot(theta)
print('For population = 35,000, we predict a profit of%f\n')
print(predict1*10000)
predict2 = np.array([1, 7]).dot(theta)
print('For population = 70,000, we predict a profit of%f\n')
print(predict2*10000)