python only implementation of regularization