{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3415114e-9577-4487-89eb-4931620ad9f0",
   "metadata": {},
   "source": [
    "# Predict Sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f271eb45-1470-4764-8c2e-31374efa1fe5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import s3fs\n",
    "import re\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, recall_score\n",
    "from sklearn.utils import class_weight\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.compose import ColumnTransformer\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler, MaxAbsScaler, MinMaxScaler\n",
    "from sklearn.metrics import make_scorer, f1_score, balanced_accuracy_score\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import roc_curve, auc, precision_recall_curve, average_precision_score\n",
    "from sklearn.exceptions import ConvergenceWarning, DataConversionWarning\n",
    "\n",
    "import pickle\n",
    "import warnings\n",
    "#import scikitplot as skplt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3fecb606-22e5-4dee-8efa-f8dff0832299",
   "metadata": {},
   "outputs": [],
   "source": [
    "warnings.filterwarnings('ignore')\n",
    "warnings.filterwarnings(\"ignore\", category=ConvergenceWarning)\n",
    "warnings.filterwarnings(\"ignore\", category=DataConversionWarning)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae591854-3003-4c75-a0c7-5abf04246e81",
   "metadata": {},
   "source": [
    "### Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59dd4694-a812-4923-b995-a2ee86c74f85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create filesystem object\n",
    "S3_ENDPOINT_URL = \"https://\" + os.environ[\"AWS_S3_ENDPOINT\"]\n",
    "fs = s3fs.S3FileSystem(client_kwargs={'endpoint_url': S3_ENDPOINT_URL})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "017f7e9a-3ba0-40fa-bdc8-51b98cc1fdb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_train_test():\n",
    "    BUCKET = \"projet-bdc2324-team1/Generalization/sport\"\n",
    "    File_path_train = BUCKET + \"/Train_set.csv\"\n",
    "    File_path_test = BUCKET + \"/Test_set.csv\"\n",
    "    \n",
    "    with fs.open( File_path_train, mode=\"rb\") as file_in:\n",
    "        dataset_train = pd.read_csv(file_in, sep=\",\")\n",
    "        # dataset_train['y_has_purchased'] = dataset_train['y_has_purchased'].fillna(0)\n",
    "\n",
    "    with fs.open(File_path_test, mode=\"rb\") as file_in:\n",
    "        dataset_test = pd.read_csv(file_in, sep=\",\")\n",
    "        # dataset_test['y_has_purchased'] = dataset_test['y_has_purchased'].fillna(0)\n",
    "    \n",
    "    return dataset_train, dataset_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c479b230-b4bd-4cfb-b76b-d9faf6d95772",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_train, dataset_test = load_train_test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c24c446d-4e1c-4ac1-a048-f0b8d8559f36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customer_id                    0\n",
       "nb_tickets                     0\n",
       "nb_purchases                   0\n",
       "total_amount                   0\n",
       "nb_suppliers                   0\n",
       "vente_internet_max             0\n",
       "purchase_date_min              0\n",
       "purchase_date_max              0\n",
       "time_between_purchase          0\n",
       "nb_tickets_internet            0\n",
       "street_id                      0\n",
       "structure_id              222825\n",
       "mcp_contact_id             70874\n",
       "fidelity                       0\n",
       "tenant_id                      0\n",
       "is_partner                     0\n",
       "deleted_at                224213\n",
       "gender                         0\n",
       "is_email_true                  0\n",
       "opt_in                         0\n",
       "last_buying_date           66139\n",
       "max_price                  66139\n",
       "ticket_sum                     0\n",
       "average_price              66023\n",
       "average_purchase_delay     66139\n",
       "average_price_basket       66139\n",
       "average_ticket_basket      66139\n",
       "total_price                  116\n",
       "purchase_count                 0\n",
       "first_buying_date          66139\n",
       "country                    23159\n",
       "gender_label                   0\n",
       "gender_female                  0\n",
       "gender_male                    0\n",
       "gender_other                   0\n",
       "country_fr                 23159\n",
       "nb_campaigns                   0\n",
       "nb_campaigns_opened            0\n",
       "time_to_open              123159\n",
       "y_has_purchased                0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "825d14a3-6967-4733-bfd4-64bf61c2bd43",
   "metadata": {},
   "outputs": [],
   "source": [
    "def features_target_split(dataset_train, dataset_test):\n",
    "    features_l = ['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'vente_internet_max', 'purchase_date_min', 'purchase_date_max', \n",
    "            'time_between_purchase', 'nb_tickets_internet', 'fidelity',  'is_email_true', 'opt_in', #'is_partner',\n",
    "            'gender_female', 'gender_male', 'gender_other', 'nb_campaigns', 'nb_campaigns_opened']\n",
    "    X_train = dataset_train[features_l]\n",
    "    y_train = dataset_train[['y_has_purchased']]\n",
    "\n",
    "    X_test = dataset_test[features_l]\n",
    "    y_test = dataset_test[['y_has_purchased']]\n",
    "    return X_train, X_test, y_train, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "69eaec12-b30f-4d30-a461-ea520d5cbf77",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = features_target_split(dataset_train, dataset_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d039f31d-0093-46c6-9743-ddec1381f758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape train :  (224213, 17)\n",
      "Shape test :  (96096, 17)\n"
     ]
    }
   ],
   "source": [
    "print(\"Shape train : \", X_train.shape)\n",
    "print(\"Shape test : \", X_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1d6de94-4e11-481a-a0ce-412bf29f692c",
   "metadata": {},
   "source": [
    "### Prepare preprocessing and Hyperparameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b808da43-c444-4e94-995a-7ec6ccd01e2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0.0: 0.5837086520288036, 1.0: 3.486549107420539}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Compute Weights\n",
    "weights = class_weight.compute_class_weight(class_weight = 'balanced', classes = np.unique(y_train['y_has_purchased']),\n",
    "                                            y = y_train['y_has_purchased'])\n",
    "\n",
    "weight_dict = {np.unique(y_train['y_has_purchased'])[i]: weights[i] for i in range(len(np.unique(y_train['y_has_purchased'])))}\n",
    "weight_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b32a79ea-907f-4dfc-9832-6c74bef3200c",
   "metadata": {},
   "outputs": [],
   "source": [
    "numeric_features = ['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'vente_internet_max', 'purchase_date_min', 'purchase_date_max', \n",
    "            'time_between_purchase', 'nb_tickets_internet', 'fidelity',  'is_email_true', 'opt_in', #'is_partner',\n",
    "            'gender_female', 'gender_male', 'gender_other', 'nb_campaigns', 'nb_campaigns_opened']\n",
    "\n",
    "numeric_transformer = Pipeline(steps=[\n",
    "    #(\"imputer\", SimpleImputer(strategy=\"mean\")),  \n",
    "    (\"scaler\", StandardScaler()) \n",
    "])\n",
    "\n",
    "categorical_features = ['opt_in']  \n",
    "\n",
    "# Transformer for the categorical features\n",
    "categorical_transformer = Pipeline(steps=[\n",
    "    #(\"imputer\", SimpleImputer(strategy=\"most_frequent\")),  # Impute missing values with the most frequent\n",
    "    (\"onehot\", OneHotEncoder(handle_unknown='ignore', sparse_output=False))\n",
    "])\n",
    "\n",
    "preproc = ColumnTransformer(\n",
    "    transformers=[\n",
    "        (\"num\", numeric_transformer, numeric_features),\n",
    "        (\"cat\", categorical_transformer, categorical_features)\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9809a688-bfbc-4685-a77f-17a8b2b79ab3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set loss\n",
    "balanced_scorer = make_scorer(balanced_accuracy_score)\n",
    "recall_scorer = make_scorer(recall_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4f9b2bbf-5f8a-4ac1-8e6c-51bd0dd8ac85",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_confusion_matrix(y_test, y_pred):\n",
    "    conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "    sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['Class 0', 'Class 1'], yticklabels=['Class 0', 'Class 1'])\n",
    "    plt.xlabel('Predicted')\n",
    "    plt.ylabel('Actual')\n",
    "    plt.title('Confusion Matrix')\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "def draw_roc_curve(X_test, y_test):\n",
    "    y_pred_prob = pipeline.predict_proba(X_test)[:, 1]\n",
    "\n",
    "    # Calcul des taux de faux positifs (FPR) et de vrais positifs (TPR)\n",
    "    fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob, pos_label=1)\n",
    "    \n",
    "    # Calcul de l'aire sous la courbe ROC (AUC)\n",
    "    roc_auc = auc(fpr, tpr)\n",
    "    \n",
    "    plt.figure(figsize = (14, 8))\n",
    "    plt.plot(fpr, tpr, label=\"ROC curve(area = %0.3f)\" % roc_auc)\n",
    "    plt.plot([0, 1], [0, 1], color=\"red\",label=\"Random Baseline\", linestyle=\"--\")\n",
    "    plt.grid(color='gray', linestyle='--', linewidth=0.5)\n",
    "    plt.xlabel('Taux de faux positifs (FPR)')\n",
    "    plt.ylabel('Taux de vrais positifs (TPR)')\n",
    "    plt.title('Courbe ROC : modèle logistique')\n",
    "    plt.legend(loc=\"lower right\")\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cf400c70-0192-42cc-9919-f61bae8382b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_features_importance(pipeline, model):\n",
    "    coefficients = pipeline.named_steps['logreg'].coef_[0]\n",
    "    feature_names = pipeline.named_steps['logreg'].feature_names_in_\n",
    "    \n",
    "    # Tracer l'importance des caractéristiques\n",
    "    plt.figure(figsize=(10, 6))\n",
    "    plt.barh(feature_names, coefficients, color='skyblue')\n",
    "    plt.xlabel('Importance des caractéristiques')\n",
    "    plt.ylabel('Caractéristiques')\n",
    "    plt.title('Importance des caractéristiques dans le modèle de régression logistique')\n",
    "    plt.grid(True)\n",
    "    plt.show()\n",
    "\n",
    "def draw_prob_distribution(X_test):\n",
    "    y_pred_prob = pipeline.predict_proba(X_test)[:, 1]\n",
    "    plt.figure(figsize=(8, 6))\n",
    "    plt.hist(y_pred_prob, bins=10, range=(0, 1), color='blue', alpha=0.7)\n",
    "    \n",
    "    plt.xlim(0, 1)\n",
    "    plt.ylim(0, None)\n",
    "    \n",
    "    plt.title('Histogramme des probabilités pour la classe 1')\n",
    "    plt.xlabel('Probabilité')\n",
    "    plt.ylabel('Fréquence')\n",
    "    plt.grid(True)\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "206d9a95-7c37-4506-949b-e77d225e42c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Hyperparameter\n",
    "param_grid = {'logreg__C': np.logspace(-10, 6, 17, base=2),\n",
    "              'logreg__penalty': ['l1', 'l2'],\n",
    "               'logreg__class_weight': ['balanced', weight_dict]} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7ff2f7bd-efc1-4f7c-a3c9-caa916aa2f2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-1 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-1 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-1 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-1 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-1 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Pipeline(steps=[(&#x27;preprocessor&#x27;,\n",
       "                 ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;scaler&#x27;,\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;,\n",
       "                                                   &#x27;total_amount&#x27;,\n",
       "                                                   &#x27;nb_suppliers&#x27;,\n",
       "                                                   &#x27;vente_internet_max&#x27;,\n",
       "                                                   &#x27;purchase_date_min&#x27;,\n",
       "                                                   &#x27;purchase_date_max&#x27;,\n",
       "                                                   &#x27;time_between_purchase&#x27;,\n",
       "                                                   &#x27;nb_tickets_internet&#x27;,\n",
       "                                                   &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;,\n",
       "                                                   &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                                   &#x27;gender_male&#x27;,\n",
       "                                                   &#x27;gender_other&#x27;,\n",
       "                                                   &#x27;nb_campaigns&#x27;,\n",
       "                                                   &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                                 (&#x27;cat&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                                   OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  [&#x27;opt_in&#x27;])])),\n",
       "                (&#x27;logreg&#x27;,\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver=&#x27;saga&#x27;))])</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;&nbsp;Pipeline<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.pipeline.Pipeline.html\">?<span>Documentation for Pipeline</span></a><span class=\"sk-estimator-doc-link \">i<span>Not fitted</span></span></label><div class=\"sk-toggleable__content \"><pre>Pipeline(steps=[(&#x27;preprocessor&#x27;,\n",
       "                 ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;scaler&#x27;,\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;,\n",
       "                                                   &#x27;total_amount&#x27;,\n",
       "                                                   &#x27;nb_suppliers&#x27;,\n",
       "                                                   &#x27;vente_internet_max&#x27;,\n",
       "                                                   &#x27;purchase_date_min&#x27;,\n",
       "                                                   &#x27;purchase_date_max&#x27;,\n",
       "                                                   &#x27;time_between_purchase&#x27;,\n",
       "                                                   &#x27;nb_tickets_internet&#x27;,\n",
       "                                                   &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;,\n",
       "                                                   &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                                   &#x27;gender_male&#x27;,\n",
       "                                                   &#x27;gender_other&#x27;,\n",
       "                                                   &#x27;nb_campaigns&#x27;,\n",
       "                                                   &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                                 (&#x27;cat&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                                   OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  [&#x27;opt_in&#x27;])])),\n",
       "                (&#x27;logreg&#x27;,\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver=&#x27;saga&#x27;))])</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;preprocessor: ColumnTransformer<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.compose.ColumnTransformer.html\">?<span>Documentation for preprocessor: ColumnTransformer</span></a></label><div class=\"sk-toggleable__content \"><pre>ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                 Pipeline(steps=[(&#x27;scaler&#x27;, StandardScaler())]),\n",
       "                                 [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;, &#x27;total_amount&#x27;,\n",
       "                                  &#x27;nb_suppliers&#x27;, &#x27;vente_internet_max&#x27;,\n",
       "                                  &#x27;purchase_date_min&#x27;, &#x27;purchase_date_max&#x27;,\n",
       "                                  &#x27;time_between_purchase&#x27;,\n",
       "                                  &#x27;nb_tickets_internet&#x27;, &#x27;fidelity&#x27;,\n",
       "                                  &#x27;is_email_true&#x27;, &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                  &#x27;gender_male&#x27;, &#x27;gender_other&#x27;, &#x27;nb_campaigns&#x27;,\n",
       "                                  &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                (&#x27;cat&#x27;,\n",
       "                                 Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                  OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                sparse_output=False))]),\n",
       "                                 [&#x27;opt_in&#x27;])])</pre></div> </div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">num</label><div class=\"sk-toggleable__content \"><pre>[&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;, &#x27;total_amount&#x27;, &#x27;nb_suppliers&#x27;, &#x27;vente_internet_max&#x27;, &#x27;purchase_date_min&#x27;, &#x27;purchase_date_max&#x27;, &#x27;time_between_purchase&#x27;, &#x27;nb_tickets_internet&#x27;, &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;, &#x27;opt_in&#x27;, &#x27;gender_female&#x27;, &#x27;gender_male&#x27;, &#x27;gender_other&#x27;, &#x27;nb_campaigns&#x27;, &#x27;nb_campaigns_opened&#x27;]</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" ><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;StandardScaler<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.preprocessing.StandardScaler.html\">?<span>Documentation for StandardScaler</span></a></label><div class=\"sk-toggleable__content \"><pre>StandardScaler()</pre></div> </div></div></div></div></div></div></div><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-5\" type=\"checkbox\" ><label for=\"sk-estimator-id-5\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">cat</label><div class=\"sk-toggleable__content \"><pre>[&#x27;opt_in&#x27;]</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" ><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;OneHotEncoder<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.preprocessing.OneHotEncoder.html\">?<span>Documentation for OneHotEncoder</span></a></label><div class=\"sk-toggleable__content \"><pre>OneHotEncoder(handle_unknown=&#x27;ignore&#x27;, sparse_output=False)</pre></div> </div></div></div></div></div></div></div></div></div><div class=\"sk-item\"><div class=\"sk-estimator  sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-7\" type=\"checkbox\" ><label for=\"sk-estimator-id-7\" class=\"sk-toggleable__label  sk-toggleable__label-arrow \">&nbsp;LogisticRegression<a class=\"sk-estimator-doc-link \" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LogisticRegression.html\">?<span>Documentation for LogisticRegression</span></a></label><div class=\"sk-toggleable__content \"><pre>LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                 1.0: 3.486549107420539},\n",
       "                   max_iter=5000, solver=&#x27;saga&#x27;)</pre></div> </div></div></div></div></div></div>"
      ],
      "text/plain": [
       "Pipeline(steps=[('preprocessor',\n",
       "                 ColumnTransformer(transformers=[('num',\n",
       "                                                  Pipeline(steps=[('scaler',\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  ['nb_tickets', 'nb_purchases',\n",
       "                                                   'total_amount',\n",
       "                                                   'nb_suppliers',\n",
       "                                                   'vente_internet_max',\n",
       "                                                   'purchase_date_min',\n",
       "                                                   'purchase_date_max',\n",
       "                                                   'time_between_purchase',\n",
       "                                                   'nb_tickets_internet',\n",
       "                                                   'fidelity', 'is_email_true',\n",
       "                                                   'opt_in', 'gender_female',\n",
       "                                                   'gender_male',\n",
       "                                                   'gender_other',\n",
       "                                                   'nb_campaigns',\n",
       "                                                   'nb_campaigns_opened']),\n",
       "                                                 ('cat',\n",
       "                                                  Pipeline(steps=[('onehot',\n",
       "                                                                   OneHotEncoder(handle_unknown='ignore',\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  ['opt_in'])])),\n",
       "                ('logreg',\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver='saga'))])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Pipeline\n",
    "pipeline = Pipeline(steps=[\n",
    "    ('preprocessor', preproc),\n",
    "    ('logreg', LogisticRegression(solver='saga', class_weight = weight_dict,\n",
    "                                  max_iter=5000))  \n",
    "])\n",
    "\n",
    "pipeline.set_output(transform=\"pandas\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed415f60-9663-4179-877b-233faf6e1645",
   "metadata": {},
   "source": [
    "## Baseline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2b467511-2ae5-4a16-a502-397c3460471d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-2 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: black;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-2 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-2 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-2 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-2 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-2 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-2 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-2 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-2 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 1ex;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-2 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-2 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Pipeline(steps=[(&#x27;preprocessor&#x27;,\n",
       "                 ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;scaler&#x27;,\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;,\n",
       "                                                   &#x27;total_amount&#x27;,\n",
       "                                                   &#x27;nb_suppliers&#x27;,\n",
       "                                                   &#x27;vente_internet_max&#x27;,\n",
       "                                                   &#x27;purchase_date_min&#x27;,\n",
       "                                                   &#x27;purchase_date_max&#x27;,\n",
       "                                                   &#x27;time_between_purchase&#x27;,\n",
       "                                                   &#x27;nb_tickets_internet&#x27;,\n",
       "                                                   &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;,\n",
       "                                                   &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                                   &#x27;gender_male&#x27;,\n",
       "                                                   &#x27;gender_other&#x27;,\n",
       "                                                   &#x27;nb_campaigns&#x27;,\n",
       "                                                   &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                                 (&#x27;cat&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                                   OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  [&#x27;opt_in&#x27;])])),\n",
       "                (&#x27;logreg&#x27;,\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver=&#x27;saga&#x27;))])</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-8\" type=\"checkbox\" ><label for=\"sk-estimator-id-8\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;&nbsp;Pipeline<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.pipeline.Pipeline.html\">?<span>Documentation for Pipeline</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>Pipeline(steps=[(&#x27;preprocessor&#x27;,\n",
       "                 ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;scaler&#x27;,\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;,\n",
       "                                                   &#x27;total_amount&#x27;,\n",
       "                                                   &#x27;nb_suppliers&#x27;,\n",
       "                                                   &#x27;vente_internet_max&#x27;,\n",
       "                                                   &#x27;purchase_date_min&#x27;,\n",
       "                                                   &#x27;purchase_date_max&#x27;,\n",
       "                                                   &#x27;time_between_purchase&#x27;,\n",
       "                                                   &#x27;nb_tickets_internet&#x27;,\n",
       "                                                   &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;,\n",
       "                                                   &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                                   &#x27;gender_male&#x27;,\n",
       "                                                   &#x27;gender_other&#x27;,\n",
       "                                                   &#x27;nb_campaigns&#x27;,\n",
       "                                                   &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                                 (&#x27;cat&#x27;,\n",
       "                                                  Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                                   OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  [&#x27;opt_in&#x27;])])),\n",
       "                (&#x27;logreg&#x27;,\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver=&#x27;saga&#x27;))])</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-9\" type=\"checkbox\" ><label for=\"sk-estimator-id-9\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;preprocessor: ColumnTransformer<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.compose.ColumnTransformer.html\">?<span>Documentation for preprocessor: ColumnTransformer</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>ColumnTransformer(transformers=[(&#x27;num&#x27;,\n",
       "                                 Pipeline(steps=[(&#x27;scaler&#x27;, StandardScaler())]),\n",
       "                                 [&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;, &#x27;total_amount&#x27;,\n",
       "                                  &#x27;nb_suppliers&#x27;, &#x27;vente_internet_max&#x27;,\n",
       "                                  &#x27;purchase_date_min&#x27;, &#x27;purchase_date_max&#x27;,\n",
       "                                  &#x27;time_between_purchase&#x27;,\n",
       "                                  &#x27;nb_tickets_internet&#x27;, &#x27;fidelity&#x27;,\n",
       "                                  &#x27;is_email_true&#x27;, &#x27;opt_in&#x27;, &#x27;gender_female&#x27;,\n",
       "                                  &#x27;gender_male&#x27;, &#x27;gender_other&#x27;, &#x27;nb_campaigns&#x27;,\n",
       "                                  &#x27;nb_campaigns_opened&#x27;]),\n",
       "                                (&#x27;cat&#x27;,\n",
       "                                 Pipeline(steps=[(&#x27;onehot&#x27;,\n",
       "                                                  OneHotEncoder(handle_unknown=&#x27;ignore&#x27;,\n",
       "                                                                sparse_output=False))]),\n",
       "                                 [&#x27;opt_in&#x27;])])</pre></div> </div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-10\" type=\"checkbox\" ><label for=\"sk-estimator-id-10\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">num</label><div class=\"sk-toggleable__content fitted\"><pre>[&#x27;nb_tickets&#x27;, &#x27;nb_purchases&#x27;, &#x27;total_amount&#x27;, &#x27;nb_suppliers&#x27;, &#x27;vente_internet_max&#x27;, &#x27;purchase_date_min&#x27;, &#x27;purchase_date_max&#x27;, &#x27;time_between_purchase&#x27;, &#x27;nb_tickets_internet&#x27;, &#x27;fidelity&#x27;, &#x27;is_email_true&#x27;, &#x27;opt_in&#x27;, &#x27;gender_female&#x27;, &#x27;gender_male&#x27;, &#x27;gender_other&#x27;, &#x27;nb_campaigns&#x27;, &#x27;nb_campaigns_opened&#x27;]</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-11\" type=\"checkbox\" ><label for=\"sk-estimator-id-11\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;StandardScaler<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.preprocessing.StandardScaler.html\">?<span>Documentation for StandardScaler</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>StandardScaler()</pre></div> </div></div></div></div></div></div></div><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-12\" type=\"checkbox\" ><label for=\"sk-estimator-id-12\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">cat</label><div class=\"sk-toggleable__content fitted\"><pre>[&#x27;opt_in&#x27;]</pre></div> </div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-13\" type=\"checkbox\" ><label for=\"sk-estimator-id-13\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;OneHotEncoder<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.preprocessing.OneHotEncoder.html\">?<span>Documentation for OneHotEncoder</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>OneHotEncoder(handle_unknown=&#x27;ignore&#x27;, sparse_output=False)</pre></div> </div></div></div></div></div></div></div></div></div><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-14\" type=\"checkbox\" ><label for=\"sk-estimator-id-14\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\">&nbsp;LogisticRegression<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LogisticRegression.html\">?<span>Documentation for LogisticRegression</span></a></label><div class=\"sk-toggleable__content fitted\"><pre>LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                 1.0: 3.486549107420539},\n",
       "                   max_iter=5000, solver=&#x27;saga&#x27;)</pre></div> </div></div></div></div></div></div>"
      ],
      "text/plain": [
       "Pipeline(steps=[('preprocessor',\n",
       "                 ColumnTransformer(transformers=[('num',\n",
       "                                                  Pipeline(steps=[('scaler',\n",
       "                                                                   StandardScaler())]),\n",
       "                                                  ['nb_tickets', 'nb_purchases',\n",
       "                                                   'total_amount',\n",
       "                                                   'nb_suppliers',\n",
       "                                                   'vente_internet_max',\n",
       "                                                   'purchase_date_min',\n",
       "                                                   'purchase_date_max',\n",
       "                                                   'time_between_purchase',\n",
       "                                                   'nb_tickets_internet',\n",
       "                                                   'fidelity', 'is_email_true',\n",
       "                                                   'opt_in', 'gender_female',\n",
       "                                                   'gender_male',\n",
       "                                                   'gender_other',\n",
       "                                                   'nb_campaigns',\n",
       "                                                   'nb_campaigns_opened']),\n",
       "                                                 ('cat',\n",
       "                                                  Pipeline(steps=[('onehot',\n",
       "                                                                   OneHotEncoder(handle_unknown='ignore',\n",
       "                                                                                 sparse_output=False))]),\n",
       "                                                  ['opt_in'])])),\n",
       "                ('logreg',\n",
       "                 LogisticRegression(class_weight={0.0: 0.5837086520288036,\n",
       "                                                  1.0: 3.486549107420539},\n",
       "                                    max_iter=5000, solver='saga'))])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6356e870-0dfc-4e60-9e48-e2de5e7f9f87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy Score: 0.7829358141858141\n",
      "F1 Score: 0.5016842256145632\n",
      "Recall Score: 0.7669831994156319\n"
     ]
    }
   ],
   "source": [
    "y_pred = pipeline.predict(X_test)\n",
    "\n",
    "# Calculate the F1 score\n",
    "acc = accuracy_score(y_test, y_pred)\n",
    "print(f\"Accuracy Score: {acc}\")\n",
    "\n",
    "f1 = f1_score(y_test, y_pred)\n",
    "print(f\"F1 Score: {f1}\")\n",
    "\n",
    "recall = recall_score(y_test, y_pred)\n",
    "print(f\"Recall Score: {recall}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "09387a09-0d53-4c54-baac-f3c2a57a629a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30104 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_confusion_matrix(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "580b58d7-596f-4207-8c99-4365aba2bc9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 95128 characters]",
      "text/plain": [
       "<Figure size 1400x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_roc_curve(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ca5d0a55-adbb-47a0-a4c8-6af9ca75ca9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 86536 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_features_importance(pipeline, 'logreg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f3782ec2-9f2c-4c23-9691-79413c4e04be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31016 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_prob_distribution(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae8e9bd3-0f6a-4f82-bb4c-470cbdc8d6bb",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Cross Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7f0535de-34f1-4e97-b993-b429ecf0a554",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = y_train['y_has_purchased']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f7fca463-d7d6-493b-8329-fdfa92457f78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best parameters found:  {'logreg__C': 0.0009765625, 'logreg__class_weight': 'balanced', 'logreg__penalty': 'l1'}\n",
      "Best cross-validation score: 0.65\n",
      "Test set score: 0.64\n"
     ]
    }
   ],
   "source": [
    "# Cross validation\n",
    "\n",
    "grid_search = GridSearchCV(pipeline, param_grid, cv=3, scoring=recall_scorer, error_score='raise',\n",
    "                          n_jobs=-1)\n",
    "\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "# Print the best parameters and the best score\n",
    "print(\"Best parameters found: \", grid_search.best_params_)\n",
    "print(\"Best cross-validation score: {:.2f}\".format(grid_search.best_score_))\n",
    "\n",
    "# Evaluate the best model on the test set\n",
    "test_score = grid_search.score(X_test, y_test)\n",
    "print(\"Test set score: {:.2f}\".format(test_score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "56bd7828-4de1-4166-bea0-5d5e152b9d38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27588 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y_pred = grid_search.predict(X_test)\n",
    "\n",
    "draw_confusion_matrix(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "319fe0eb-4d4a-492c-bd50-3f08ab483021",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 97496 characters]",
      "text/plain": [
       "<Figure size 1400x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_roc_curve(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab122f66-1591-43ea-a364-2564f09b2bb3",
   "metadata": {},
   "source": [
    "# Segmentation du score de prédiction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "279e18c7-29d8-4328-963a-18babd13c2c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 86344 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "coefficients = pipeline.named_steps['logreg'].coef_[0]\n",
    "feature_names = pipeline.named_steps['logreg'].feature_names_in_\n",
    "\n",
    "# Tracer l'importance des caractéristiques\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.barh(feature_names, coefficients, color='skyblue')\n",
    "plt.xlabel('Importance des caractéristiques')\n",
    "plt.ylabel('Caractéristiques')\n",
    "plt.title('Importance des caractéristiques dans le modèle de régression logistique')\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "210b931c-6d46-4ebf-a9c7-d1ee05c3fadf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Création d'un dataframe avec le score\n",
    "dataset_for_segmentation = dataset_test[['customer_id'] + numeric_features + categorical_features]\n",
    "\n",
    "y_predict_proba = pipeline.predict_proba(X_test)[:, 1]\n",
    "\n",
    "dataset_for_segmentation['prediction_probability'] = y_predict_proba\n",
    "\n",
    "# Arrondir les valeurs de la colonne 'prediction_probability' et les multiplier par 10\n",
    "dataset_for_segmentation['category'] = dataset_for_segmentation['prediction_probability'].apply(lambda x: int(x * 10))\n",
    "\n",
    "dataset_for_segmentation['prediction'] = y_pred\n",
    "\n",
    "def premiere_partie(chaine):\n",
    "    if chaine:\n",
    "        return chaine.split('_')[0]\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "dataset_for_segmentation['company_number'] = dataset_for_segmentation['customer_id'].apply(lambda x: premiere_partie(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "055e47dd-9ff3-4853-a46d-d5a5edc1f361",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "969f1f92-d715-4d74-85a7-437e72838cb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>nb_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "      <th>mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>category</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.113637</td>\n",
       "      <td>0.006274</td>\n",
       "      <td>1.586366</td>\n",
       "      <td>0.005821</td>\n",
       "      <td>0.000647</td>\n",
       "      <td>548.790455</td>\n",
       "      <td>548.773103</td>\n",
       "      <td>-0.977118</td>\n",
       "      <td>0.001585</td>\n",
       "      <td>0.000776</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000032</td>\n",
       "      <td>0.999968</td>\n",
       "      <td>13.984219</td>\n",
       "      <td>1.302720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.810841</td>\n",
       "      <td>0.128432</td>\n",
       "      <td>9.611292</td>\n",
       "      <td>0.125295</td>\n",
       "      <td>0.018186</td>\n",
       "      <td>525.437516</td>\n",
       "      <td>525.275222</td>\n",
       "      <td>-0.729328</td>\n",
       "      <td>0.054312</td>\n",
       "      <td>0.111832</td>\n",
       "      <td>0.245480</td>\n",
       "      <td>0.495929</td>\n",
       "      <td>0.258591</td>\n",
       "      <td>18.413562</td>\n",
       "      <td>3.718711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.159419</td>\n",
       "      <td>0.339253</td>\n",
       "      <td>15.182143</td>\n",
       "      <td>0.337577</td>\n",
       "      <td>0.323824</td>\n",
       "      <td>501.529129</td>\n",
       "      <td>501.415505</td>\n",
       "      <td>-0.554439</td>\n",
       "      <td>0.969939</td>\n",
       "      <td>0.304757</td>\n",
       "      <td>0.392570</td>\n",
       "      <td>0.297258</td>\n",
       "      <td>0.310173</td>\n",
       "      <td>17.395042</td>\n",
       "      <td>2.608084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.153080</td>\n",
       "      <td>0.744161</td>\n",
       "      <td>27.820044</td>\n",
       "      <td>0.734881</td>\n",
       "      <td>0.600982</td>\n",
       "      <td>287.051054</td>\n",
       "      <td>286.675385</td>\n",
       "      <td>0.105360</td>\n",
       "      <td>1.776035</td>\n",
       "      <td>0.659878</td>\n",
       "      <td>0.288813</td>\n",
       "      <td>0.253244</td>\n",
       "      <td>0.457943</td>\n",
       "      <td>16.790421</td>\n",
       "      <td>4.173954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.044749</td>\n",
       "      <td>0.777640</td>\n",
       "      <td>27.353145</td>\n",
       "      <td>0.754549</td>\n",
       "      <td>0.079213</td>\n",
       "      <td>297.179255</td>\n",
       "      <td>295.019902</td>\n",
       "      <td>1.898178</td>\n",
       "      <td>0.293760</td>\n",
       "      <td>0.894877</td>\n",
       "      <td>0.666980</td>\n",
       "      <td>0.301424</td>\n",
       "      <td>0.031596</td>\n",
       "      <td>16.954707</td>\n",
       "      <td>6.060621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.237988</td>\n",
       "      <td>0.958520</td>\n",
       "      <td>46.637380</td>\n",
       "      <td>0.807655</td>\n",
       "      <td>0.484785</td>\n",
       "      <td>387.464785</td>\n",
       "      <td>380.145068</td>\n",
       "      <td>7.111357</td>\n",
       "      <td>2.080397</td>\n",
       "      <td>1.164958</td>\n",
       "      <td>0.497758</td>\n",
       "      <td>0.259769</td>\n",
       "      <td>0.242473</td>\n",
       "      <td>27.006406</td>\n",
       "      <td>12.457719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3.592233</td>\n",
       "      <td>1.102881</td>\n",
       "      <td>49.989226</td>\n",
       "      <td>0.878014</td>\n",
       "      <td>0.599906</td>\n",
       "      <td>268.627019</td>\n",
       "      <td>250.949344</td>\n",
       "      <td>17.539247</td>\n",
       "      <td>2.525994</td>\n",
       "      <td>1.420921</td>\n",
       "      <td>0.534607</td>\n",
       "      <td>0.304259</td>\n",
       "      <td>0.161134</td>\n",
       "      <td>14.073285</td>\n",
       "      <td>4.604134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.747016</td>\n",
       "      <td>1.391266</td>\n",
       "      <td>40.710335</td>\n",
       "      <td>0.914702</td>\n",
       "      <td>0.160990</td>\n",
       "      <td>309.716173</td>\n",
       "      <td>274.795570</td>\n",
       "      <td>34.796876</td>\n",
       "      <td>0.844250</td>\n",
       "      <td>1.963028</td>\n",
       "      <td>0.650364</td>\n",
       "      <td>0.263464</td>\n",
       "      <td>0.086172</td>\n",
       "      <td>26.186317</td>\n",
       "      <td>8.891703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5.698276</td>\n",
       "      <td>1.567006</td>\n",
       "      <td>63.033699</td>\n",
       "      <td>0.907915</td>\n",
       "      <td>0.334248</td>\n",
       "      <td>326.485952</td>\n",
       "      <td>257.940194</td>\n",
       "      <td>68.425460</td>\n",
       "      <td>2.794279</td>\n",
       "      <td>2.413009</td>\n",
       "      <td>0.606583</td>\n",
       "      <td>0.251567</td>\n",
       "      <td>0.141850</td>\n",
       "      <td>30.987461</td>\n",
       "      <td>11.676332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14.505956</td>\n",
       "      <td>3.211571</td>\n",
       "      <td>107.288514</td>\n",
       "      <td>1.011628</td>\n",
       "      <td>0.157119</td>\n",
       "      <td>369.696066</td>\n",
       "      <td>209.280306</td>\n",
       "      <td>160.348544</td>\n",
       "      <td>3.514464</td>\n",
       "      <td>5.394498</td>\n",
       "      <td>0.669314</td>\n",
       "      <td>0.223766</td>\n",
       "      <td>0.106920</td>\n",
       "      <td>45.928247</td>\n",
       "      <td>18.241634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2262.859155</td>\n",
       "      <td>45.619718</td>\n",
       "      <td>11051.732394</td>\n",
       "      <td>1.464789</td>\n",
       "      <td>0.154930</td>\n",
       "      <td>467.111875</td>\n",
       "      <td>31.146796</td>\n",
       "      <td>435.950994</td>\n",
       "      <td>54.295775</td>\n",
       "      <td>64.704225</td>\n",
       "      <td>0.507042</td>\n",
       "      <td>0.295775</td>\n",
       "      <td>0.197183</td>\n",
       "      <td>53.352113</td>\n",
       "      <td>26.070423</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           nb_tickets nb_purchases  total_amount nb_suppliers  \\\n",
       "                 mean         mean          mean         mean   \n",
       "category                                                        \n",
       "0            0.113637     0.006274      1.586366     0.005821   \n",
       "1            0.810841     0.128432      9.611292     0.125295   \n",
       "2            1.159419     0.339253     15.182143     0.337577   \n",
       "3            2.153080     0.744161     27.820044     0.734881   \n",
       "4            2.044749     0.777640     27.353145     0.754549   \n",
       "5            3.237988     0.958520     46.637380     0.807655   \n",
       "6            3.592233     1.102881     49.989226     0.878014   \n",
       "7            3.747016     1.391266     40.710335     0.914702   \n",
       "8            5.698276     1.567006     63.033699     0.907915   \n",
       "9           14.505956     3.211571    107.288514     1.011628   \n",
       "10        2262.859155    45.619718  11051.732394     1.464789   \n",
       "\n",
       "         vente_internet_max purchase_date_min purchase_date_max  \\\n",
       "                       mean              mean              mean   \n",
       "category                                                          \n",
       "0                  0.000647        548.790455        548.773103   \n",
       "1                  0.018186        525.437516        525.275222   \n",
       "2                  0.323824        501.529129        501.415505   \n",
       "3                  0.600982        287.051054        286.675385   \n",
       "4                  0.079213        297.179255        295.019902   \n",
       "5                  0.484785        387.464785        380.145068   \n",
       "6                  0.599906        268.627019        250.949344   \n",
       "7                  0.160990        309.716173        274.795570   \n",
       "8                  0.334248        326.485952        257.940194   \n",
       "9                  0.157119        369.696066        209.280306   \n",
       "10                 0.154930        467.111875         31.146796   \n",
       "\n",
       "         time_between_purchase nb_tickets_internet   fidelity gender_female  \\\n",
       "                          mean                mean       mean          mean   \n",
       "category                                                                      \n",
       "0                    -0.977118            0.001585   0.000776      0.000000   \n",
       "1                    -0.729328            0.054312   0.111832      0.245480   \n",
       "2                    -0.554439            0.969939   0.304757      0.392570   \n",
       "3                     0.105360            1.776035   0.659878      0.288813   \n",
       "4                     1.898178            0.293760   0.894877      0.666980   \n",
       "5                     7.111357            2.080397   1.164958      0.497758   \n",
       "6                    17.539247            2.525994   1.420921      0.534607   \n",
       "7                    34.796876            0.844250   1.963028      0.650364   \n",
       "8                    68.425460            2.794279   2.413009      0.606583   \n",
       "9                   160.348544            3.514464   5.394498      0.669314   \n",
       "10                  435.950994           54.295775  64.704225      0.507042   \n",
       "\n",
       "         gender_male gender_other nb_campaigns nb_campaigns_opened  \n",
       "                mean         mean         mean                mean  \n",
       "category                                                            \n",
       "0           0.000032     0.999968    13.984219            1.302720  \n",
       "1           0.495929     0.258591    18.413562            3.718711  \n",
       "2           0.297258     0.310173    17.395042            2.608084  \n",
       "3           0.253244     0.457943    16.790421            4.173954  \n",
       "4           0.301424     0.031596    16.954707            6.060621  \n",
       "5           0.259769     0.242473    27.006406           12.457719  \n",
       "6           0.304259     0.161134    14.073285            4.604134  \n",
       "7           0.263464     0.086172    26.186317            8.891703  \n",
       "8           0.251567     0.141850    30.987461           11.676332  \n",
       "9           0.223766     0.106920    45.928247           18.241634  \n",
       "10          0.295775     0.197183    53.352113           26.070423  "
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Grouper le DataFrame par la colonne 'category' et calculer la moyenne pour chaque groupe\n",
    "summary_stats = dataset_for_segmentation.groupby('category')[numeric_features].describe()\n",
    "\n",
    "# Sélectionner uniquement la colonne 'mean' pour chaque variable numérique\n",
    "mean_stats = summary_stats.loc[:, (slice(None), 'mean')]\n",
    "\n",
    "# Afficher le DataFrame résultant\n",
    "mean_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "14da601e-7b1b-469c-bab1-de8fad4047f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28948 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot histogram\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.hist(y_predict_proba, bins=10, range=(0, 1), color='blue', alpha=0.7)\n",
    "\n",
    "# Réglage des limites des axes x et y\n",
    "plt.xlim(0, 1)\n",
    "plt.ylim(0, None)  # Laissez le maximum sur l'axe y pour s'ajuster automatiquement\n",
    "\n",
    "plt.title('Histogramme des probabilités pour la classe 1')\n",
    "plt.xlabel('Probabilité')\n",
    "plt.ylabel('Fréquence')\n",
    "plt.grid(True)\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
