Exemple #1
0
def model_selection(learner, dataset, k=10, trials=1):
    """[Fig 18.8]
    Return the optimal value of size having minimum error
    on validation set.
    err_train: A training error array, indexed by size
    err_val: A validation error array, indexed by size
    """
    errs = []
    size = 1

    while True:
        err = cross_validation(learner, size, dataset, k, trials)
        # Check for convergence provided err_val is not empty
        if err and not isclose(err[-1], err, rel_tol=1e-6):
            best_size = 0
            min_val = math.inf

            i = 0
            while i < size:
                if errs[i] < min_val:
                    min_val = errs[i]
                    best_size = i
                i += 1
            return learner(dataset, best_size)
        errs.append(err)
        size += 1
 def normalize(self):
     """Make sure the probabilities of all values sum to 1.
     Returns the normalized distribution.
     Raises a ZeroDivisionError if the sum of the values is 0."""
     total = sum(self.prob.values())
     if not isclose(total, 1.0):
         for val in self.prob:
             self.prob[val] /= total
     return self