HYPERPARAMETER TUNING
# How to optimize hyper parameters of a Logistic Regression model using Grid Search in Python?

# How to optimize hyper parameters of a Logistic Regression model using Grid Search in Python?

This recipe helps you optimize hyper parameters of a Logistic Regression model using Grid Search in Python

This data science python source code does the following: 1. Hyper-parameters of logistic regression.2. Implements Standard Scaler function on the dataset.3. Performs train_test_split on your dataset. 4. Uses Cross Validation to prevent overfitting.

In [2]:

```
## How to optimize hyper-parameters of a Logistic Regression model using Grid Search in Python
def Snippet_145():
print()
print(format('How to optimize hyper-parameters of a LR model using Grid Search in Python','*^82'))
import warnings
warnings.filterwarnings("ignore")
# load libraries
import numpy as np
from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
# Load the iris flower data
dataset = datasets.load_iris()
X = dataset.data
y = dataset.target
# Create an scaler object
sc = StandardScaler()
# Create a pca object
pca = decomposition.PCA()
# Create a logistic regression object with an L2 penalty
logistic = linear_model.LogisticRegression()
# Create a pipeline of three steps. First, standardize the data.
# Second, tranform the data with PCA.
# Third, train a logistic regression on the data.
pipe = Pipeline(steps=[('sc', sc),
('pca', pca),
('logistic', logistic)])
# Create Parameter Space
# Create a list of a sequence of integers from 1 to 30 (the number of features in X + 1)
n_components = list(range(1,X.shape[1]+1,1))
# Create a list of values of the regularization parameter
C = np.logspace(-4, 4, 50)
# Create a list of options for the regularization penalty
penalty = ['l1', 'l2']
# Create a dictionary of all the parameter options
# Note has you can access the parameters of steps of a pipeline by using '__’
parameters = dict(pca__n_components=n_components,
logistic__C=C,
logistic__penalty=penalty)
# Conduct Parameter Optmization With Pipeline
# Create a grid search object
clf = GridSearchCV(pipe, parameters)
# Fit the grid search
clf.fit(X, y)
# View The Best Parameters
print('Best Penalty:', clf.best_estimator_.get_params()['logistic__penalty'])
print('Best C:', clf.best_estimator_.get_params()['logistic__C'])
print('Best Number Of Components:', clf.best_estimator_.get_params()['pca__n_components'])
print(); print(clf.best_estimator_.get_params()['logistic'])
# Use Cross Validation To Evaluate Model
CV_Result = cross_val_score(clf, X, y, cv=4, n_jobs=-1)
print(); print(CV_Result)
print(); print(CV_Result.mean())
print(); print(CV_Result.std())
Snippet_145()
```

Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.