Example #1
0
def abs_error(x: X, theta: Vector, y: Vector) -> Tuple[Scalar, Vector]:
    pred = mult_mv(x.by_sample(), theta)
    diff = diff_vv(pred, y)
    error = sum(map(lambda e: abs(e), diff))

    sign = vector(map(lambda d: signum(d), diff))
    grad = mult_mv(x.by_feature(), sign)

    return error, grad
    def _train(self, x: X, y: Vector, cost: Cost, step: float,
               stdev: float) -> Vector:
        x = x.append_ones()
        m = x.nsamples()

        theta = vector(
            map(lambda _: gauss(0., stdev),
                range(x.nfeatures() - 1))) + (gauss(mean(y), stdev), )

        stop_condition = self.stop_condition
        while True:
            error, gradient = cost(x, theta, y)
            error, gradient = error / m, mult_vs(gradient, 1 / m)

            theta = sum_vv(theta, mult_vs(gradient, -step))

            stop_condition, stop = stop_condition.update(gradient, error)
            if stop:
                break
        return theta
def _min_max_1(data: Vector) -> Vector:
    minim, maxim = min(data), max(data)
    return vector(map(lambda x: (x - minim) / (maxim - minim), data))
def _standard(data: Vector) -> Vector:
    avg, sdev = mean(data), stdev(data)
    return vector(map(lambda x: (x - avg) / sdev, data))
def _min_max_2(data: Vector) -> Vector:
    avg, minim, maxim = mean(data), min(data), max(data)
    return vector(map(lambda x: (x - avg) / (maxim - minim), data))
Example #6
0
 def append_ones(self) -> 'X':
     ones = vector(repeat(1., self._m))
     extended = self.by_feature() + (ones, )
     return X(vector(zip(*extended)))
Example #7
0
 def convert(self, basis_functions: Tuple[BasisFunction, ...]) -> 'X':
     sample_mapper = lambda s: vector(map(lambda bf: bf(s), basis_functions)
                                      )
     samples = map(lambda sample: sample_mapper(sample), self._data)
     return X(vector(samples))
Example #8
0
 def normalize(self, scaling_type: ScalingType) -> 'X':
     normalized_vectors = map(lambda f: normalize(f, scaling_type),
                              self.by_feature())
     return X(vector(zip(*normalized_vectors)))
Example #9
0
 def by_feature(self) -> Matrix:
     return vector(zip(*self._data))
Example #10
0
def lasso(theta: Vector, lmb: Scalar) -> Tuple[Scalar, Vector]:
    # ignore bias
    cost = lmb * reduce(lambda acc, t: acc + abs(t), theta[:-1], 0)
    grad = vector(map(lambda t: lmb * signum(t), theta[:-1])) + (0., )
    return cost, grad
Example #11
0
def ridge(theta: Vector, lmb: Scalar) -> Tuple[Scalar, Vector]:
    # ignore bias
    cost = lmb * mult_v(theta[:-1])
    grad = vector(map(lambda t: 2 * lmb * t, theta[:-1])) + (0., )
    return cost, grad