# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
# **Libraries**
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import randint

# Model Selection
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RandomizedSearchCV

# Logistic Regression Model
from sklearn.linear_model import LogisticRegression

# Random Forest Model
from sklearn.ensemble import RandomForestClassifier

# Evaluate Model
from sklearn.metrics import roc_curve, roc_auc_score, confusion_matrix, classification_report
"""
# **EDA**
"""
# Loading Data
X_train = pd.read_csv("../input/ecommerce-shipping-data-competition-form/X_train.csv", index_col="ID")
X_test = pd.read_csv("../input/ecommerce-shipping-data-competition-form/X_test.csv", index_col="ID")
y_train = pd.read_csv("../input/ecommerce-shipping-data-competition-form/y_train.csv", index_col="ID")
y_test = pd.read_csv("../input/ecommerce-shipping-data-competition-form/test_label/y_test.csv", index_col="ID")
X_train.head()
X_train.info()
X_train.isna().sum()
print(y_train.value_counts())
print(y_test.value_counts())
for col in X_train.columns :
    print(X_train[col].value_counts())
# Spliting Categorical Variables
X_train_ctg = X_train[
    [
        "Warehouse_block",
        "Mode_of_Shipment",
        "Gender",
    ]
]

# Replace Values
X_train.Product_importance.replace(
    {
        "high":3,
        "medium":2,
        "low":1,
    },
    inplace=True
)
X_train.Customer_care_calls.replace("$7",7, inplace=True)
X_train.Customer_care_calls = pd.to_numeric(X_train.Customer_care_calls)

X_train_others = X_train.drop(columns = [
        "Warehouse_block",
        "Mode_of_Shipment",
        "Gender",
    ]
)
# One-hot Encoding
X_train_ctg = pd.get_dummies(X_train_ctg)

# Concat
X_train = pd.concat([X_train_others,X_train_ctg], axis=1)

X_train.head()
# Spliting Categorical Variables
X_test_ctg = X_test[
    [
        "Warehouse_block",
        "Mode_of_Shipment",
        "Gender",
    ]
]

# Replace Values
X_test.Product_importance.replace(
    {
        "high":3,
        "medium":2,
        "low":1,
    },
    inplace=True
)
X_test.Customer_care_calls.replace("$7",7, inplace=True)
X_test.Customer_care_calls = pd.to_numeric(X_test.Customer_care_calls)

X_test_others = X_test.drop(columns = [
        "Warehouse_block",
        "Mode_of_Shipment",
        "Gender",
    ]
)
# One-hot Encoding
X_test_ctg = pd.get_dummies(X_test_ctg)

# Concat
X_test = pd.concat([X_test_others,X_test_ctg], axis=1)

X_train.info()
X_train.describe()
fig, axes = plt.subplots(1,2,figsize=(12,5))
X_train.boxplot(column="Discount_offered", ax=axes[0])
X_train[np.ravel(y_train==0)].hist(column="Discount_offered", bins=30, ax=axes[1], color='r', alpha=0.5)
X_train[np.ravel(y_train==1)].hist(column="Discount_offered", bins=30, ax=axes[1], color='b', alpha=0.5)
plt.show()
plt.figure(figsize=(20,20))
for num, col in enumerate(X_train.columns):
    plt.subplot(4,5, num+1)
    plt.hist(X_train[col][np.ravel(y_train==0)], color="blue", alpha=0.3, label=0)
    plt.hist(X_train[col][np.ravel(y_train==1)], color="red", alpha=0.3, label=1)
    plt.legend()
    plt.title(col)
plt.show()
"""
# **Logistic Regression**
"""
logistic = LogisticRegression(C=2, max_iter=1000)
model = logistic.fit(X_train, np.ravel(y_train))
pred_train = model.predict(X_train)
pred_test = model.predict(X_test)

# Model Metrics
print(f"Test data R^2 Score: {model.score(X_test, np.ravel(y_test)):.3f}")
print(confusion_matrix(y_test, pred_test))
print(classification_report(y_test, pred_test))
print(f"ROC AUC Score: {roc_auc_score(y_test, model.decision_function(X_test)):.3f}")
"""
# **Random Forest**
"""
randomforest = RandomForestClassifier(random_state=100, n_jobs=-1)
model = randomforest.fit(X_train, np.ravel(y_train))
pred_test = model.predict(X_test)
pred_test_proba = model.predict_proba(X_test)
print(f"R^2 Score : {model.score(X_test, np.ravel(y_test)):.3f}")
print(classification_report(y_test, pred_test))
print(f"ROC AUC Score : {roc_auc_score(y_test, pred_test_proba[:,1]):.3f}")
"""
# **Submission**
"""
submission = pd.DataFrame( 
    pred_test_proba[:,1],
    index = X_test.index,
    columns = ["Reached.on.Time_Y.N"]
)
submission.to_csv("result.csv")
submission.head()