# What is Regularization?

### What is Regularization?

In Machine Learning, very often the task is to fit a model to a set of training data and use the fitted model to make predictions or classify new (out of sample) data points. Sometimes model fits the training data very well but does not well in predicting out of sample data points. A model may be too complex and overfit or too simple and underfit, either way giving poor predictions. Regularization is a way to avoid overfitting by penalizing high regression coefficients, it can be seen as a way to control the trade-off between bias and variance in favor of an increased generalization. In simple terms, it reduces parameters and simplifies the model or selects the preferred level of model complexity so it is better at predicting-generalizing.

To apply regularization two things are required:

• A way of quantifying how a good model is eg. cross-validation
• A tuning parameter which enables changing the complexity of the model

### How does Regularization work?

In order to find the best model, the common method in machine learning is to define a loss function that describes how well the model fits the data. The ultimate goal is to minimize this loss function. Regularization is the process of adding a tuning parameter to a model, this is most often done by adding a constant multiple to an existing weight vector. The model predictions should then minimize the mean of the loss function calculated on the regularized training set.

Most often used regularization methods:

• ##### ElasticNet

Example code of L1 regularization using Python:

```from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.cross_validation import train_test_split
import numpy as np

X = data['data']
y = data['target']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

for Coef in np.arange(0.1, 1,0.1):
clf = LogisticRegression(penalty='l1', C=Coef)
clf.fit(X_train, y_train)
print('C:', Coef)
print('Accuracy:', clf.score(X_test, y_test))
print('')

```

Was the above useful? Please share with others on social media.

If you want to look for more information, check some free online courses available at   coursera.orgedx.org or udemy.com.