Skip to content

Commit

Permalink
lab 2 and solutions 1
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaggi committed Mar 2, 2022
1 parent e08607d commit 803b226
Show file tree
Hide file tree
Showing 8 changed files with 11,737 additions and 0 deletions.
Binary file added labs/ex01/solutions01.pdf
Binary file not shown.
Binary file added labs/ex02/exercise02.pdf
Binary file not shown.
1,030 changes: 1,030 additions & 0 deletions labs/ex02/template/Concrete_Data.csv

Large diffs are not rendered by default.

551 changes: 551 additions & 0 deletions labs/ex02/template/Lab 2 - Gradient Descent.ipynb

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions labs/ex02/template/grid_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""Exercise 2.
Grid Search
"""

import numpy as np


def generate_w(num_intervals):
"""Generate a grid of values for w0 and w1."""
w0 = np.linspace(-100, 200, num_intervals)
w1 = np.linspace(-150, 150, num_intervals)
return w0, w1


def grid_search(y, tx, w0, w1):
"""Algorithm for grid search."""
losses = np.zeros((len(w0), len(w1)))
# compute loss for each combination of w0 and w1.
for ind_row, row in enumerate(w0):
for ind_col, col in enumerate(w1):
w = np.array([row, col])
e = y - tx.dot(w)
losses[ind_row, ind_col] = 1/2*np.mean(e**2)
return losses


def get_best_parameters(w0, w1, losses):
"""Get the best w from the result of grid search."""
min_row, min_col = np.unravel_index(np.argmin(losses), losses.shape)
return losses[min_row, min_col], w0[min_row], w1[min_col]
Loading

0 comments on commit 803b226

Please sign in to comment.