You are on page 1of 4

9/7/23, 8:24 PM machine_learning_refined/exercises/ed_2/chapter_5/chapter_5_exercises.

ipynb at main · jermwatt/machine_learning_refined · GitHub

jermwatt/machine_learning_refined Public

Code Issues 1 Pull requests 1 Actions Projects Security Ins

machine_learning_refined / exercises / ed_2 / chapter_5 / chapter_5_exercises.ipynb


jermwatt 6 months ago
Executable File · 330 lines (330 loc) · 8.62 KB

Preview Code Blame

https://github.com/jermwatt/machine_learning_refined/blob/main/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb 1/4
9/7/23, 8:24 PM machine_learning_refined/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb at main · jermwatt/machine_learning_refined · GitHub

https://github.com/jermwatt/machine_learning_refined/blob/main/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb 2/4
9/7/23, 8:24 PM machine_learning_refined/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb at main · jermwatt/machine_learning_refined · GitHub

Table of Contents
1 Exercise 5.1. Fitting a regression line to the student debt data
2 Exercise 5.2. Kleiber’s law and linear regression
3 Exercise 5.3. The Least Squares cost function and a single Newton step
4 Exercise 5.6. Compare the Least Squares and Least Absolute Deviation costs
5 Exercise 5.7. Empirically confirm convexity for a toy dataset
6 Exercise 5.9. Housing price and Automobile Miles-per-Gallon prediction
7 Exercise 5.11. Multi-output regression
In [1]:
# import autograd-wrapped numpy
import autograd.numpy as np

# datapath to data
datapath = '../mlrefined_datasets/superlearn_datasets/'

Exercise 5.1. Fitting


the student debt data a regression line to
In [2]:
# import the dataset
csvname = datapath + 'student_debt_data.csv'
data = np.loadtxt(csvname,delimiter=',')

# extract input - for this dataset, these are times


x = data[:,0]

# extract output - for this dataset, these are total student debt
y = data[:,1]

print(np.shape(x))
print(np.shape(y))

(40,)
(40,)

Exercise 5.2.
regression Kleiber’s law and linear
In [3]:
# import the dataset
csvname = datapath + 'kleibers_law_data.csv'
data = np.loadtxt(csvname,delimiter=',')
x = data[:-1,:]
y = data[-1:,:]

print(np.shape(x))
print(np.shape(y))

(1, 1498)
(1, 1498)

https://github.com/jermwatt/machine_learning_refined/blob/main/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb 3/4
Exercise 5.3. The Least Squares cost
9/7/23, 8:24 PM machine_learning_refined/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb at main · jermwatt/machine_learning_refined · GitHub

function and a single Newton step


In [4]:
# load in data
csvname = datapath + '3d_linregress_data.csv'
data = np.loadtxt(csvname,delimiter=',')
x = data[:-1,:]
y = data[-1:,:]

print(np.shape(x))
print(np.shape(y))

(2, 50)
(1, 50)

Exercise 5.6. Compare the Least Squares


and Least Absolute Deviation costs
In [5]:
# load in dataset
data = np.loadtxt(datapath + 'regression_outliers.csv',delimiter = ','
x = data[:-1,:]
y = data[-1:,:]

print(np.shape(x))
print(np.shape(y))

(1 10)

https://github.com/jermwatt/machine_learning_refined/blob/main/exercises/ed_2/chapter_5/chapter_5_exercises.ipynb 4/4

You might also like