"""
# **Firstly, let's import all necessary libraries**
"""
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import random
from scipy import stats
from sklearn.preprocessing import OrdinalEncoder
from sklearn.metrics import mean_squared_error,roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold,StratifiedKFold
from xgboost import XGBRegressor
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from sklearn import preprocessing
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
"""
# **Reading dataset as train, test and submit**
"""
train = pd.read_csv('../input/30-days-of-ml/train.csv')
test  = pd.read_csv('../input/30-days-of-ml/test.csv')
submit = pd.read_csv('../input/30-days-of-ml/sample_submission.csv')
"""
# **Let's observe our data**
"""
train.head()
train.tail()
test.head()
test.tail()
train.info()
test.info()
train.isnull().sum()
test.isnull().sum()
"""
As you see code below (*train.isnull().sum()*), we don't have any null value.
"""
train.describe()
categorical_cols=['cat'+str(i) for i in range(10)]
continous_cols=['cont'+str(i) for i in range(14)]
print(categorical_cols)
print(32*"----") 
print(continous_cols)
"""
# Data Visualization
"""
"""
## Distribution of Continuous Features  
"""
i = 1
plt.figure()
fig, ax = plt.subplots(7, 2,figsize=(20, 24))
for feature in continous_cols:
    plt.subplot(7, 2,i)
    sns.histplot(train[feature],color="blue", kde=True,bins=100, label='train')
    sns.histplot(test[feature],color="olive", kde=True,bins=100, label='test')
    plt.xlabel(feature, fontsize=9); plt.legend()
    i += 1
"""
## Distribution of Categorical Features  
"""
i = 1
plt.figure()
fig, ax = plt.subplots(5, 2,figsize=(28, 44))
for feature in categorical_cols:
    plt.subplot(5, 2,i)
    sns.histplot(train[feature],color="blue", label='train')
    sns.histplot(test[feature],color="olive", label='test')
    plt.xlabel(feature, fontsize=9); plt.legend()
    i += 1
plt.show()
"""
## Heat Map
"""
corr = train[continous_cols+['target']].corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(2)
"""
# Data Encoding
"""
train.drop("id", axis=1, inplace=True)
test.drop("id", axis=1, inplace=True)
x = train.drop(['target'], axis=1)
y = train['target']
X_test = test.copy()
ordinal_encoder = OrdinalEncoder()
x[categorical_cols] = ordinal_encoder.fit_transform(x[categorical_cols])
X_test[categorical_cols] = ordinal_encoder.transform(X_test[categorical_cols])
x.head()
# train test split
from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(x, y,test_size=0.2, random_state=50)
"""
# Application of Xgboost 
"""
xgb_params = {'objective': 'reg:squarederror',
              'n_estimators': 10000,
              'learning_rate': 0.036,
              'subsample': 0.926,
              'colsample_bytree': 0.118,
              'grow_policy':'lossguide',
              'max_depth': 3,
              'booster': 'gbtree', 
              'reg_lambda': 45.1,
              'reg_alpha': 34.9,
              'random_state': 42,
              'reg_lambda': 0.00087,
              'reg_alpha': 23.132}

model_XGB = XGBRegressor(**xgb_params)
model_XGB.fit(X_train, y_train) 
pred_XGB = model_XGB.predict(X_valid)
print(mean_squared_error(y_valid, pred_XGB, squared=False))
"""
Parameters were taken from https://www.kaggle.com/miladagdam/30-days-of-ml-xgboost
"""
"""
# Let's submit to the competition
"""
pred = model_XGB.predict(X_test)
# Save the predictions to a CSV file
submit['target']=pred
submit.to_csv('submission.csv', index=False)