#!/usr/bin/env python
# coding: utf-8

# # <span style=&#039;color:DarkBlue&#039;> Day21 - PyCaret for  </span> <span style=&#039;color:Red&#039;> Regression </span>
# 
# - ### An open source automated library for Machine Learning.
# - ### <span style=&#039;color:Red&#039;> Three Step Process</span> to build machine learning models for:
#     - Classification
#     - Regression
#     - Clustering
# 
# ### Self Learning Resource
# 1. Explore Pycaret mannual on Regression: <a href="https://pycaret.org/regression/"> Click Here </a>
# 2. Tutorial on Pycaret <a href="https://pycaret.readthedocs.io/en/latest/tutorials.html"> Click Here</a> 
# 
# 
# 
# ### <span style=&#039;color:DarkBlue&#039;> Method 1</span>: To install `pycaret`
# - Installing PyCaret in Local Jupyter Notebook, Google Colab or Azure Notebooks
#     - Using conda: `!conda install pycaret`
#     - Using pip: `!pip install pycaret`
# - Installing PyCaret in Anaconda
#     - Using conda: `conda install pycaret`
#     - Using pip: `pip install pycaret`
# 
# 
# ### <span style=&#039;color:DarkBlue&#039;> Method 2</span>: To install `pycaret` | Online manual to install pycaret <a href="https://pycaret.org/install/"> Click Here</a> 
# -  <span style=&#039;color:DarkRed&#039;> Step 1</span>: To Install pycaret (One Time)
#     - Open Anaconda prompt
#     - Create a conda environment: `conda create --name myenv python=3.6`
#     - Activate environment: `conda activate myenv`
#     - To install pycaret: `pip install pycaret`
# 
# -  <span style=&#039;color:DarkRed&#039;> Step 2</span>: To use pycaret environment through Jypyter notebook (Always)
#     - Open Anaconda prompt
#     - Activate environment: `conda activate myenv`
#     - Start Jupyter Notebook: `jupyter notebook`
# 
# 
# 
# ### In this tutorial we will learn:
# 
# - Getting Data: How to import data from PyCaret repository
# - Setting up Environment: How to setup an experiment in PyCaret and get started with building regression models
# - Create Model: How to create a model, perform cross validation and evaluate regression metrics
# - Tune Model: How to automatically tune the hyperparameters of a regression model
# - Plot Model: How to analyze model performance using various plots
# - Finalize Model: How to finalize the best model at the end of the experiment
# - Predict Model: How to make prediction on new / unseen data
# - Save / Load Model: How to save / load a model for future use
# 

# # <span style=&#039;color:Red&#039;> 1. Regression: Basics </span>

# ### <span style=&#039;color:DarkBlue&#039;>1.1 Data loading</span>

# #### Get the version of the pycaret

# In[1]:


from pycaret.utils import version
version()


# #### Loading dataset from pycaret

# In[2]:


from pycaret.datasets import get_data


# #### Get the list of datasets available in pycaret

# In[3]:


# Internet connection is required
dataSets = get_data(&#039;index&#039;)
dataSets


# #### Get boston dataset

# In[19]:


# Internet connection is required
boston_df = get_data("traffic")
# This is regression dataset. The values in medv are continuous values


# #### Get the dimention of dataset

# In[20]:


print(boston_df.shape)


# #### Remove duplicates

# In[21]:


print(boston_df.shape)
boston_df.drop_duplicates()
print(boston_df.shape)


# ### <span style=&#039;color:DarkBlue&#039;>1.2 Parameter setting for all regression models</span>
# - Train/Test division
# - Sampling
# - Normalization
# - Transformation
# - PCA (Dimention Reduction)
# - Handaling of Outliers
# - Feature Selection

# #### Setup parameters for regression models (defaults)

# In[22]:


from pycaret.regression import *
reg = setup(data = boston_df, target=&#039;traffic_volume&#039;,fold = 15,data_split_shuffle=False)


# ### <span style=&#039;color:DarkBlue&#039;>1.3 Run and compare the Model Performance</span>

# #### Comparing models

# In[23]:


compare_models()
# Explore more parameters


# ### <span style=&#039;color:DarkBlue&#039;>1.4 Plot the Best Model</span>

# ##### Plot Residuals

# In[24]:


from pycaret.regression import *
reg = setup(data = boston_df, target=&#039;traffic_volume&#039;,fold = 15,data_split_shuffle=False, normalize = True, normalize_method = &#039;zscore&#039;)
compare_models()


# ##### Plot Error (Scatter Plot)

# In[ ]:


from pycaret.regression import *
reg = setup(data = boston_df, target=&#039;traffic_volume&#039;,fold = 15,data_split_shuffle=False, normalize = True, normalize_method = &#039;zscore&#039;,transformation = True, transformation_method = &#039;yeo-johnson&#039;)
compare_models()


# ##### Plot Learning Curve

# In[26]:


from pycaret.regression import *
reg = setup(data = boston_df, target=&#039;traffic_volume&#039;,fold = 15,data_split_shuffle=False, normalize = True, normalize_method = &#039;zscore&#039;, remove_outliers = True, outliers_threshold = 0.1)
compare_models()


# ##### Plot Validation Curve

# In[27]:


from pycaret.regression import *
reg = setup(data = boston_df, target=&#039;traffic_volume&#039;,fold = 15,data_split_shuffle=False, normalize = True, pca = True, pca_method = &#039;linear&#039;, remove_outliers = True, outliers_threshold = 0.15)
compare_models()

Add a code snippet to your website: www.paste.org