text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from sklearn.impute import KNNImputer

from sklearn.impute import SimpleImputer

from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix, classification_report

from sklearn.metrics import roc_auc_score

from sklearn.preprocessing import OneHotEncoder

from sklearn.tree import DecisionTreeClassifier

from sklearn.tree import DecisionTreeClassifier

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

train = pd.read_csv('/kaggle/input/e-commerce-shoppers-behaviour-understanding/train_data_v2.csv')
test = pd.read_csv('/kaggle/input/e-commerce-shoppers-behaviour-understanding/test_data_v2.csv')

train.shape

train.columns

train.isna().sum()

var = train.corr()

target = train['Made_Purchase']
features = train.drop('Made_Purchase', axis=1)

col_float = []
col_str = []
for i in features.columns:
    if features[i].dtype == 'float64':
        col_float.append(i)
    else:
        col_str.append(i)

from sklearn.impute import SimpleImputer
si = SimpleImputer(strategy='most_frequent')
features[col_str] = si.fit_transform(features[col_str])
features.isna().sum()

from sklearn.impute import KNNImputer
knn = KNNImputer(n_neighbors=7, weights='distance')
features[col_float] = knn.fit_transform(features[col_float])
features.isna().sum()

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False)
encoded = encoder.fit_transform(features[col_str])
new_features = pd.DataFrame(encoded)

from sklearn.metrics import roc_auc_score
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix, classification_report
def get_metrics(classifier, Xvalid, yvalid):
    """Function to Get the metrics of the given Classifier"""
    y_train_pred = classifier.predict_proba(X_train)[:, 1]
    y_valid_pred = classifier.predict_proba(Xvalid)[:, 1]
    y_valid_predict = classifier.predict(Xvalid)
    cm = confusion_matrix(yvalid, y_valid_predict)
    dist = ConfusionMatrixDisplay(cm)

from sklearn.tree import DecisionTreeClassifier
dc = DecisionTreeClassifier()
dc.fit(X_train, y_train)
get_metrics(dc, X_valid, y_valid)

from sklearn.tree import DecisionTreeClassifier
dc = DecisionTreeClassifier(max_depth=4, criterion='gini', min_samples_split=2, min_samples_leaf=4)
dc.fit(X_train, y_train)
get_metrics(dc, X_valid, y_valid)

test[col_str] = si.transform(test[col_str])
test[col_float] = knn.transform(test[col_float])
test_encoded = pd.DataFrame(encoder.fit_transform(test[col_str]))
new_df_test = pd.DataFrame(test_encoded)
new_df_test[col_float] = test[col_float]

prediction = dc.predict(new_df_test)
prediction