# Logistic Regression With L1 Regularization

L1 regularization (also called least absolute deviations) is a powerful tool in data science. There are many tutorials out there explaining L1 regularization and I will not try to do that here. Instead, this tutorial is show the effect of the regularization parameter `C`

on the coefficients and model accuracy.

## Preliminaries

```
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
```

## Create The Data

The dataset used in this tutorial is the famous iris dataset. The Iris target data contains 50 samples from three species of Iris, `y`

and four feature variables, `X`

.

The dataset contains three categories (three species of Iris), however for the sake of simplicity it is easier if the target data is binary. Therefore we will remove the data from the last species of Iris.

```
/* Load the iris dataset */
iris = datasets.load_iris()
/* Create X from the features */
X = iris.data
/* Create y from output */
y = iris.target
/* Remake the variable, keeping all data where the category is not 2. */
X = X[y != 2]
y = y[y != 2]
```

## View The Data

```
/* View the features */
X[0:5]
```

```
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2]])
```

```
/* View the target data */
y
```

```
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
```

## Split The Data Into Training And Test Sets

```
/* Split the data into test and training sets, with 30% of samples being put into the test set */
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
```

## Standardize Features

Because the regularization penalty is comprised of the sum of the absolute value of the coefficients, we need to scale the data so the coefficients are all based on the same scale.

```
/* Create a scaler object */
sc = StandardScaler()
/* Fit the scaler to the training data and transform */
X_train_std = sc.fit_transform(X_train)
/* Apply the scaler to the test data */
X_test_std = sc.transform(X_test)
```

## Run Logistic Regression With A L1 Penalty With Various Regularization Strengths

The usefulness of L1 is that it can push feature coefficients to 0, creating a method for feature selection. In the code below we run a logistic regression with a L1 penalty four times, each time decreasing the value of `C`

. We should expect that as `C`

decreases, more coefficients become 0.

```
C = [10, 1, .1, .001]
for c in C:
clf = LogisticRegression(penalty='l1', C=c, solver='liblinear')
clf.fit(X_train, y_train)
print('C:', c)
print('Coefficient of each feature:', clf.coef_)
print('Training accuracy:', clf.score(X_train_std, y_train))
print('Test accuracy:', clf.score(X_test_std, y_test))
print('')
```

```
C: 10
Coefficient of each feature: [[-0.00902649 -3.83902983 4.34904293 0. ]]
Training accuracy: 0.9857142857142858
Test accuracy: 1.0
C: 1
Coefficient of each feature: [[ 0. -2.27441684 2.56760315 0. ]]
Training accuracy: 0.9857142857142858
Test accuracy: 1.0
C: 0.1
Coefficient of each feature: [[ 0. -0.82143435 0.97187285 0. ]]
Training accuracy: 0.9857142857142858
Test accuracy: 1.0
C: 0.001
Coefficient of each feature: [[0. 0. 0. 0.]]
Training accuracy: 0.5
Test accuracy: 0.5
```

Notice that as `C`

decreases the model coefficients become smaller (for example from `4.36276075`

when `C=10`

to `0.0.97175097`

when `C=0.1`

), until at `C=0.001`

all the coefficients are zero. This is the effect of the regularization penalty becoming more prominent.

# Python Example for Beginners

## Two Machine Learning Fields

There are two sides to machine learning:

**Practical Machine Learning:**This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.**Theoretical Machine Learning**: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

**Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes**

**Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!**

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

**Applied Statistics with R for Beginners and Business Professionals**

**Data Science and Machine Learning Projects in Python: Tabular Data Analytics**

**Data Science and Machine Learning Projects in R: Tabular Data Analytics**

**Python Machine Learning & Data Science Recipes: Learn by Coding**

**R Machine Learning & Data Science Recipes: Learn by Coding**

**Comparing Different Machine Learning Algorithms in Python for Classification (FREE)**

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.The information presented here could also be found in public knowledge domains.

**Google –> SETScholars**

A list of Python, R and SQL Codes for Applied Machine Learning and Data Science at **https://setscholars.net/**Learn by Coding Categories:

- Classification:
**https://setscholars.net/category/classification/** - Data Analytics:
**https://setscholars.net/category/data-analytics/** - Data Science:
**https://setscholars.net/category/data-science/** - Data Visualisation:
**https://setscholars.net/category/data-visualisation/** - Machine Learning Recipe:
**https://setscholars.net/category/machine-learning-recipe/** - Pandas:
**https://setscholars.net/category/pandas/** - Python:
**https://setscholars.net/category/python/** - SKLEARN:
**https://setscholars.net/category/sklearn/** - Supervised Learning:
**https://setscholars.net/category/supervised-learning/** - Tabular Data Analytics:
**https://setscholars.net/category/tabular-data-analytics/** - End-to-End Data Science Recipes:
**https://setscholars.net/category/a-star-data-science-recipe/** - Applied Statistics:
**https://setscholars.net/category/applied-statistics/** - Bagging Ensemble:
**https://setscholars.net/category/bagging-ensemble/** - Boosting Ensemble:
**https://setscholars.net/category/boosting-ensemble/** - CatBoost:
**https://setscholars.net/category/catboost/** - Clustering:
**https://setscholars.net/category/clustering/** - Data Analytics:
**https://setscholars.net/category/data-analytics/** - Data Science:
**https://setscholars.net/category/data-science/** - Data Visualisation:
**https://setscholars.net/category/data-visualisation/** - Decision Tree:
**https://setscholars.net/category/decision-tree/** - LightGBM:
**https://setscholars.net/category/lightgbm/** - Machine Learning Recipe:
**https://setscholars.net/category/machine-learning-recipe/** - Multi-Class Classification:
**https://setscholars.net/category/multi-class-classification/** - Neural Networks:
**https://setscholars.net/category/neural-networks/** - Python Machine Learning:
**https://setscholars.net/category/python-machine-learning/** - Python Machine Learning Crash Course:
**https://setscholars.net/category/python-machine-learning-crash-course/** - R Classification:
**https://setscholars.net/category/r-classification/** - R for Beginners:
**https://setscholars.net/category/r-for-beginners/** - R for Business Analytics:
**https://setscholars.net/category/r-for-business-analytics/** - R for Data Science:
**https://setscholars.net/category/r-for-data-science/** - R for Data Visualisation:
**https://setscholars.net/category/r-for-data-visualisation/** - R for Excel Users:
**https://setscholars.net/category/r-for-excel-users/** - R Machine Learning:
**https://setscholars.net/category/r-machine-learning/** - R Machine Learning Crash Course:
**https://setscholars.net/category/r-machine-learning-crash-course/** - R Regression:
**https://setscholars.net/category/r-regression/** - Regression:
**https://setscholars.net/category/regression/** - XGBOOST:
**https://setscholars.net/category/xgboost/** - Excel examples for beginners:
**https://setscholars.net/category/excel-examples-for-beginners/** - C Programming tutorials & examples:
**https://setscholars.net/category/c-programming-tutorials/** - Javascript tutorials & examples:
**https://setscholars.net/category/javascript-tutorials-and-examples/** - Python tutorials & examples:
**https://setscholars.net/category/python-tutorials/** - R tutorials & examples:
**https://setscholars.net/category/r-for-beginners/** - SQL tutorials & examples:
**https://setscholars.net/category/sql-tutorials-for-business-analyst/**

**FREE downloadable Mathematics Worksheet for Kids**)

- Year 1 Mathematics Worksheet:
**https://setscholars.net/category/year-1-mathematics-worksheet/**