# Why would you use Regularization and what it is?

### Why would you use Regularization and what it is?

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.

### What is Regularization ?

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.  Data Science from Scratch: First Principles with Python Data science libraries, frameworks, modules, and toolkits are great for doing data science, but they’re also a good way to dive into the discipline without actually understanding data science. In this book, you’ll learn how many of the most fundamental data science tools and algorithms work by implementing them from scratch. If you have an aptitude for mathematics and some programming skills, author Joel Grus will help you get comfortable with the math and statistics at the core of data science, and with hacking skills you need to get started as a data scientist. Today’s messy glut of data holds answers to questions no one’s even thought to ask. This book provides you with the know-how to dig those answers out. Get a crash course in Python Learn the basics of linear algebra, statistics, and probability—and understand how and when they're used in data science Collect, explore, clean, munge, and manipulate data Dive into the fundamentals of machine learning Implement models such as k-nearest Neighbors, Naive Bayes, linear and logistic regression, decision trees, neural networks, and clustering Explore recommender systems, natural language processing, network analysis, MapReduce, and databases