machine_learning_hw1
In [4]:
# import statements
datapath = ‘datasets/’
from autograd import numpy as np
A gradient descent and cost function history plotting function¶
In [5]:
# import automatic differentiator to compute gradient module
from autograd import grad
# gradient descent function
def gradient_descent(g,alpha,max_its,w):
# compute gradient module using autograd
gradient = grad(g)
# run the gradient descent loop
weight_history = [w] # weight history container
cost_history = [g(w)] # cost function history container
for k in range(max_its):
# evaluate the gradient
grad_eval = gradient(w)
# take gradient descent step
w = w – alpha*grad_eval
# record weight and cost
weight_history.append(w)
cost_history.append(g(w))
return weight_history,cost_history
In [6]:
# the import statement for matplotlib
import matplotlib.pyplot as plt
# cost function history plotter
def plot_cost_histories(cost_histories,labels):
# create figure
plt.figure()
# loop over cost histories and plot each one
for j in range(len(cost_histories)):
history = cost_histories[j]
label = labels[j]
plt.plot(history,label = label)
plt.legend(loc=’center left’, bbox_to_anchor=(1, 0.5))
plt.show()
Chapter 5 Exercises¶
Complete exercises 2, 6, and 7 from Chapter 5 – make sure to download the most recent version of the text. Below we load in each dataset.
Exercise 2¶
In [7]:
# load in dataset
csvname = datapath + ‘kleibers_law_data.csv’
data = np.loadtxt(csvname,delimiter=’,’)
# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:]
Exercise 6¶
In [8]:
# load in dataset
csvname = datapath + ‘regression_outliers.csv’
data = np.loadtxt(csvname,delimiter = ‘,’)
# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:]
Exercise 7¶
In [9]:
# load in dataset
csvname = datapath + ‘linear_2output_regression.csv’
data = np.loadtxt(csvname,delimiter=’,’)
# get input and output of dataset
x = data[:2,:]
y = data[2:,:]
Chapter 6¶
Complete exercises 7, 8, 9, and 14 from Chapter 6 – make sure to download the most recent version of the text. Below we load in each dataset.
Exercise 7¶
In [10]:
# load in dataset
csvname = datapath + ‘2d_classification_data_v1.csv’
data = np.loadtxt(csvname,delimiter = ‘,’)
# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:]
Exercise 8¶
In [11]:
# load in dataset
csvname = datapath + ‘3d_classification_data_v0.csv’
data = np.loadtxt(csvname,delimiter = ‘,’)
# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:]
Exercise 9¶
In [12]:
# load in dataset
csvname = datapath + ‘3d_classification_data_v0.csv’
data = np.loadtxt(csvname,delimiter = ‘,’)
# get input and output of dataset
x = data[:-1,:]
y = data[-1:,:]
Exercise 14¶
In [13]:
# load in dataset
csvname = datapath + ‘breast_cancer_data.csv’
data1 = np.loadtxt(csvname,delimiter = ‘,’)
# get input and output of dataset
x = data1[:-1,:]
y = data1[-1:,:]