forked from epfml/OptML_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e08607d
commit 803b226
Showing
8 changed files
with
11,737 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.