coding utf8 linear combination of l1 and l2 penalties def penaltyself weights return 0 5 self c weights 2 1 0 self c np absweights coding utf 8 linear combination of l1 and l2 penalties
import numpy as np
from autograd import elementwise_grad


class Regularizer(object):
    def __init__(self, C=0.01):
        self.C = C
        self._grad = elementwise_grad(self._penalty)

    def _penalty(self, weights):
        raise NotImplementedError()

    def grad(self, weights):
        return self._grad(weights)

    def __call__(self, weights):
        return self.grad(weights)


class L1(Regularizer):
    def _penalty(self, weights):
        return self.C * np.abs(weights)


class L2(Regularizer):
    def _penalty(self, weights):
        return self.C * weights ** 2


class ElasticNet(Regularizer):
    

    def _penalty(self, weights):
        return 0.5 * self.C * weights ** 2 + (1.0 - self.C) * np.abs(weights)