# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Introduction to PyCaret - An open source low-code ML library
#
# ## This notebook consists 2 parts
#  - Classification part using Titanic DataSet
#  - Regression part using House Price Regression DataSet

# ![](https://pycaret.org/wp-content/uploads/2020/03/Divi93_43.png)
#
# You can reach pycaret website and documentation from https://pycaret.org
#
# PyCaret is an open source, low-code machine learning library in Python that allows you to go from preparing your data to deploying your model within seconds in your choice of notebook environment.
#
# PyCaret being a low-code library makes you more productive. With less time spent coding, you and your team can now focus on business problems.
#
# PyCaret is simple and easy to use machine learning library that will help you to perform end-to-end ML experiments with less lines of code. 
#
# PyCaret is a business ready solution. It allows you to do prototyping quickly and efficiently from your choice of notebook environment.
#

# # let's install pycaret ! 

# + _kg_hide-output=true
# !pip install pycaret
# -

# # Part 1 Classification
#
# ![](https://www.sciencealert.com/images/articles/processed/titanic-1_1024.jpg)

# # We start by loading the libraries

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5"
import numpy as np 
import pandas as pd 
# -

# # Read our files

# + _cell_guid="79c7e3d0-c299-4dcb-8224-4455121ee9b0" _uuid="d629ff2d2480ee46fbb7e2d37f6b5fab8052498a"
train = pd.read_csv('../input/titanic/train.csv')
test  = pd.read_csv('../input/titanic/test.csv')
sub   = pd.read_csv('../input/titanic/gender_submission.csv')
# -

# # Import whole classification

from pycaret.classification import *

# # let's see what we're dealing with

train.head()

train.info()

# # Set up our dataset (preprocessing)

# +
clf1 = setup(data = train, 
             target = 'Survived',
             numeric_imputation = 'mean',
             categorical_features = ['Sex','Embarked'], 
             ignore_features = ['Name','Ticket','Cabin'],
             silent = True)

#quite intuitive isn't it ?
# -

# # Compare the models

compare_models()

# # let's create a Light GBM Model

lgbm  = create_model('lightgbm')      

# # Let's tune it!

tuned_lightgbm = tune_model('lightgbm')

# # Learning Curve

plot_model(estimator = tuned_lightgbm, plot = 'learning')

# # AUC Curve

plot_model(estimator = tuned_lightgbm, plot = 'auc')

# # Confusion Matrix

plot_model(estimator = tuned_lightgbm, plot = 'confusion_matrix')

# # Feature Importance

plot_model(estimator = tuned_lightgbm, plot = 'feature')

# # whole thing!

evaluate_model(tuned_lightgbm)

# # Interpretation

interpret_model(tuned_lightgbm)

# # Predictions

predict_model(tuned_lightgbm, data=test)

predictions = predict_model(tuned_lightgbm, data=test)
predictions.head()

sub['Survived'] = round(predictions['Score']).astype(int)
sub.to_csv('submission.csv',index=False)
sub.head()

# # Extra: Blending made easy!

# +
logr  = create_model('lr');      
xgb   = create_model('xgboost');            

#blending 3 models
blend = blend_models(estimator_list=[tuned_lightgbm,logr,xgb])
# -

# # Part2 - Regression

# ![](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSYeyNpaoAW-3rFX9-ORmiJ-uLAAswYBRhszs2QzllV7MCfFPvk&usqp=CAU)

# # Import Whole Regression

from pycaret.regression import *

# # let's see the data

train = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')
test  = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')
sample= pd.read_csv('../input/house-prices-advanced-regression-techniques/sample_submission.csv')

train.head()

train.info()

# # Set up our dataset (preprocessing)

reg = setup(data = train, 
             target = 'SalePrice',
             numeric_imputation = 'mean',
             categorical_features = ['MSZoning','Exterior1st','Exterior2nd','KitchenQual','Functional','SaleType',
                                     'Street','LotShape','LandContour','LotConfig','LandSlope','Neighborhood',   
                                     'Condition1','Condition2','BldgType','HouseStyle','RoofStyle','RoofMatl',    
                                     'MasVnrType','ExterQual','ExterCond','Foundation','BsmtQual','BsmtCond',   
                                     'BsmtExposure','BsmtFinType1','BsmtFinType2','Heating','HeatingQC','CentralAir',   
                                     'Electrical','GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive',
                                     'SaleCondition']  , 
             ignore_features = ['Alley','PoolQC','MiscFeature','Fence','FireplaceQu','Utilities'],
             normalize = True,
             silent = True)

# # let's compare different regression models!

compare_models()

# # let's do CatBoost

cb = create_model('catboost')

# # gotta tune it

tuned_cb = tune_model('catboost')

# # SHAP Values (impact on model output)

interpret_model(tuned_cb)

predictions = predict_model(tuned_cb, data = test)
sample['SalePrice'] = predictions['Label']
sample.to_csv('submission_house_price.csv',index=False)
sample.head()

# # thank you very much for checking my notebook!

StarcoderdataJupyterScriptsDedupFiltered