{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ac01a6ea-bef6-4ace-89ff-1dc03a4215c2",
   "metadata": {},
   "source": [
    "# Segmentation des clients par régression logistique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "bca785be-39f7-4583-9bd8-67c1134ae275",
   "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\n",
    "from sklearn.preprocessing import StandardScaler\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",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8be4cda5-fd19-437f-bf23-9af20be537e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import scipy\n",
    "import scikitplot as skplt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "14378e7b-240f-4df7-9ce8-5e60920a7729",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.11.4'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy\n",
    "scipy.__version__ # il faut cette version pr eviter les pb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3bf57816-b023-4e84-9450-095620bddebc",
   "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": 6,
   "id": "27002f2f-a78a-414c-8e4f-b15bf6dd9e40",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2186/1677066092.py:7: DtypeWarning: Columns (11,40) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  dataset_train = pd.read_csv(file_in, sep=\",\")\n",
      "/tmp/ipykernel_2186/1677066092.py:12: DtypeWarning: Columns (40) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  dataset_test = pd.read_csv(file_in, sep=\",\")\n"
     ]
    }
   ],
   "source": [
    "# Importation des données\n",
    "BUCKET = \"projet-bdc2324-team1/1_Output/Logistique Regression databases - First approach\"\n",
    "\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + \"dataset_train.csv\"\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    dataset_train = pd.read_csv(file_in, sep=\",\")\n",
    "\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + \"dataset_test.csv\"\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    dataset_test = pd.read_csv(file_in, sep=\",\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c3928b55-8821-46da-b3b5-a036efd6d2cf",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event_type_id</th>\n",
       "      <th>name_event_types</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5.0</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   event_type_id          name_event_types\n",
       "0            2.0  offre muséale individuel\n",
       "1            4.0          spectacle vivant\n",
       "2            5.0      offre muséale groupe\n",
       "3            NaN                       NaN"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train[['event_type_id', 'name_event_types']].drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7e8a9d4d-7e55-4173-a7f4-8b8baa9610d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Choose type of event \n",
    "type_event_choosed = 5\n",
    "\n",
    "dataset_test = dataset_test[(dataset_test['event_type_id'] == type_event_choosed) | np.isnan(dataset_test['event_type_id'])]\n",
    "dataset_test['y_has_purchased'] = dataset_test['y_has_purchased'].fillna(0)\n",
    "dataset_train = dataset_train[(dataset_train['event_type_id'] == type_event_choosed) | np.isnan(dataset_train['event_type_id'])]\n",
    "dataset_train['y_has_purchased'] = dataset_train['y_has_purchased'].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e20ced8f-df1c-43bb-8d15-79f414c8225c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customer_id               0.000000\n",
       "event_type_id             0.967882\n",
       "nb_tickets                0.000000\n",
       "nb_purchases              0.000000\n",
       "total_amount              0.000000\n",
       "nb_suppliers              0.000000\n",
       "vente_internet_max        0.000000\n",
       "purchase_date_min         0.967882\n",
       "purchase_date_max         0.967882\n",
       "time_between_purchase     0.967882\n",
       "nb_tickets_internet       0.000000\n",
       "name_event_types          0.967882\n",
       "avg_amount                0.967882\n",
       "street_id                 0.000000\n",
       "is_partner                0.000000\n",
       "gender                    0.000000\n",
       "is_email_true             0.000000\n",
       "opt_in                    0.000000\n",
       "structure_id              0.856471\n",
       "mcp_contact_id            0.297844\n",
       "last_buying_date          0.642312\n",
       "max_price                 0.642312\n",
       "ticket_sum                0.000000\n",
       "average_price             0.107403\n",
       "fidelity                  0.000000\n",
       "average_purchase_delay    0.642312\n",
       "average_price_basket      0.642312\n",
       "average_ticket_basket     0.642312\n",
       "total_price               0.534909\n",
       "purchase_count            0.000000\n",
       "first_buying_date         0.642312\n",
       "country                   0.066622\n",
       "tenant_id                 0.000000\n",
       "gender_label              0.000000\n",
       "gender_female             0.000000\n",
       "gender_male               0.000000\n",
       "gender_other              0.000000\n",
       "country_fr                0.066622\n",
       "nb_campaigns              0.000000\n",
       "nb_campaigns_opened       0.000000\n",
       "time_to_open              0.553988\n",
       "y_has_purchased           0.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train.isna().sum()/len(dataset_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "05e29adb-7eef-416f-8f7b-248229eee0fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nb_tickets             0\n",
       "nb_purchases           0\n",
       "total_amount           0\n",
       "nb_suppliers           0\n",
       "vente_internet_max     0\n",
       "nb_tickets_internet    0\n",
       "opt_in                 0\n",
       "fidelity               0\n",
       "nb_campaigns           0\n",
       "nb_campaigns_opened    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train[['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'vente_internet_max', 'nb_tickets_internet', 'opt_in', 'fidelity', 'nb_campaigns', 'nb_campaigns_opened']].isna().sum()\n",
    "# pas de NaN, OK !"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2ce94258-e2d1-472a-81fc-fc11e247b423",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "228.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train['y_has_purchased'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "34bae3f7-d579-4f80-a38d-a83eb5ea8a7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.9986037223669636\n",
      "Confusion Matrix:\n",
      " [[128000     37]\n",
      " [   142     19]]\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    128037\n",
      "         1.0       0.34      0.12      0.18       161\n",
      "\n",
      "    accuracy                           1.00    128198\n",
      "   macro avg       0.67      0.56      0.59    128198\n",
      "weighted avg       1.00      1.00      1.00    128198\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "reg_columns = ['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'vente_internet_max', 'nb_tickets_internet', 'opt_in', 'fidelity', 'nb_campaigns', 'nb_campaigns_opened']\n",
    "\n",
    "X_train = dataset_train[reg_columns]\n",
    "y_train = dataset_train['y_has_purchased']\n",
    "X_test = dataset_test[reg_columns]\n",
    "y_test = dataset_test['y_has_purchased']\n",
    "\n",
    "# Fit and transform the scaler on the training data\n",
    "scaler = StandardScaler()\n",
    "\n",
    "# Transform the test data using the same scaler\n",
    "X_train_scaled = scaler.fit_transform(X_train)\n",
    "X_test_scaled = scaler.fit_transform(X_test)\n",
    "\n",
    "# Create and fit the linear regression model\n",
    "logit_model = LogisticRegression(penalty='l1', solver='liblinear', C=1.0)\n",
    "logit_model.fit(X_train_scaled, y_train)\n",
    "\n",
    "y_pred = logit_model.predict(X_test_scaled)\n",
    "\n",
    "#Evaluation du modèle \n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "class_report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Confusion Matrix:\\n\", conf_matrix)\n",
    "print(\"Classification Report:\\n\", class_report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ccc78c36-3287-46e6-89ac-7494c1a7106a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28812 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "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()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe6e14d2-001d-4585-9344-f240b84ce4af",
   "metadata": {},
   "source": [
    "## Ajout TP : test d'une nouvelle pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3782988b-52f9-4172-92d4-68948bf259c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# etape supp : suppression du client 1 (outlier car client anonyme)\n",
    "\n",
    "dataset_train = dataset_train[dataset_train[\"customer_id\"]!=1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9d19f8c0-ed31-46cd-8879-47810fa099d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# definition des variables utilisées\n",
    "\n",
    "numeric_features = ['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'nb_tickets_internet', 'fidelity', 'nb_campaigns', 'nb_campaigns_opened']\n",
    "# categorical_features = [\"opt_in\"]\n",
    "encoded_features = [\"opt_in\", \"vente_internet_max\"]\n",
    "features = numeric_features + encoded_features\n",
    "X_train = dataset_train[features]\n",
    "y_train = dataset_train['y_has_purchased']\n",
    "X_test = dataset_test[features]\n",
    "y_test = dataset_test['y_has_purchased']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "412ddfad-3d20-4fa0-afaa-79ec87b3122d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    122016.000000\n",
       "mean          0.307656\n",
       "std           3.135563\n",
       "min           0.000000\n",
       "25%           0.000000\n",
       "50%           0.000000\n",
       "75%           0.000000\n",
       "max         907.000000\n",
       "Name: fidelity, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### variable fidelity\n",
    "\n",
    "X_train[\"fidelity\"].describe() # sûrement un problème d'outlier pour fidelity\n",
    "# X_train[\"total_amount\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "97e1cd25-0961-45dd-af7f-78ab1d8088ee",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\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>nb_tickets_internet</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>vente_internet_max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>173</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2233.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>25703.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>94</td>\n",
       "      <td>130.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>True</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>224</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>557.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>175.0</td>\n",
       "      <td>34</td>\n",
       "      <td>32.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>True</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144823</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144824</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>120</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144868</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>907</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144877</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150595</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>True</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>279 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "3              0.0           0.0           0.0           0.0   \n",
       "15          2233.0          66.0       25703.0           2.0   \n",
       "24             0.0           0.0           0.0           0.0   \n",
       "28           557.0          25.0           0.0           2.0   \n",
       "34             0.0           0.0           0.0           0.0   \n",
       "...            ...           ...           ...           ...   \n",
       "144823         0.0           0.0           0.0           0.0   \n",
       "144824         0.0           0.0           0.0           0.0   \n",
       "144868         0.0           0.0           0.0           0.0   \n",
       "144877         0.0           0.0           0.0           0.0   \n",
       "150595         0.0           0.0           0.0           0.0   \n",
       "\n",
       "        nb_tickets_internet  fidelity  nb_campaigns  nb_campaigns_opened  \\\n",
       "3                       0.0       173           2.0                  0.0   \n",
       "15                      2.0        94         130.0                 60.0   \n",
       "24                      0.0       224          16.0                  0.0   \n",
       "28                    175.0        34          32.0                 15.0   \n",
       "34                      0.0        24           0.0                  0.0   \n",
       "...                     ...       ...           ...                  ...   \n",
       "144823                  0.0         9           0.0                  0.0   \n",
       "144824                  0.0       120           0.0                  0.0   \n",
       "144868                  0.0       907           0.0                  0.0   \n",
       "144877                  0.0         8           0.0                  0.0   \n",
       "150595                  0.0         6           0.0                  0.0   \n",
       "\n",
       "        opt_in  vente_internet_max  \n",
       "3         True                 0.0  \n",
       "15        True                 1.0  \n",
       "24        True                 0.0  \n",
       "28        True                 1.0  \n",
       "34        True                 0.0  \n",
       "...        ...                 ...  \n",
       "144823    True                 0.0  \n",
       "144824    True                 0.0  \n",
       "144868    True                 0.0  \n",
       "144877    True                 0.0  \n",
       "150595    True                 0.0  \n",
       "\n",
       "[279 rows x 10 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[X_train[\"fidelity\"]>5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fc17957e-b684-41cd-880f-049a4ffcc7dc",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>event_type_id</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>...</th>\n",
       "      <th>tenant_id</th>\n",
       "      <th>gender_label</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>125.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>1 days 04:13:20.492957746</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>5 days 18:07:22.615384615</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152554</th>\n",
       "      <td>1256102</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152555</th>\n",
       "      <td>1256103</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152556</th>\n",
       "      <td>1256104</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152557</th>\n",
       "      <td>1256105</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152558</th>\n",
       "      <td>1256106</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>122016 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  event_type_id  nb_tickets  nb_purchases  total_amount  \\\n",
       "3                 2            NaN         0.0           0.0           0.0   \n",
       "4                 3            NaN         0.0           0.0           0.0   \n",
       "6                 5            NaN         0.0           0.0           0.0   \n",
       "7                 6            NaN         0.0           0.0           0.0   \n",
       "8                 7            NaN         0.0           0.0           0.0   \n",
       "...             ...            ...         ...           ...           ...   \n",
       "152554      1256102            NaN         0.0           0.0           0.0   \n",
       "152555      1256103            NaN         0.0           0.0           0.0   \n",
       "152556      1256104            NaN         0.0           0.0           0.0   \n",
       "152557      1256105            NaN         0.0           0.0           0.0   \n",
       "152558      1256106            NaN         0.0           0.0           0.0   \n",
       "\n",
       "        nb_suppliers  vente_internet_max  purchase_date_min  \\\n",
       "3                0.0                 0.0                NaN   \n",
       "4                0.0                 0.0                NaN   \n",
       "6                0.0                 0.0                NaN   \n",
       "7                0.0                 0.0                NaN   \n",
       "8                0.0                 0.0                NaN   \n",
       "...              ...                 ...                ...   \n",
       "152554           0.0                 0.0                NaN   \n",
       "152555           0.0                 0.0                NaN   \n",
       "152556           0.0                 0.0                NaN   \n",
       "152557           0.0                 0.0                NaN   \n",
       "152558           0.0                 0.0                NaN   \n",
       "\n",
       "        purchase_date_max  time_between_purchase  ...  tenant_id gender_label  \\\n",
       "3                     NaN                    NaN  ...       1311         male   \n",
       "4                     NaN                    NaN  ...       1311         male   \n",
       "6                     NaN                    NaN  ...       1311         male   \n",
       "7                     NaN                    NaN  ...       1311         male   \n",
       "8                     NaN                    NaN  ...       1311       female   \n",
       "...                   ...                    ...  ...        ...          ...   \n",
       "152554                NaN                    NaN  ...       1311       female   \n",
       "152555                NaN                    NaN  ...       1311        other   \n",
       "152556                NaN                    NaN  ...       1311        other   \n",
       "152557                NaN                    NaN  ...       1311        other   \n",
       "152558                NaN                    NaN  ...       1311        other   \n",
       "\n",
       "        gender_female  gender_male  gender_other  country_fr  nb_campaigns  \\\n",
       "3                   0            1             0         1.0           2.0   \n",
       "4                   0            1             0         1.0         125.0   \n",
       "6                   0            1             0         1.0           2.0   \n",
       "7                   0            1             0         1.0          17.0   \n",
       "8                   1            0             0         1.0          27.0   \n",
       "...               ...          ...           ...         ...           ...   \n",
       "152554              1            0             0         1.0           0.0   \n",
       "152555              0            0             1         NaN           0.0   \n",
       "152556              0            0             1         NaN           0.0   \n",
       "152557              0            0             1         NaN           0.0   \n",
       "152558              0            0             1         NaN           0.0   \n",
       "\n",
       "        nb_campaigns_opened               time_to_open  y_has_purchased  \n",
       "3                       0.0                        NaN              0.0  \n",
       "4                      71.0  1 days 04:13:20.492957746              0.0  \n",
       "6                       0.0                        NaN              0.0  \n",
       "7                       0.0                        NaN              0.0  \n",
       "8                      13.0  5 days 18:07:22.615384615              0.0  \n",
       "...                     ...                        ...              ...  \n",
       "152554                  0.0                        NaN              0.0  \n",
       "152555                  0.0                        NaN              0.0  \n",
       "152556                  0.0                        NaN              0.0  \n",
       "152557                  0.0                        NaN              0.0  \n",
       "152558                  0.0                        NaN              0.0  \n",
       "\n",
       "[122016 rows x 42 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# on transforme opt_in en indicatrice\n",
    "\n",
    "dataset_train[\"opt_in\"] = dataset_train[\"opt_in\"].astype(int)\n",
    "dataset_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8ad69b5d-e2e2-4d70-b8f0-ea0d37f7fe0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# definition des variables utilisées\n",
    "\n",
    "numeric_features = ['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'nb_tickets_internet', 'fidelity', 'nb_campaigns', 'nb_campaigns_opened']\n",
    "# categorical_features = [\"opt_in\"]\n",
    "encoded_features = [\"opt_in\", \"vente_internet_max\"]\n",
    "features = numeric_features + encoded_features\n",
    "X_train = dataset_train[features]\n",
    "y_train = dataset_train['y_has_purchased']\n",
    "X_test = dataset_test[features]\n",
    "y_test = dataset_test['y_has_purchased']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ed647a6-db9a-4737-b819-57cb81691ea2",
   "metadata": {},
   "source": [
    "### Autre ajout : travail de preprocessing des données - étude des outliers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "3771eeb1-5221-44e5-a5cd-15475fbe4858",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    128198.000000\n",
       "mean          0.582536\n",
       "std         181.774597\n",
       "min           0.000000\n",
       "25%           0.000000\n",
       "50%           0.000000\n",
       "75%           0.000000\n",
       "max       65082.000000\n",
       "Name: nb_purchases, dtype: float64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. number of purchases\n",
    "\n",
    "X_train[\"nb_purchases\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "63c44b80-88cd-4339-91b9-3764e2690316",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\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>nb_tickets_internet</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>vente_internet_max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>122983.0</td>\n",
       "      <td>65082.0</td>\n",
       "      <td>878762.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>330831</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2773.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>32338.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>94</td>\n",
       "      <td>126.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>282.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>34</td>\n",
       "      <td>32.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>40.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4</td>\n",
       "      <td>24.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>52.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "      <td>22.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147155</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147242</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147414</th>\n",
       "      <td>12.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147636</th>\n",
       "      <td>15.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>165.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147950</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>747 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "2         122983.0       65082.0      878762.5           5.0   \n",
       "15          2773.0          81.0       32338.0           2.0   \n",
       "28           282.0          15.0           0.0           2.0   \n",
       "29            40.0           2.0           0.0           1.0   \n",
       "31            52.0           2.0           0.0           1.0   \n",
       "...            ...           ...           ...           ...   \n",
       "147155         4.0           2.0          44.0           1.0   \n",
       "147242         3.0           2.0          40.0           1.0   \n",
       "147414        12.0           2.0         132.0           1.0   \n",
       "147636        15.0           2.0         165.0           1.0   \n",
       "147950         2.0           2.0          29.0           1.0   \n",
       "\n",
       "        nb_tickets_internet  fidelity  nb_campaigns  nb_campaigns_opened  \\\n",
       "2                       9.0    330831           0.0                  0.0   \n",
       "15                      2.0        94         126.0                 50.0   \n",
       "28                     53.0        34          32.0                 13.0   \n",
       "29                      0.0         4          24.0                 17.0   \n",
       "31                      0.0         5          22.0                  6.0   \n",
       "...                     ...       ...           ...                  ...   \n",
       "147155                  4.0         2           0.0                  0.0   \n",
       "147242                  3.0         2           0.0                  0.0   \n",
       "147414                 12.0         2           0.0                  0.0   \n",
       "147636                 15.0         2           0.0                  0.0   \n",
       "147950                  2.0         2           0.0                  0.0   \n",
       "\n",
       "        opt_in  vente_internet_max  \n",
       "2            0                 1.0  \n",
       "15           1                 1.0  \n",
       "28           1                 1.0  \n",
       "29           1                 0.0  \n",
       "31           1                 0.0  \n",
       "...        ...                 ...  \n",
       "147155       0                 1.0  \n",
       "147242       0                 1.0  \n",
       "147414       0                 1.0  \n",
       "147636       0                 1.0  \n",
       "147950       0                 1.0  \n",
       "\n",
       "[747 rows x 10 columns]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[X_train[\"nb_purchases\"]>1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "032fbc5a-9044-41bd-b992-78077a6c8432",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.quantile(X_train[\"nb_purchases\"], 0.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "cad9f7cb-8b71-49a6-874b-e15cb9d7a204",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count    128198.000000\n",
      "mean          1.946941\n",
      "std         343.940117\n",
      "min           0.000000\n",
      "25%           0.000000\n",
      "50%           0.000000\n",
      "75%           0.000000\n",
      "max      122983.000000\n",
      "Name: nb_tickets, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "23.0"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 2. nb tickets\n",
    "\n",
    "print(X_train[\"nb_tickets\"].describe())\n",
    "np.quantile(X_train[\"nb_tickets\"], 0.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "6bb0c86d-eb61-473d-a29b-c59e7e5af489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count    128198.000000\n",
      "mean         10.496193\n",
      "std        2457.094272\n",
      "min           0.000000\n",
      "25%           0.000000\n",
      "50%           0.000000\n",
      "75%           0.000000\n",
      "max      878762.500000\n",
      "Name: total_amount, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "44.0"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. total amount\n",
    "\n",
    "print(X_train[\"total_amount\"].describe())\n",
    "np.quantile(X_train[\"total_amount\"], 0.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "ab6fded3-d8a5-4bb4-8f2d-472ea0e5e755",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count    128198.000000\n",
      "mean          2.924687\n",
      "std         923.990506\n",
      "min           0.000000\n",
      "25%           0.000000\n",
      "50%           0.000000\n",
      "75%           1.000000\n",
      "max      330831.000000\n",
      "Name: fidelity, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. fidelity\n",
    "\n",
    "print(X_train[\"fidelity\"].describe())\n",
    "np.quantile(X_train[\"fidelity\"], 0.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "c1f0ac75-71a4-43fb-844b-e006acf5927b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count    128198.000000\n",
      "mean         24.276463\n",
      "std          37.899868\n",
      "min           0.000000\n",
      "25%           1.000000\n",
      "50%           4.000000\n",
      "75%          28.000000\n",
      "max         299.000000\n",
      "Name: nb_campaigns, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "133.0"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5. nb campaigns - semble pas aberrant meme si forte variance\n",
    "\n",
    "print(X_train[\"nb_campaigns\"].describe())\n",
    "np.quantile(X_train[\"nb_campaigns\"], 0.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "8bb01064-1c23-4100-ace8-56f155e0b4ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "### on retire les outliers - variables : nb purchases, nb tickets, total amount, fidelity\n",
    "\n",
    "p99_nb_purchases = np.quantile(X_train[\"nb_purchases\"], 0.99)\n",
    "p99_nb_tickets = np.quantile(X_train[\"nb_tickets\"], 0.99)\n",
    "p99_total_amount = np.quantile(X_train[\"total_amount\"], 0.99)\n",
    "p99_fidelity = np.quantile(X_train[\"fidelity\"], 0.99)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b2b43ab6-16aa-41bc-9a62-47ab769c5bf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# filtre - on enlève les valeurs aberrantes sur les variables problématiques (retire 2% des valeurs en tt)\n",
    "\n",
    "X_train = X_train.loc[(X_train[\"nb_purchases\"] <= p99_nb_purchases) &\n",
    "(X_train[\"nb_tickets\"] <= p99_nb_tickets) &\n",
    "(X_train[\"total_amount\"] <= p99_total_amount) &\n",
    "(X_train[\"fidelity\"] <= p99_fidelity)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "b254a671-9e57-4123-ae65-55c852eb64cd",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\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>nb_tickets_internet</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>vente_internet_max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>24.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>14.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>23.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    nb_tickets  nb_purchases  total_amount  nb_suppliers  nb_tickets_internet  \\\n",
       "6          0.0           0.0           0.0           0.0                  0.0   \n",
       "7          0.0           0.0           0.0           0.0                  0.0   \n",
       "8          0.0           0.0           0.0           0.0                  0.0   \n",
       "9          0.0           0.0           0.0           0.0                  0.0   \n",
       "10         0.0           0.0           0.0           0.0                  0.0   \n",
       "\n",
       "    fidelity  nb_campaigns  nb_campaigns_opened  opt_in  vente_internet_max  \n",
       "6          1           2.0                  0.0       1                 0.0  \n",
       "7          1          12.0                  0.0       1                 0.0  \n",
       "8          1          24.0                 10.0       1                 0.0  \n",
       "9          1          14.0                  7.0       1                 0.0  \n",
       "10         1          23.0                 11.0       1                 0.0  "
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "86d90380-6ad2-4c6b-a103-53e4c1fa59e0",
   "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>customer_id</th>\n",
       "      <th>event_type_id</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>...</th>\n",
       "      <th>tenant_id</th>\n",
       "      <th>gender_label</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5 days 11:58:52</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0 days 13:29:25.714285714</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>0 days 17:17:44.090909090</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152645</th>\n",
       "      <td>1256102</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152646</th>\n",
       "      <td>1256103</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152647</th>\n",
       "      <td>1256104</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152648</th>\n",
       "      <td>1256105</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>152649</th>\n",
       "      <td>1256106</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1311</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>125792 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  event_type_id  nb_tickets  nb_purchases  total_amount  \\\n",
       "6                 5            NaN         0.0           0.0           0.0   \n",
       "7                 6            NaN         0.0           0.0           0.0   \n",
       "8                 7            NaN         0.0           0.0           0.0   \n",
       "9                 8            NaN         0.0           0.0           0.0   \n",
       "10                9            NaN         0.0           0.0           0.0   \n",
       "...             ...            ...         ...           ...           ...   \n",
       "152645      1256102            NaN         0.0           0.0           0.0   \n",
       "152646      1256103            NaN         0.0           0.0           0.0   \n",
       "152647      1256104            NaN         0.0           0.0           0.0   \n",
       "152648      1256105            NaN         0.0           0.0           0.0   \n",
       "152649      1256106            NaN         0.0           0.0           0.0   \n",
       "\n",
       "        nb_suppliers  vente_internet_max  purchase_date_min  \\\n",
       "6                0.0                 0.0                NaN   \n",
       "7                0.0                 0.0                NaN   \n",
       "8                0.0                 0.0                NaN   \n",
       "9                0.0                 0.0                NaN   \n",
       "10               0.0                 0.0                NaN   \n",
       "...              ...                 ...                ...   \n",
       "152645           0.0                 0.0                NaN   \n",
       "152646           0.0                 0.0                NaN   \n",
       "152647           0.0                 0.0                NaN   \n",
       "152648           0.0                 0.0                NaN   \n",
       "152649           0.0                 0.0                NaN   \n",
       "\n",
       "        purchase_date_max  time_between_purchase  ...  tenant_id gender_label  \\\n",
       "6                     NaN                    NaN  ...       1311         male   \n",
       "7                     NaN                    NaN  ...       1311         male   \n",
       "8                     NaN                    NaN  ...       1311       female   \n",
       "9                     NaN                    NaN  ...       1311       female   \n",
       "10                    NaN                    NaN  ...       1311       female   \n",
       "...                   ...                    ...  ...        ...          ...   \n",
       "152645                NaN                    NaN  ...       1311       female   \n",
       "152646                NaN                    NaN  ...       1311        other   \n",
       "152647                NaN                    NaN  ...       1311        other   \n",
       "152648                NaN                    NaN  ...       1311        other   \n",
       "152649                NaN                    NaN  ...       1311        other   \n",
       "\n",
       "        gender_female  gender_male  gender_other  country_fr  nb_campaigns  \\\n",
       "6                   0            1             0         1.0           2.0   \n",
       "7                   0            1             0         1.0          12.0   \n",
       "8                   1            0             0         1.0          24.0   \n",
       "9                   1            0             0         1.0          14.0   \n",
       "10                  1            0             0         1.0          23.0   \n",
       "...               ...          ...           ...         ...           ...   \n",
       "152645              1            0             0         1.0           0.0   \n",
       "152646              0            0             1         NaN           0.0   \n",
       "152647              0            0             1         NaN           0.0   \n",
       "152648              0            0             1         NaN           0.0   \n",
       "152649              0            0             1         NaN           0.0   \n",
       "\n",
       "        nb_campaigns_opened               time_to_open  y_has_purchased  \n",
       "6                       0.0                        NaN              0.0  \n",
       "7                       0.0                        NaN              0.0  \n",
       "8                      10.0            5 days 11:58:52              0.0  \n",
       "9                       7.0  0 days 13:29:25.714285714              0.0  \n",
       "10                     11.0  0 days 17:17:44.090909090              0.0  \n",
       "...                     ...                        ...              ...  \n",
       "152645                  0.0                        NaN              0.0  \n",
       "152646                  0.0                        NaN              0.0  \n",
       "152647                  0.0                        NaN              0.0  \n",
       "152648                  0.0                        NaN              0.0  \n",
       "152649                  0.0                        NaN              0.0  \n",
       "\n",
       "[125792 rows x 42 columns]"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "dataset_train = dataset_train.loc[(dataset_train[\"nb_purchases\"] <= p99_nb_purchases) &\n",
    "(dataset_train[\"nb_tickets\"] <= p99_nb_tickets) &\n",
    "(dataset_train[\"total_amount\"] <= p99_total_amount) &\n",
    "(dataset_train[\"fidelity\"] <= p99_fidelity)]\n",
    "\n",
    "dataset_train"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9487c48-b973-4d9e-abb9-902800ab778f",
   "metadata": {},
   "source": [
    "En enlevant les outliers, on supprime la plupart des clients ayant acheté à nouveau ... Il faut trouver un autre moyen de preprocessing qui ne dégrade pas le dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "9fe7513b-f23b-4bee-957d-f98919d6eb30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19.0"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_train[\"y_has_purchased\"].sum() # pb : on passe de 161 à 19 clients ayant acheté ..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b531aebb-3b2f-4c62-ae01-84bdf8e45f49",
   "metadata": {},
   "source": [
    "### Construction de la pipeline pour le modèle de régression logistique et résultats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1476da0d-cbb5-46ac-9f97-10855eec0108",
   "metadata": {},
   "outputs": [],
   "source": [
    "# importations pr créer la pipeline\n",
    "\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.linear_model import LogisticRegression\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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f905cb6f-b0be-4a47-ac8d-7b3e16ff1dce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# debut de la pipeline\n",
    "numeric_transformer = Pipeline(steps=[\n",
    "                                # (\"imputer\", SimpleImputer(strategy=\"mean\")), # NaN remplacés par la moyenne, mais peu importe car on a supprimé les valeurs manquantes\n",
    "                                (\"scaler\", StandardScaler())])\n",
    "\"\"\"\n",
    "categorical_transformer = Pipeline(steps=[\n",
    "                                    (\"imputer\", SimpleImputer(strategy=\"constant\", fill_value=\"Not defined\")),\n",
    "                                    (\"onehot\", OneHotEncoder(handle_unknown='ignore'))]) # to deal with missing categorical data\n",
    "\n",
    "\"\"\"\n",
    "preproc = ColumnTransformer(transformers=[(\"num\", numeric_transformer, numeric_features)])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d322fb8f-1e97-4a44-96ca-c0f5d7ebd383",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Returned hyperparameter: {'logreg__C': 0.0009765625, 'logreg__class_weight': 'balanced'}\n",
      "Best classification accuracy in train is: 0.25403118665289387\n",
      "Classification accuracy on test is: 0.0495079950799508\n"
     ]
    }
   ],
   "source": [
    "# on doit prendre une métrique adaptée aux datasets déséquilibrés\n",
    "balanced_scorer = make_scorer(balanced_accuracy_score)\n",
    "f1_scorer = make_scorer(f1_score)\n",
    "\n",
    "parameter_space = np.logspace(-10, 6, 17, base=2)\n",
    "\n",
    "pipe = Pipeline([('preprocessor', preproc), ('logreg', LogisticRegression(max_iter=500))]) # prendre 5k iter\n",
    "# on met plus de poids sur les observations rares (utile pr gérer le déséquilibre du dataset)\n",
    "parameters4 = {'logreg__C': parameter_space, 'logreg__class_weight': ['balanced']} \n",
    "clf4 = GridSearchCV(pipe, parameters4, cv=3, scoring = f1_scorer)\n",
    "clf4.fit(X_train, y_train)\n",
    "\n",
    "# print results\n",
    "# print(clf4.cv_results_)\n",
    "print('Returned hyperparameter: {}'.format(clf4.best_params_))\n",
    "print('Best classification accuracy in train is: {}'.format(clf4.best_score_))\n",
    "print('Classification accuracy on test is: {}'.format(clf4.score(X_test, y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b32bb668-c816-4055-b786-e548eb71f318",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.9517777188411676\n",
      "Confusion Matrix:\n",
      " [[121855   6182]\n",
      " [     0    161]]\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      0.95      0.98    128037\n",
      "         1.0       0.03      1.00      0.05       161\n",
      "\n",
      "    accuracy                           0.95    128198\n",
      "   macro avg       0.51      0.98      0.51    128198\n",
      "weighted avg       1.00      0.95      0.97    128198\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# visualisation des résultats \n",
    "\n",
    "y_pred = clf4.predict(X_test)\n",
    "\n",
    "#Evaluation du modèle \n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "class_report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Confusion Matrix:\\n\", conf_matrix)\n",
    "print(\"Classification Report:\\n\", class_report)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "faebbecb-3f85-4181-8005-2f52180fa37e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29528 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# matrice de confusion\n",
    "\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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dc66d09e-3f7b-4f6d-a60f-c21a3a057c6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47228 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# on trace la courbe ROC\n",
    "\n",
    "# Prédictions sur l'ensemble de test\n",
    "y_pred_prob = clf4.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)\n",
    "\n",
    "# Calcul de l'aire sous la courbe ROC (AUC)\n",
    "roc_auc = auc(fpr, tpr)\n",
    "\n",
    "# Tracé de la courbe ROC\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'AUC = {roc_auc:.2f}')\n",
    "plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\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": 24,
   "id": "b36a11db-5d7a-487a-9b22-f02339e6d413",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30276 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Calcul des valeurs de précision et de rappel à différents seuils\n",
    "precision, recall, thresholds = precision_recall_curve(y_test, y_pred_prob)\n",
    "\n",
    "# Calcul de l'aire sous la courbe PR (AUC-PR)\n",
    "average_precision = average_precision_score(y_test, y_pred_prob)\n",
    "\n",
    "# Tracé de la courbe PR\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.step(recall, precision, color='b', alpha=0.2, where='post')\n",
    "plt.fill_between(recall, precision, step='post', alpha=0.2, color='b')\n",
    "plt.xlabel('Rappel')\n",
    "plt.ylabel('Précision')\n",
    "plt.ylim([0.0, 1.05])\n",
    "plt.xlim([0.0, 1.0])\n",
    "plt.title(f'Courbe PR (AUC-PR = {average_precision:.2f})')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7fb157b6-4e4e-4c7d-8a37-c3ac99323795",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32796 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# utilisation d'une métrique plus adaptée aux modèles de marketing : courbe de lift\n",
    "\n",
    "# Tri des prédictions de probabilités et des vraies valeurs\n",
    "sorted_indices = np.argsort(y_pred_prob)[::-1]\n",
    "y_pred_prob_sorted = y_pred_prob[sorted_indices]\n",
    "y_test_sorted = y_test.iloc[sorted_indices]\n",
    "\n",
    "# Calcul du gain cumulatif\n",
    "cumulative_gain = np.cumsum(y_test_sorted) / np.sum(y_test_sorted)\n",
    "\n",
    "# Tracé de la courbe de lift\n",
    "plt.plot(np.linspace(0, 1, len(cumulative_gain))[:10000], (cumulative_gain/np.linspace(0, 1, len(cumulative_gain)))[:10000], label='Courbe de lift')\n",
    "plt.xlabel('Pourcentage des données')\n",
    "plt.ylabel('Gain cumulatif')\n",
    "plt.title('Courbe de Lift')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98b93d38-a5d7-4480-91e6-e79be5de18e7",
   "metadata": {},
   "source": [
    "## Random forest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "771bee72-8b12-4ffb-b3ce-82f7e2ba6a8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 3 folds for each of 9 candidates, totalling 27 fits\n",
      "Best parameters: {'max_depth': 20, 'n_estimators': 100, 'random_state': 20}\n",
      "Best classification accuracy in train is: 0.3224906065485776\n",
      "Classification accuracy on test is: 0.31906614785992216\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "# Define models and parameters for GridSearch\n",
    "params = {\n",
    "            'n_estimators': [100, 150, 200],\n",
    "            'max_depth': [5, 20, 30],\n",
    "            'random_state' : [20]\n",
    "        }\n",
    "\n",
    "\n",
    "# define model and pipeline - no preprocessing\n",
    "clf = GridSearchCV(RandomForestClassifier(), params, cv=3, scoring=f1_scorer, verbose=True)\n",
    "clf.fit(X_train, y_train)\n",
    "\n",
    "print(f\"Best parameters: {clf.best_params_}\")\n",
    "print('Best classification accuracy in train is: {}'.format(clf.best_score_))\n",
    "print('Classification accuracy on test is: {}'.format(clf.score(X_test, y_test)))\n",
    "print(\"------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bf44a84d-607e-48c3-b8c6-28a07d1b1c14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.99863492410178\n",
      "Confusion Matrix:\n",
      " [[127982     55]\n",
      " [   120     41]]\n",
      "Classification Report:\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    128037\n",
      "         1.0       0.43      0.25      0.32       161\n",
      "\n",
      "    accuracy                           1.00    128198\n",
      "   macro avg       0.71      0.63      0.66    128198\n",
      "weighted avg       1.00      1.00      1.00    128198\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# visualisation des résultats \n",
    "\n",
    "y_pred = clf.predict(X_test)\n",
    "\n",
    "#Evaluation du modèle \n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "class_report = classification_report(y_test, y_pred)\n",
    "\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Confusion Matrix:\\n\", conf_matrix)\n",
    "print(\"Classification Report:\\n\", class_report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0fa2189c-5c0a-405b-b686-b9df3958c85c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28912 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# matrice de confusion\n",
    "\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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "311f0208-b79e-4e80-8016-075a98708f6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 45760 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# on trace la courbe ROC\n",
    "\n",
    "# Prédictions sur l'ensemble de test\n",
    "y_pred_prob = clf.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)\n",
    "\n",
    "# Calcul de l'aire sous la courbe ROC (AUC)\n",
    "roc_auc = auc(fpr, tpr)\n",
    "\n",
    "# Tracé de la courbe ROC\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'AUC = {roc_auc:.2f}')\n",
    "plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')\n",
    "plt.xlabel('Taux de faux positifs (FPR)')\n",
    "plt.ylabel('Taux de vrais positifs (TPR)')\n",
    "plt.title('Courbe ROC : random forest')\n",
    "plt.legend(loc='lower right')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e20e9ac2-7232-4418-87f0-c7299a6d7de3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27552 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Calcul des valeurs de précision et de rappel à différents seuils\n",
    "precision, recall, thresholds = precision_recall_curve(y_test, y_pred_prob)\n",
    "\n",
    "# Calcul de l'aire sous la courbe PR (AUC-PR)\n",
    "average_precision = average_precision_score(y_test, y_pred_prob)\n",
    "\n",
    "# Tracé de la courbe PR\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.step(recall, precision, color='b', alpha=0.2, where='post')\n",
    "plt.fill_between(recall, precision, step='post', alpha=0.2, color='b')\n",
    "plt.xlabel('Rappel')\n",
    "plt.ylabel('Précision')\n",
    "plt.ylim([0.0, 1.05])\n",
    "plt.xlim([0.0, 1.0])\n",
    "plt.title(f'Courbe PR (AUC-PR = {average_precision:.2f})')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0633df2d-686e-4f9d-823e-e54c23f983f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33252 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# utilisation d'une métrique plus adaptée aux modèles de marketing : courbe de lift\n",
    "\n",
    "# Tri des prédictions de probabilités et des vraies valeurs\n",
    "sorted_indices = np.argsort(y_pred_prob)[::-1]\n",
    "y_pred_prob_sorted = y_pred_prob[sorted_indices]\n",
    "y_test_sorted = y_test.iloc[sorted_indices]\n",
    "\n",
    "# Calcul du gain cumulatif\n",
    "cumulative_gain = np.cumsum(y_test_sorted) / np.sum(y_test_sorted)\n",
    "\n",
    "# Tracé de la courbe de lift\n",
    "plt.plot(np.linspace(0, 1, len(cumulative_gain))[:10000], (cumulative_gain/np.linspace(0, 1, len(cumulative_gain)))[:10000], label='Courbe de lift')\n",
    "plt.xlabel('Pourcentage des données')\n",
    "plt.ylabel('Gain cumulatif')\n",
    "plt.title('Courbe de Lift')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "49dc4e25-a79e-44d7-a577-524468336b96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "52512    0.000000\n",
       "87081    0.000000\n",
       "2695     0.000000\n",
       "51486    0.006211\n",
       "15       0.012422\n",
       "           ...   \n",
       "86959    1.000000\n",
       "86960    1.000000\n",
       "86961    1.000000\n",
       "86962    1.000000\n",
       "65836    1.000000\n",
       "Name: y_has_purchased, Length: 128198, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cumulative_gain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fde953b-4cce-4879-bb5e-1852511e7054",
   "metadata": {},
   "source": [
    "## Sauvegarde des résultats (à reprendre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7ac941bf-7994-4baf-8d9f-13b93eed73a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sauvegarde\n",
    "\n",
    "with open('test_logit.pkl', 'wb') as file:\n",
    "    pickle.dump(clf4, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3ac3def3-00f2-4b31-b6f7-2cae5038b766",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pour charger les paramètres \n",
    "\n",
    "# Chargement du modèle à partir du fichier\n",
    "with open('test_logit.pkl', 'rb') as file:\n",
    "    loaded_logit = pickle.load(file)"
   ]
  }
 ],
 "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
}
