# -*- coding: utf-8 -*-
""" Grid Search"""

import numpy as np
import costs


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 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]


# ***************************************************
# INSERT YOUR CODE HERE
# TODO: Paste your implementation of grid_search
#       here when it is done.
# ***************************************************


def grid_search(y,tx,w0,w1):
    loss = np.zeros((len(w0),len(w1)))
    min_loss = np.inf
    w_0 = 0
    w_1= 0
    for i in range(len(w0)):
        for j in range(len(w1)):
            loss[i][j] = costs.compute_loss(y,tx,[w0,w1])
            if loss[i][j] < min_loss:
                min_loss = loss[i][j]
                w_0 = w0[i]
                w_1 = w1[i]
                
    
                
    return loss, w_0, w_1


                