How to find optimal parameters using RandomizedSearchCV?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to find optimal parameters using RandomizedSearchCV?

This recipe helps you find optimal parameters using RandomizedSearchCV

1
This python source code does the following: 1. Imports the necessary libraries 2. Loads the dataset and performs train_test_split 3. Applies GradientBoostingClassifier and evaluates the result 4. Hyperparameter tunes the GBR Classifier model using RandomSearchCV
In [1]:
```def Snippet_196():
print()
print(format('How to find parameters using RandomizedSearchCV','*^82'))

import warnings
warnings.filterwarnings("ignore")

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform as sp_randFloat
from scipy.stats import randint as sp_randInt

X = dataset.data; y = dataset.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

parameters = {'learning_rate': sp_randFloat(),
'subsample'    : sp_randFloat(),
'n_estimators' : sp_randInt(100, 1000),
'max_depth'    : sp_randInt(4, 10)
}

randm = RandomizedSearchCV(estimator=model, param_distributions = parameters,
cv = 2, n_iter = 10, n_jobs=-1)
randm.fit(X_train, y_train)

# Results from Random Search
print("\n========================================================")
print(" Results from Random Search " )
print("========================================================")
print("\n The best estimator across ALL searched params:\n",
randm.best_estimator_)
print("\n The best score across ALL searched params:\n",
randm.best_score_)
print("\n The best parameters across ALL searched params:\n",
randm.best_params_)
print("\n ========================================================")

Snippet_196()
```
```*****************How to find parameters using RandomizedSearchCV******************
```
```/Users/nilimesh/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
DeprecationWarning)
```
```========================================================
Results from Random Search
========================================================

The best estimator across ALL searched params:
learning_rate=0.02933763179021598, loss='deviance',
max_depth=6, max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=973,
n_iter_no_change=None, presort='auto', random_state=None,
subsample=0.34643411696436155, tol=0.0001,
validation_fraction=0.1, verbose=0, warm_start=False)

The best score across ALL searched params:
0.9473684210526315

The best parameters across ALL searched params:
{'learning_rate': 0.02933763179021598, 'max_depth': 6, 'n_estimators': 973, 'subsample': 0.34643411696436155}

========================================================
```

#### Relevant Projects

##### Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

##### Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

##### German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

##### PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

##### Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

##### Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

##### Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.