"""
![](https://i1.wp.com/pycaret.org/wp-content/uploads/2020/04/thumbnail.png?fit=1166%2C656&ssl=1)
"""
"""
⚙️ Install PyCaret & Import Libraries
Kaggle notebooks do not provide pycaret by default. So, you can install it with the following command :

> !pip install pycaret
"""
! pip install pycaret
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

from pycaret.regression import setup, compare_models, blend_models, finalize_model, predict_model
train = pd.read_csv(r'../input/30-days-of-ml/train.csv')
test = pd.read_csv(r'../input/30-days-of-ml/test.csv')
submission = pd.read_csv(r'../input/30-days-of-ml/sample_submission.csv')
train.shape, test.shape, submission.shape
train.head()
train.drop('id',axis=1,inplace=True)
test.drop('id',axis=1,inplace=True)
train.nunique()
"""
### **Data Visualization**

"""
plt.figure(figsize=(15,5))
sns.kdeplot(train['target'], shade = True, alpha = 0.9, linewidth = 1.5, facecolor=(1, 1, 1, 0), edgecolor=".2")
plt.title("Target", fontdict={'fontsize':20})
plt.show()
# plot the boxplot of area distribution
plt.figure(figsize=(15,5))
sns.boxplot(train.target,color = 'white',linewidth=2.5)
plt.title('target Distribution')
plt.xlabel('target')
plt.show()
"""
#### **Categorical features**
"""
fig, axes =plt.subplots(5,2, figsize=(15,15), sharex=True)
axes = axes.flatten()
object_bol = train.dtypes == 'object'
for ax, catplot in zip(axes, train.dtypes[object_bol].index):
    sns.countplot(y=catplot, data=train, ax=ax)
plt.tight_layout()     
plt.show()
"""
#### **Continuous Features**
"""
fig = plt.figure(figsize = (15, 60))
for i in range(len(train.columns.tolist()[10:24])):
    plt.subplot(20,5,i+1)
    sns.set_style("white")
    plt.title(train.columns.tolist()[10:24][i], size = 12, fontname = 'monospace')
    a = sns.kdeplot(train[train.columns.tolist()[10:24][i]], shade = True, alpha = 0.9, linewidth = 1.5, facecolor=(1, 1, 1, 0), edgecolor=".2")
    plt.ylabel('')
    plt.xlabel('')
    plt.xticks(fontname = 'monospace')
    plt.yticks([])
    for j in ['right', 'left', 'top']:
        a.spines[j].set_visible(False)
        a.spines['bottom'].set_linewidth(1.2)
        
fig.tight_layout(h_pad = 3)
plt.show()
fig = plt.figure(figsize = (15, 60))
for i in range(len(train.columns.tolist()[10:24])):
    plt.subplot(20,5,i+1)
    sns.set_style("white")
    plt.title(train.columns.tolist()[10:24][i], size = 12, fontname = 'monospace')
    a = sns.boxplot(train[train.columns.tolist()[10:24][i]], linewidth = 2.5,color = 'white')
    plt.ylabel('')
    plt.xlabel('')
    plt.xticks(fontname = 'monospace')
    plt.yticks([])
    for j in ['right', 'left', 'top']:
        a.spines[j].set_visible(False)
        a.spines['bottom'].set_linewidth(1.2)
        
fig.tight_layout(h_pad = 3)
plt.show()

print('train continuous features: ')
train.describe().T.style.bar(subset=['mean'], color='#606ff2')\
                            .background_gradient(subset=['std'], cmap='PuBu')\
                            .background_gradient(subset=['50%'], cmap='PuBu')
print('test continuous features: ')
test.describe().T.style.bar(subset=['mean'], color='#606ff2')\
                            .background_gradient(subset=['std'], cmap='PuBu')\
                            .background_gradient(subset=['50%'], cmap='PuBu')
"""
### **Data preprocessinng**
"""
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
cat_cols = ['cat0','cat1','cat2','cat3','cat4','cat5','cat6','cat7','cat8','cat9']
for i in cat_cols:
    train[i] = le.fit_transform(train[i])
    test[i] = le.transform(test[i])
from sklearn.preprocessing import MinMaxScaler
ss = MinMaxScaler()
cont_cols = ['cont0','cont1','cont2','cont3','cont4','cont5','cont6','cont7','cont8','cont9','cont10','cont11','cont12','cont13']
for i in cont_cols:
    train[i] = ss.fit_transform(np.array(train[i]).reshape(-1,1))
    test[i] = ss.transform(np.array(test[i]).reshape(-1,1))
"""
#### **PyCaret**
"""
def pycaret_model(train, target,test, n_select, fold, opt):
    print('Setup Your Data....')
    setup(data=train,
          target=target,
          silent= True,
         use_gpu = True)
  
    print('Comparing Models....')
    best = compare_models(sort=opt, n_select=n_select, fold = fold,include=['xgboost','lightgbm','catboost'])
    
    
    print('Blending Models....')
    blended = blend_models(estimator_list= best, fold=fold, optimize=opt)
    pred = predict_model(blended)
    
    
    print('Finallizing Models....')
    final_model = finalize_model(blended)
    print('Done...!!!')

    pred_test = predict_model(final_model, test)
    re = pred_test['Label']

    return re

submission['target'] = pycaret_model(train,'target',test, 2, 10, 'RMSE')
submission.to_csv('submission.csv',index=False)