{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c488134e-680f-44e4-8c43-40c246140519",
   "metadata": {},
   "source": [
    "# Analysis of segments and marketing personae associated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9a8b8c3a-8e74-49f3-91d1-cccfc057fdcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# importations\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import io\n",
    "import s3fs\n",
    "import re\n",
    "import pickle\n",
    "import warnings\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d553c868-695f-4d57-96d6-d5c6629cefb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_model(type_of_activity, model):\n",
    "    #BUCKET = f\"projet-bdc2324-team1/Output_model/{type_of_activity}/{model}/\"\n",
    "    BUCKET = f\"projet-bdc2324-team1/2_Output/2_1_Modeling_results/standard/{type_of_activity}/{model}/\"\n",
    "    filename = model + '.pkl'\n",
    "    file_path = BUCKET + filename\n",
    "    with fs.open(file_path, mode=\"rb\") as f:\n",
    "        model_bytes = f.read()\n",
    "\n",
    "    model = pickle.loads(model_bytes)\n",
    "    return model\n",
    "\n",
    "\n",
    "def load_test_file(type_of_activity):\n",
    "    #file_path_test = f\"projet-bdc2324-team1/Generalization/{type_of_activity}/Test_set.csv\"\n",
    "    file_path_test = f\"projet-bdc2324-team1/1_Temp/1_0_Modelling_Datasets/{type_of_activity}/Test_set.csv\"\n",
    "    with fs.open(file_path_test, mode=\"rb\") as file_in:\n",
    "        dataset_test = pd.read_csv(file_in, sep=\",\")\n",
    "    return dataset_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3af80fea-a937-4ea8-bece-cfeaa89d1055",
   "metadata": {},
   "outputs": [],
   "source": [
    "# exec(open('utils_segmentation.py').read())\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# Create filesystem object\n",
    "S3_ENDPOINT_URL = \"https://\" + os.environ[\"AWS_S3_ENDPOINT\"]\n",
    "fs = s3fs.S3FileSystem(client_kwargs={'endpoint_url': S3_ENDPOINT_URL})\n",
    "\n",
    "# choose the type of companies for which you want to run the pipeline\n",
    "type_of_activity = \"sport\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cc6af7fa-33b2-4d58-ada4-e2ee7262bab9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load test set\n",
    "dataset_test = load_test_file(type_of_activity)\n",
    "\n",
    "# Load Model \n",
    "model = load_model(type_of_activity, 'LogisticRegression_Benchmark')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8238ee71-47ec-4621-9813-4b5d2fd03efd",
   "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>street_id</th>\n",
       "      <th>structure_id</th>\n",
       "      <th>mcp_contact_id</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>tenant_id</th>\n",
       "      <th>is_partner</th>\n",
       "      <th>deleted_at</th>\n",
       "      <th>is_email_true</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>...</th>\n",
       "      <th>purchases_5_2022</th>\n",
       "      <th>purchases_6_2021</th>\n",
       "      <th>purchases_6_2022</th>\n",
       "      <th>purchases_7_2021</th>\n",
       "      <th>purchases_7_2022</th>\n",
       "      <th>purchases_8_2021</th>\n",
       "      <th>purchases_8_2022</th>\n",
       "      <th>purchases_9_2021</th>\n",
       "      <th>purchases_9_2022</th>\n",
       "      <th>y_has_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5_4317407</td>\n",
       "      <td>969908</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6156473.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5_477635</td>\n",
       "      <td>109121</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6213652.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5_411639</td>\n",
       "      <td>92929</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6160271.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5_326623</td>\n",
       "      <td>79862</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6140109.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5_383915</td>\n",
       "      <td>85421</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6149409.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</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>96091</th>\n",
       "      <td>9_91205</td>\n",
       "      <td>76215</td>\n",
       "      <td>NaN</td>\n",
       "      <td>47280.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96092</th>\n",
       "      <td>9_369887</td>\n",
       "      <td>815891</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30764537.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96093</th>\n",
       "      <td>9_1007562</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96094</th>\n",
       "      <td>9_15037</td>\n",
       "      <td>12992</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2213448.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96095</th>\n",
       "      <td>9_135370</td>\n",
       "      <td>76215</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2164740.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>96096 rows × 87 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      customer_id  street_id  structure_id  mcp_contact_id  fidelity  \\\n",
       "0       5_4317407     969908           NaN       6156473.0         1   \n",
       "1        5_477635     109121           NaN       6213652.0         2   \n",
       "2        5_411639      92929           NaN       6160271.0         4   \n",
       "3        5_326623      79862           NaN       6140109.0         1   \n",
       "4        5_383915      85421           NaN       6149409.0         2   \n",
       "...           ...        ...           ...             ...       ...   \n",
       "96091     9_91205      76215           NaN         47280.0         0   \n",
       "96092    9_369887     815891           NaN      30764537.0         4   \n",
       "96093   9_1007562          1           NaN             NaN         0   \n",
       "96094     9_15037      12992           NaN       2213448.0         0   \n",
       "96095    9_135370      76215           NaN       2164740.0         0   \n",
       "\n",
       "       tenant_id  is_partner  deleted_at  is_email_true  opt_in  ...  \\\n",
       "0           1771       False         NaN           True       0  ...   \n",
       "1           1771       False         NaN           True       0  ...   \n",
       "2           1771       False         NaN           True       0  ...   \n",
       "3           1771       False         NaN           True       1  ...   \n",
       "4           1771       False         NaN           True       1  ...   \n",
       "...          ...         ...         ...            ...     ...  ...   \n",
       "96091       1490       False         NaN           True       1  ...   \n",
       "96092       1490       False         NaN           True       0  ...   \n",
       "96093       1490       False         NaN           True       0  ...   \n",
       "96094       1490       False         NaN           True       1  ...   \n",
       "96095       1490       False         NaN           True       1  ...   \n",
       "\n",
       "      purchases_5_2022 purchases_6_2021  purchases_6_2022  purchases_7_2021  \\\n",
       "0                  0.0              0.0               0.0               0.0   \n",
       "1                  0.0              0.0               0.0               0.0   \n",
       "2                  0.0              0.0               0.0               0.0   \n",
       "3                  0.0              0.0               0.0               0.0   \n",
       "4                  0.0              0.0               0.0               0.0   \n",
       "...                ...              ...               ...               ...   \n",
       "96091              0.0              0.0               0.0               0.0   \n",
       "96092              0.0              0.0               0.0               0.0   \n",
       "96093              0.0              0.0               0.0               0.0   \n",
       "96094              0.0              0.0               0.0               0.0   \n",
       "96095              0.0              0.0               0.0               0.0   \n",
       "\n",
       "       purchases_7_2022  purchases_8_2021  purchases_8_2022  purchases_9_2021  \\\n",
       "0                   0.0               0.0               0.0               0.0   \n",
       "1                   0.0               0.0               0.0               0.0   \n",
       "2                   0.0               0.0               0.0               0.0   \n",
       "3                   0.0               0.0               0.0               1.0   \n",
       "4                   0.0               0.0               0.0               0.0   \n",
       "...                 ...               ...               ...               ...   \n",
       "96091               0.0               0.0               0.0               0.0   \n",
       "96092               0.0               0.0               0.0               0.0   \n",
       "96093               0.0               0.0               0.0               0.0   \n",
       "96094               0.0               0.0               0.0               0.0   \n",
       "96095               0.0               0.0               0.0               0.0   \n",
       "\n",
       "       purchases_9_2022  y_has_purchased  \n",
       "0                   0.0              0.0  \n",
       "1                   0.0              0.0  \n",
       "2                   0.0              0.0  \n",
       "3                   0.0              0.0  \n",
       "4                   0.0              0.0  \n",
       "...                 ...              ...  \n",
       "96091               0.0              0.0  \n",
       "96092               0.0              1.0  \n",
       "96093               0.0              0.0  \n",
       "96094               0.0              0.0  \n",
       "96095               0.0              0.0  \n",
       "\n",
       "[96096 rows x 87 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5d0d0c18-8930-4304-84df-d5885ab21b16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# added : recup age\n",
    "\n",
    "def generate_test_set(type_of_comp):\n",
    "    file_path_list = fs.ls(f\"projet-bdc2324-team1/1_Temp/1_0_Modelling_Datasets/{type_of_comp}/Test_set\")\n",
    "    test_set = pd.DataFrame()\n",
    "    for file in file_path_list:\n",
    "        print(file)\n",
    "        with fs.open(file, mode=\"rb\") as file_in:\n",
    "            df = pd.read_csv(file_in, sep=\",\")\n",
    "        test_set = pd.concat([test_set, df], ignore_index = True)\n",
    "    return test_set\n",
    "\n",
    "def generate_train_set(type_of_comp):\n",
    "    file_path_list = fs.ls(f\"projet-bdc2324-team1/1_Temp/1_0_Modelling_Datasets/{type_of_comp}/Train_set\")\n",
    "    train_set = pd.DataFrame()\n",
    "    for file in file_path_list:\n",
    "        print(file)\n",
    "        with fs.open(file, mode=\"rb\") as file_in:\n",
    "            df = pd.read_csv(file_in, sep=\",\")\n",
    "        train_set = pd.concat([train_set, df], ignore_index = True)\n",
    "    return train_set\n",
    "\n",
    "def recup_var(df, activity, var) :\n",
    "    \n",
    "    df_test = generate_test_set(activity)\n",
    "    df_train = generate_train_set(activity)\n",
    "    df_all = pd.concat([df_train, df_test], ignore_index=True)\n",
    "\n",
    "    df_used = df\n",
    "    \n",
    "    df_used = df_used.set_index(\"customer_id\")\n",
    "    df_used[var] = df_all.set_index(\"customer_id\")[var]\n",
    "    df_used = df_used.reset_index()\n",
    "\n",
    "    return df_used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "51843556-d785-4d11-abfa-d4e603b32fe7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'street_id', 'structure_id', 'mcp_contact_id',\n",
       "       'fidelity', 'tenant_id', 'is_partner', 'deleted_at', 'is_email_true',\n",
       "       'opt_in', 'profession', 'last_buying_date', 'max_price', 'ticket_sum',\n",
       "       'average_price', 'average_purchase_delay', 'average_price_basket',\n",
       "       'average_ticket_basket', 'total_price', 'preferred_category',\n",
       "       'preferred_supplier', 'preferred_formula', 'purchase_count',\n",
       "       'first_buying_date', 'last_visiting_date', 'zipcode', 'country', 'age',\n",
       "       'gender_label', 'gender_female', 'gender_male', 'gender_other',\n",
       "       'categorie_age_0_10', 'categorie_age_10_20', 'categorie_age_20_30',\n",
       "       'categorie_age_30_40', 'categorie_age_40_50', 'categorie_age_50_60',\n",
       "       'categorie_age_60_70', 'categorie_age_70_80', 'categorie_age_plus_80',\n",
       "       'categorie_age_inconnue', 'country_fr', 'is_profession_known',\n",
       "       'is_zipcode_known', 'nb_campaigns', 'nb_campaigns_opened',\n",
       "       'time_to_open', 'taux_ouverture_mail', 'nb_targets', 'target_jeune',\n",
       "       'target_optin', 'target_optout', 'target_scolaire', 'target_entreprise',\n",
       "       'target_famille', 'target_newsletter', 'target_abonne', 'nb_tickets',\n",
       "       'nb_purchases', 'total_amount', 'nb_suppliers', 'achat_internet',\n",
       "       'purchase_date_min', 'purchase_date_max', 'time_between_purchase',\n",
       "       'nb_purchases_internet', 'prop_purchases_internet', 'purchases_10_2021',\n",
       "       'purchases_10_2022', 'purchases_11_2021', 'purchases_12_2021',\n",
       "       'purchases_1_2022', 'purchases_2_2022', 'purchases_3_2022',\n",
       "       'purchases_4_2022', 'purchases_5_2021', 'purchases_5_2022',\n",
       "       'purchases_6_2021', 'purchases_6_2022', 'purchases_7_2021',\n",
       "       'purchases_7_2022', 'purchases_8_2021', 'purchases_8_2022',\n",
       "       'purchases_9_2021', 'purchases_9_2022', 'y_has_purchased',\n",
       "       'has_purchased'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test = recup_var(dataset_test, type_of_activity, \"age\")\n",
    "dataset_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e4287c1a-eab6-4897-91d6-d21804518dc4",
   "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>street_id</th>\n",
       "      <th>structure_id</th>\n",
       "      <th>mcp_contact_id</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>tenant_id</th>\n",
       "      <th>is_partner</th>\n",
       "      <th>deleted_at</th>\n",
       "      <th>is_email_true</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>...</th>\n",
       "      <th>purchases_7_2022</th>\n",
       "      <th>purchases_8_2021</th>\n",
       "      <th>purchases_8_2022</th>\n",
       "      <th>purchases_9_2021</th>\n",
       "      <th>purchases_9_2022</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>has_purchased</th>\n",
       "      <th>has_purchased_estim</th>\n",
       "      <th>score</th>\n",
       "      <th>segment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5_4317407</td>\n",
       "      <td>969908</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6156473.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.366661</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5_477635</td>\n",
       "      <td>109121</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6213652.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.376898</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5_411639</td>\n",
       "      <td>92929</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6160271.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.971493</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5_326623</td>\n",
       "      <td>79862</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6140109.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.042499</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5_383915</td>\n",
       "      <td>85421</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6149409.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.351686</td>\n",
       "      <td>2</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>96091</th>\n",
       "      <td>9_91205</td>\n",
       "      <td>76215</td>\n",
       "      <td>NaN</td>\n",
       "      <td>47280.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.004917</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96092</th>\n",
       "      <td>9_369887</td>\n",
       "      <td>815891</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30764537.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.797374</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96093</th>\n",
       "      <td>9_1007562</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.039944</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96094</th>\n",
       "      <td>9_15037</td>\n",
       "      <td>12992</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2213448.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.049646</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96095</th>\n",
       "      <td>9_135370</td>\n",
       "      <td>76215</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2164740.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1490</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>...</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>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.007398</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>96096 rows × 91 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      customer_id  street_id  structure_id  mcp_contact_id  fidelity  \\\n",
       "0       5_4317407     969908           NaN       6156473.0         1   \n",
       "1        5_477635     109121           NaN       6213652.0         2   \n",
       "2        5_411639      92929           NaN       6160271.0         4   \n",
       "3        5_326623      79862           NaN       6140109.0         1   \n",
       "4        5_383915      85421           NaN       6149409.0         2   \n",
       "...           ...        ...           ...             ...       ...   \n",
       "96091     9_91205      76215           NaN         47280.0         0   \n",
       "96092    9_369887     815891           NaN      30764537.0         4   \n",
       "96093   9_1007562          1           NaN             NaN         0   \n",
       "96094     9_15037      12992           NaN       2213448.0         0   \n",
       "96095    9_135370      76215           NaN       2164740.0         0   \n",
       "\n",
       "       tenant_id  is_partner  deleted_at  is_email_true  opt_in  ...  \\\n",
       "0           1771       False         NaN           True       0  ...   \n",
       "1           1771       False         NaN           True       0  ...   \n",
       "2           1771       False         NaN           True       0  ...   \n",
       "3           1771       False         NaN           True       1  ...   \n",
       "4           1771       False         NaN           True       1  ...   \n",
       "...          ...         ...         ...            ...     ...  ...   \n",
       "96091       1490       False         NaN           True       1  ...   \n",
       "96092       1490       False         NaN           True       0  ...   \n",
       "96093       1490       False         NaN           True       0  ...   \n",
       "96094       1490       False         NaN           True       1  ...   \n",
       "96095       1490       False         NaN           True       1  ...   \n",
       "\n",
       "      purchases_7_2022 purchases_8_2021  purchases_8_2022  purchases_9_2021  \\\n",
       "0                  0.0              0.0               0.0               0.0   \n",
       "1                  0.0              0.0               0.0               0.0   \n",
       "2                  0.0              0.0               0.0               0.0   \n",
       "3                  0.0              0.0               0.0               1.0   \n",
       "4                  0.0              0.0               0.0               0.0   \n",
       "...                ...              ...               ...               ...   \n",
       "96091              0.0              0.0               0.0               0.0   \n",
       "96092              0.0              0.0               0.0               0.0   \n",
       "96093              0.0              0.0               0.0               0.0   \n",
       "96094              0.0              0.0               0.0               0.0   \n",
       "96095              0.0              0.0               0.0               0.0   \n",
       "\n",
       "       purchases_9_2022  y_has_purchased  has_purchased  has_purchased_estim  \\\n",
       "0                   0.0              0.0            0.0                  0.0   \n",
       "1                   0.0              0.0            0.0                  0.0   \n",
       "2                   0.0              0.0            0.0                  1.0   \n",
       "3                   0.0              0.0            0.0                  0.0   \n",
       "4                   0.0              0.0            0.0                  0.0   \n",
       "...                 ...              ...            ...                  ...   \n",
       "96091               0.0              0.0            0.0                  0.0   \n",
       "96092               0.0              1.0            1.0                  1.0   \n",
       "96093               0.0              0.0            0.0                  0.0   \n",
       "96094               0.0              0.0            0.0                  0.0   \n",
       "96095               0.0              0.0            0.0                  0.0   \n",
       "\n",
       "          score  segment  \n",
       "0      0.366661        2  \n",
       "1      0.376898        2  \n",
       "2      0.971493        4  \n",
       "3      0.042499        1  \n",
       "4      0.351686        2  \n",
       "...         ...      ...  \n",
       "96091  0.004917        1  \n",
       "96092  0.797374        4  \n",
       "96093  0.039944        1  \n",
       "96094  0.049646        1  \n",
       "96095  0.007398        1  \n",
       "\n",
       "[96096 rows x 91 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Processing\n",
    "\"\"\"\n",
    "X_test = dataset_test[['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'vente_internet_max', 'purchase_date_min', 'purchase_date_max', \n",
    "            'time_between_purchase', 'nb_tickets_internet',  'is_email_true', 'opt_in', 'age', #'is_partner',\n",
    "            'gender_female', 'gender_male', 'gender_other', 'nb_campaigns', 'nb_campaigns_opened', 'country_fr']]\n",
    "\"\"\"\n",
    "\"\"\"\n",
    "X_test = dataset_test[['nb_tickets', 'nb_purchases', 'total_amount', 'nb_suppliers', 'purchase_date_min', 'purchase_date_max', \n",
    "            'time_between_purchase',  'is_email_true', 'opt_in', 'age', #'is_partner',\n",
    "            'gender_female', 'gender_male', 'gender_other', 'nb_campaigns', 'nb_campaigns_opened', 'country_fr']]\n",
    "\"\"\"\n",
    "\n",
    "y_test = dataset_test[['y_has_purchased']]\n",
    "\n",
    "\n",
    "# X_test_segment = X_test\n",
    "X_test_segment = dataset_test\n",
    "\n",
    "# X_test_segment.insert(X_test.shape[1], \"country_fr\", dataset_test[\"country_fr\"])\n",
    "\n",
    "# add y_has_purchased to X_test\n",
    "X_test_segment[\"has_purchased\"] = y_test\n",
    "\n",
    "# Add prediction and probability to dataset_test\n",
    "# y_pred = model.predict(X_test)\n",
    "y_pred = model.predict(dataset_test)\n",
    "\n",
    "X_test_segment[\"has_purchased_estim\"] = y_pred\n",
    "\n",
    "#y_pred_prob = model.predict_proba(X_test)[:, 1]\n",
    "y_pred_prob = model.predict_proba(dataset_test)[:, 1]\n",
    "\n",
    "X_test_segment['score'] = y_pred_prob\n",
    "\n",
    "X_test_segment[\"segment\"] = np.where(X_test_segment['score']<0.25, '1',\n",
    "                   np.where(X_test_segment['score']<0.5, '2',\n",
    "                   np.where(X_test_segment['score']<0.75, '3', '4')))\n",
    "\n",
    "X_test_segment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d0d3e25f-3f0d-40ca-adb6-6f87e24edc8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'street_id', 'structure_id', 'mcp_contact_id',\n",
       "       'fidelity', 'tenant_id', 'is_partner', 'deleted_at', 'is_email_true',\n",
       "       'opt_in', 'profession', 'last_buying_date', 'max_price', 'ticket_sum',\n",
       "       'average_price', 'average_purchase_delay', 'average_price_basket',\n",
       "       'average_ticket_basket', 'total_price', 'preferred_category',\n",
       "       'preferred_supplier', 'preferred_formula', 'purchase_count',\n",
       "       'first_buying_date', 'last_visiting_date', 'zipcode', 'country', 'age',\n",
       "       'gender_label', 'gender_female', 'gender_male', 'gender_other',\n",
       "       'categorie_age_0_10', 'categorie_age_10_20', 'categorie_age_20_30',\n",
       "       'categorie_age_30_40', 'categorie_age_40_50', 'categorie_age_50_60',\n",
       "       'categorie_age_60_70', 'categorie_age_70_80', 'categorie_age_plus_80',\n",
       "       'categorie_age_inconnue', 'country_fr', 'is_profession_known',\n",
       "       'is_zipcode_known', 'nb_campaigns', 'nb_campaigns_opened',\n",
       "       'time_to_open', 'taux_ouverture_mail', 'nb_targets', 'target_jeune',\n",
       "       'target_optin', 'target_optout', 'target_scolaire', 'target_entreprise',\n",
       "       'target_famille', 'target_newsletter', 'target_abonne', 'nb_tickets',\n",
       "       'nb_purchases', 'total_amount', 'nb_suppliers', 'achat_internet',\n",
       "       'purchase_date_min', 'purchase_date_max', 'time_between_purchase',\n",
       "       'nb_purchases_internet', 'prop_purchases_internet', 'purchases_10_2021',\n",
       "       'purchases_10_2022', 'purchases_11_2021', 'purchases_12_2021',\n",
       "       'purchases_1_2022', 'purchases_2_2022', 'purchases_3_2022',\n",
       "       'purchases_4_2022', 'purchases_5_2021', 'purchases_5_2022',\n",
       "       'purchases_6_2021', 'purchases_6_2022', 'purchases_7_2021',\n",
       "       'purchases_7_2022', 'purchases_8_2021', 'purchases_8_2022',\n",
       "       'purchases_9_2021', 'purchases_9_2022', 'y_has_purchased',\n",
       "       'has_purchased', 'has_purchased_estim', 'score', 'segment',\n",
       "       'share_campaigns_opened'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9058c3b2-8fa2-4322-a57b-395da4033eaf",
   "metadata": {},
   "source": [
    "## 1. Business KPIs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3067d919-50c9-49e9-b0a6-b676a5dbae56",
   "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_purchases_internet</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>segment</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34667.0</td>\n",
       "      <td>14116.0</td>\n",
       "      <td>6.772701e+05</td>\n",
       "      <td>5836.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>36994.0</td>\n",
       "      <td>16853.0</td>\n",
       "      <td>1.215306e+06</td>\n",
       "      <td>10363.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40121.0</td>\n",
       "      <td>17157.0</td>\n",
       "      <td>1.059581e+06</td>\n",
       "      <td>10628.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>413816.0</td>\n",
       "      <td>101811.0</td>\n",
       "      <td>1.751393e+07</td>\n",
       "      <td>34378.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         nb_tickets  nb_purchases  total_amount  nb_purchases_internet\n",
       "segment                                                               \n",
       "1           34667.0       14116.0  6.772701e+05                 5836.0\n",
       "2           36994.0       16853.0  1.215306e+06                10363.0\n",
       "3           40121.0       17157.0  1.059581e+06                10628.0\n",
       "4          413816.0      101811.0  1.751393e+07                34378.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# business figures\n",
    "X_test_segment.groupby(\"segment\")[[\"nb_tickets\", \"nb_purchases\", \"total_amount\",\n",
    "                                   \"nb_purchases_internet\"]].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5b1acd28-b346-45b1-8da2-b79ca7f4fa96",
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_business_fig(df, segment, list_var) :\n",
    "    df_business_kpi = df.groupby(segment)[list_var].sum().reset_index()\n",
    "    df_business_kpi.insert(1, \"size\", df.groupby(segment).size().values)\n",
    "    all_var = [\"size\"] + list_var\n",
    "    df_business_kpi[all_var] = 100 * df_business_kpi[all_var] / df_business_kpi[all_var].sum()\n",
    "\n",
    "    return df_business_kpi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bd63d787-3ef8-4f23-9069-e9b16b4a0de8",
   "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>segment</th>\n",
       "      <th>size</th>\n",
       "      <th>nb_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_campaigns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>57.890027</td>\n",
       "      <td>6.595725</td>\n",
       "      <td>9.414621</td>\n",
       "      <td>3.309231</td>\n",
       "      <td>56.178807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>17.360764</td>\n",
       "      <td>7.038459</td>\n",
       "      <td>11.240054</td>\n",
       "      <td>5.938147</td>\n",
       "      <td>13.839223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>10.909923</td>\n",
       "      <td>7.633400</td>\n",
       "      <td>11.442806</td>\n",
       "      <td>5.177254</td>\n",
       "      <td>10.487089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>13.839286</td>\n",
       "      <td>78.732415</td>\n",
       "      <td>67.902519</td>\n",
       "      <td>85.575368</td>\n",
       "      <td>19.494881</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment       size  nb_tickets  nb_purchases  total_amount  nb_campaigns\n",
       "0       1  57.890027    6.595725      9.414621      3.309231     56.178807\n",
       "1       2  17.360764    7.038459     11.240054      5.938147     13.839223\n",
       "2       3  10.909923    7.633400     11.442806      5.177254     10.487089\n",
       "3       4  13.839286   78.732415     67.902519     85.575368     19.494881"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "business_var = [\"nb_tickets\", \"nb_purchases\", \"total_amount\", \"nb_campaigns\"]\n",
    "X_test_business_fig = df_business_fig(X_test_segment, \"segment\",\n",
    "                                      business_var)\n",
    "X_test_business_fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d2f618b6-c984-4790-bd8f-29c7d01c6707",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hist_segment_business_KPIs(df, segment, size, nb_tickets, nb_purchases, total_amount, nb_campaigns) :\n",
    "    \n",
    "    plt.figure()\n",
    "\n",
    "    df_plot = df[[segment, size, nb_tickets, nb_purchases, total_amount, nb_campaigns]]\n",
    "    \n",
    "    x = [\"number of\\ncustomers\", \"number of\\ntickets\", \"number of\\npurchases\", \"total\\namount\", \n",
    "         \"number of\\ncampaigns\"]\n",
    "\n",
    "    # liste_var = [size, nb_tickets, nb_purchases, total_amount]\n",
    "    \n",
    "    bottom = np.zeros(5)\n",
    "    \n",
    "    # Définir une palette de couleurs\n",
    "    colors = plt.cm.Blues(np.linspace(0.1, 0.9, 4))\n",
    "    \n",
    "    for i in range(4) :\n",
    "        # print(str(df_plot[segment][i]))\n",
    "        # segment = df_plot[segment][i]\n",
    "        height = list(df_plot.loc[i,size:].values)\n",
    "    \n",
    "        plt.bar(x=x, height=height, label = str(df_plot[segment][i]), bottom=bottom, color=colors[i])\n",
    "    \n",
    "        bottom+=height\n",
    "\n",
    "    # Ajuster les marges\n",
    "    plt.subplots_adjust(left = 0.125, right = 0.8, bottom = 0.1, top = 0.9)\n",
    "    \n",
    "    plt.legend(title = \"segment\", loc = \"upper right\", bbox_to_anchor=(1.2, 1))\n",
    "    plt.ylabel(\"Fraction represented by the segment (%)\")\n",
    "    plt.title(f\"Relative weight of each segment regarding business KPIs\\nfor {type_of_activity} companies\", size=12)\n",
    "    # plt.title(\"test\")\n",
    "    # plt.show()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "14b6ae5c-d704-4f5d-9f9b-5646e29ea470",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41872 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "hist_segment_business_KPIs(X_test_business_fig, \"segment\", \"size\", *business_var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f358fba3-f778-4414-bf55-c830be647ddd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'projet-bdc2324-team1/Output_marketing_personae_analysis/sport/segments_business_KPIs_sport.csv'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "activity =  \"sport\"\n",
    "PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{activity}/\"\n",
    "\n",
    "file_name = \"segments_business_KPIs_\" + activity\n",
    "FILE_PATH_OUT_S3 = PATH + file_name + \".csv\"\n",
    "\n",
    "FILE_PATH_OUT_S3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3eee7b59-f658-402d-95b2-fa051188fd10",
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_file_s3_mp(File_name, type_of_activity):\n",
    "    image_buffer = io.BytesIO()\n",
    "    plt.savefig(image_buffer, format='png')\n",
    "    image_buffer.seek(0)\n",
    "    PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{type_of_activity}/\"\n",
    "    FILE_PATH_OUT_S3 = PATH + File_name + type_of_activity + '.png'\n",
    "    with fs.open(FILE_PATH_OUT_S3, 'wb') as s3_file:\n",
    "        s3_file.write(image_buffer.read())\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "1790cb81-3304-41f1-a371-d8c926d32906",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save to Minio\n",
    "\n",
    "activity =  \"sport\"\n",
    "PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{activity}/\"\n",
    "\n",
    "file_name = \"segments_business_KPI_\" + activity\n",
    "# file_name = \"segments_business_KPIs_\" + activity\n",
    "FILE_PATH_OUT_S3 = PATH + file_name + \".png\"\n",
    "\n",
    "hist_segment_business_KPIs(X_test_business_fig, \"segment\", \"size\", \"nb_tickets\", \n",
    "                           \"nb_purchases\", \"total_amount\", \"nb_campaigns\")\n",
    "\n",
    "image_buffer = io.BytesIO()\n",
    "plt.savefig(image_buffer, format='png', dpi=110)\n",
    "image_buffer.seek(0)\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as s3_file:\n",
    "    s3_file.write(image_buffer.read())\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "cbf2cc62-1144-48c6-90d8-e12c8e510e02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41904 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "hist_segment_business_KPIs(X_test_business_fig, \"segment\", \"size\", \"nb_tickets\", \n",
    "                           \"nb_purchases\", \"total_amount\", \"nb_campaigns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "7a42523d-f80f-488b-ad8f-39dd793cddd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# with function\n",
    "\n",
    "# activity = \"sport\"\n",
    "\n",
    "hist_segment_business_KPIs(X_test_business_fig, \"segment\", \"size\", \"nb_tickets\", \n",
    "                           \"nb_purchases\", \"total_amount\", \"nb_campaigns\")\n",
    "\n",
    "save_file_s3_mp(File_name = \"segments_business_KPIs_\", type_of_activity = type_of_activity)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53d24165-6b98-4b66-9ad8-7514564689d8",
   "metadata": {},
   "source": [
    "## 2. Spider plot summarizing sociodemographic characteristics and purchasing behaviour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "beb31e4b-a01b-4312-879a-fe5757ea061f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_segment_mp(df, segment, gender_female, gender_male, gender_other, country_fr, age) :\n",
    "    df_mp = df.groupby(segment)[[gender_female, gender_male, gender_other, country_fr, age]].mean().reset_index()\n",
    "    # df_mp.insert(3, \"share_known_gender\", df_mp[gender_female]+df_mp[gender_male])\n",
    "    df_mp.insert(4, \"share_of_women\", df_mp[gender_female]/(df_mp[gender_female]+df_mp[gender_male]))\n",
    "    return df_mp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "267ebaee-eaef-4720-8ca9-e40c0cf125df",
   "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>segment</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>share_of_women</th>\n",
       "      <th>country_fr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.234460</td>\n",
       "      <td>0.419216</td>\n",
       "      <td>0.346324</td>\n",
       "      <td>0.358679</td>\n",
       "      <td>0.511056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.295031</td>\n",
       "      <td>0.539591</td>\n",
       "      <td>0.165378</td>\n",
       "      <td>0.353490</td>\n",
       "      <td>0.726962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.232354</td>\n",
       "      <td>0.583174</td>\n",
       "      <td>0.184472</td>\n",
       "      <td>0.284912</td>\n",
       "      <td>0.633363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.200692</td>\n",
       "      <td>0.674637</td>\n",
       "      <td>0.124671</td>\n",
       "      <td>0.229276</td>\n",
       "      <td>0.678772</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  gender_female  gender_male  gender_other  share_of_women  \\\n",
       "0       1       0.234460     0.419216      0.346324        0.358679   \n",
       "1       2       0.295031     0.539591      0.165378        0.353490   \n",
       "2       3       0.232354     0.583174      0.184472        0.284912   \n",
       "3       4       0.200692     0.674637      0.124671        0.229276   \n",
       "\n",
       "   country_fr  \n",
       "0    0.511056  \n",
       "1    0.726962  \n",
       "2    0.633363  \n",
       "3    0.678772  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# description of marketing personae\n",
    "\n",
    "X_test_segment_mp = X_test_segment.groupby(\"segment\")[['gender_female', 'gender_male', 'gender_other', 'country_fr']].mean().reset_index()\n",
    "# X_test_segment_mp.insert(3, \"share_known_gender\", X_test_segment_mp[\"gender_female\"]+X_test_segment_mp[\"gender_male\"])\n",
    "X_test_segment_mp.insert(4, \"share_of_women\", X_test_segment_mp[\"gender_female\"]/(X_test_segment_mp[\"gender_female\"]+X_test_segment_mp[\"gender_male\"]))\n",
    "X_test_segment_mp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5f908232-b0fe-4707-a8c5-5cadb7d8653f",
   "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>segment</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>share_of_women</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.234460</td>\n",
       "      <td>0.419216</td>\n",
       "      <td>0.346324</td>\n",
       "      <td>0.358679</td>\n",
       "      <td>0.511056</td>\n",
       "      <td>40.652136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.295031</td>\n",
       "      <td>0.539591</td>\n",
       "      <td>0.165378</td>\n",
       "      <td>0.353490</td>\n",
       "      <td>0.726962</td>\n",
       "      <td>36.204792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.232354</td>\n",
       "      <td>0.583174</td>\n",
       "      <td>0.184472</td>\n",
       "      <td>0.284912</td>\n",
       "      <td>0.633363</td>\n",
       "      <td>37.533425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.200692</td>\n",
       "      <td>0.674637</td>\n",
       "      <td>0.124671</td>\n",
       "      <td>0.229276</td>\n",
       "      <td>0.678772</td>\n",
       "      <td>39.665371</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  gender_female  gender_male  gender_other  share_of_women  \\\n",
       "0       1       0.234460     0.419216      0.346324        0.358679   \n",
       "1       2       0.295031     0.539591      0.165378        0.353490   \n",
       "2       3       0.232354     0.583174      0.184472        0.284912   \n",
       "3       4       0.200692     0.674637      0.124671        0.229276   \n",
       "\n",
       "   country_fr        age  \n",
       "0    0.511056  40.652136  \n",
       "1    0.726962  36.204792  \n",
       "2    0.633363  37.533425  \n",
       "3    0.678772  39.665371  "
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_segment_mp = df_segment_mp(X_test_segment, \"segment\", \"gender_female\", \n",
    "                                  \"gender_male\", \"gender_other\", \"country_fr\", \"age\")\n",
    "X_test_segment_mp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "910876fe-e6df-4f8d-9978-5d6fdd893ac0",
   "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>segment</th>\n",
       "      <th>prop_purchases_internet</th>\n",
       "      <th>share_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.090439</td>\n",
       "      <td>0.141985</td>\n",
       "      <td>0.587075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.502232</td>\n",
       "      <td>0.271623</td>\n",
       "      <td>0.111611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.681753</td>\n",
       "      <td>0.299255</td>\n",
       "      <td>0.122377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.528249</td>\n",
       "      <td>0.349811</td>\n",
       "      <td>0.178660</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  prop_purchases_internet  share_campaigns_opened    opt_in\n",
       "0       1                 0.090439                0.141985  0.587075\n",
       "1       2                 0.502232                0.271623  0.111611\n",
       "2       3                 0.681753                0.299255  0.122377\n",
       "3       4                 0.528249                0.349811  0.178660"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# purchasing behaviour\n",
    "\n",
    "# X_test_segment[\"share_tickets_internet\"] = X_test_segment[\"nb_tickets_internet\"]/X_test_segment[\"nb_tickets\"]\n",
    "X_test_segment[\"share_campaigns_opened\"] = X_test_segment[\"nb_campaigns_opened\"]/X_test_segment[\"nb_campaigns\"]\n",
    "X_test_segment_pb = X_test_segment.groupby(\"segment\")[[\"prop_purchases_internet\", \"share_campaigns_opened\", \"opt_in\"]].mean().reset_index()\n",
    "X_test_segment_pb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8d3ab073-040c-4480-bd44-33fc88626707",
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_segment_pb (df, segment, nb_tickets_internet, nb_tickets, nb_campaigns_opened, nb_campaigns, opt_in,\n",
    "                  time_to_open) :\n",
    "    df_used = df\n",
    "    df_used[\"share_tickets_internet\"] = df_used[nb_tickets_internet]/df_used[nb_tickets]\n",
    "    df_used[\"share_campaigns_opened\"] = df_used[nb_campaigns_opened]/df_used[nb_campaigns]\n",
    "    df_pb = df_used.groupby(segment)[[\"share_tickets_internet\", \"share_campaigns_opened\", \n",
    "                                      opt_in, time_to_open]].mean().reset_index()\n",
    "    df_pb[\"time_to_open_med\"] = df_used.groupby(segment)[[time_to_open]].apply(lambda x: x.dropna().median()).values\n",
    "    return df_pb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "33a11ddf-b410-4cf1-9e6b-645de6dad604",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Durée totale en heures : 49.65333333333333\n"
     ]
    }
   ],
   "source": [
    "# add : variable time to open\n",
    "\n",
    "from datetime import timedelta\n",
    "\n",
    "def str_duration_to_hours(duration_str):\n",
    "    parts = duration_str.split()\n",
    "    days = int(parts[0]) if len(parts) > 1 else 0\n",
    "    time_parts = parts[-1].split(':')\n",
    "    hours = int(time_parts[0])\n",
    "    minutes = int(time_parts[1])\n",
    "    seconds = int(time_parts[2].split('.')[0])\n",
    "    total_hours = days * 24 + hours + minutes / 60 + seconds / 3600\n",
    "    return total_hours\n",
    "\n",
    "# Exemple d'utilisation :\n",
    "duration_str = '2 days 01:39:12.750000'\n",
    "\n",
    "hours = str_duration_to_hours(duration_str)\n",
    "print(\"Durée totale en heures :\", hours)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4760743c-1032-452a-85fa-63d1447a742c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "segment\n",
       "1     6.418056\n",
       "2     8.031389\n",
       "3    13.037500\n",
       "4    15.197500\n",
       "Name: time_to_open, dtype: float64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# def of the variable time_to_open\n",
    "\n",
    "X_test_segment[\"time_to_open\"] = dataset_test[\"time_to_open\"].apply(lambda x : np.nan if pd.isna(x) else str_duration_to_hours(x))\n",
    "X_test_segment.groupby(\"segment\")[\"time_to_open\"].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0cb8f47a-bf0f-4285-b2ff-d90de394c787",
   "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>segment</th>\n",
       "      <th>share_tickets_internet</th>\n",
       "      <th>share_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>time_to_open_med</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.527270</td>\n",
       "      <td>0.136565</td>\n",
       "      <td>0.730064</td>\n",
       "      <td>56.785498</td>\n",
       "      <td>6.418056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.629648</td>\n",
       "      <td>0.194240</td>\n",
       "      <td>0.275860</td>\n",
       "      <td>56.349272</td>\n",
       "      <td>8.031389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.654488</td>\n",
       "      <td>0.292206</td>\n",
       "      <td>0.054260</td>\n",
       "      <td>57.847390</td>\n",
       "      <td>13.037500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.606618</td>\n",
       "      <td>0.370733</td>\n",
       "      <td>0.127051</td>\n",
       "      <td>57.567684</td>\n",
       "      <td>15.197500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  share_tickets_internet  share_campaigns_opened    opt_in  \\\n",
       "0       1                0.527270                0.136565  0.730064   \n",
       "1       2                0.629648                0.194240  0.275860   \n",
       "2       3                0.654488                0.292206  0.054260   \n",
       "3       4                0.606618                0.370733  0.127051   \n",
       "\n",
       "   time_to_open  time_to_open_med  \n",
       "0     56.785498          6.418056  \n",
       "1     56.349272          8.031389  \n",
       "2     57.847390         13.037500  \n",
       "3     57.567684         15.197500  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_segment_pb = df_segment_pb(X_test_segment, \"segment\", \"nb_tickets_internet\", \"nb_tickets\", \n",
    "                                  \"nb_campaigns_opened\", \"nb_campaigns\", \"opt_in\", \"time_to_open\")\n",
    "X_test_segment_pb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ba2884e3-004a-4554-ab82-6d477dcc4869",
   "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>segment</th>\n",
       "      <th>prop_purchases_internet</th>\n",
       "      <th>share_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>share_of_women</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.090439</td>\n",
       "      <td>0.141985</td>\n",
       "      <td>0.587075</td>\n",
       "      <td>0.358679</td>\n",
       "      <td>40.652136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.502232</td>\n",
       "      <td>0.271623</td>\n",
       "      <td>0.111611</td>\n",
       "      <td>0.353490</td>\n",
       "      <td>36.204792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.681753</td>\n",
       "      <td>0.299255</td>\n",
       "      <td>0.122377</td>\n",
       "      <td>0.284912</td>\n",
       "      <td>37.533425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.528249</td>\n",
       "      <td>0.349811</td>\n",
       "      <td>0.178660</td>\n",
       "      <td>0.229276</td>\n",
       "      <td>39.665371</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  prop_purchases_internet  share_campaigns_opened    opt_in  \\\n",
       "0       1                 0.090439                0.141985  0.587075   \n",
       "1       2                 0.502232                0.271623  0.111611   \n",
       "2       3                 0.681753                0.299255  0.122377   \n",
       "3       4                 0.528249                0.349811  0.178660   \n",
       "\n",
       "   share_of_women        age  \n",
       "0        0.358679  40.652136  \n",
       "1        0.353490  36.204792  \n",
       "2        0.284912  37.533425  \n",
       "3        0.229276  39.665371  "
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#X_test_segment_caract = pd.concat([X_test_segment_pb.drop(\"time_to_open\", axis=1), X_test_segment_mp[['share_known_gender', 'share_of_women', 'country_fr', 'age']]], axis=1)\n",
    "X_test_segment_caract = pd.concat([X_test_segment_pb, X_test_segment_mp[[ 'share_of_women', 'age']]], axis=1)\n",
    "X_test_segment_caract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "id": "23a37e9b-bb29-4122-85cb-cc15cc344ee2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "share_tickets_internet     0.654488\n",
       "share_campaigns_opened     0.370733\n",
       "opt_in                     0.730064\n",
       "time_to_open_med          15.197500\n",
       "share_known_gender         0.903085\n",
       "share_of_women             0.571869\n",
       "country_fr                 0.805862\n",
       "dtype: float64"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_segment_caract.loc[:,\"share_tickets_internet\":].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0809e2ae-3487-4b24-8f60-741c683cb9af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def d'une fonction associée - KEEP THIS !!!\n",
    "\n",
    "def radar_mp_plot(df, categories, index, var_not_perc) :\n",
    "    categories = categories\n",
    "\n",
    "    # true values are used to print the true value in parenthesis\n",
    "    tvalues = list(df.loc[index,categories]) \n",
    "\n",
    "    max_values = df[categories].max()\n",
    "\n",
    "    # values are true values / max among the 4 segments, allows to \n",
    "    # put values in relation with the values for other segments\n",
    "    # if the point has a maximal abscisse it means that value is maximal for the segment considered\n",
    "    # , event if not equal to 1\n",
    "    \n",
    "    values = list(df.loc[index,categories]/max_values)\n",
    "                  \n",
    "    # values normalized are used to adjust the value around the circle\n",
    "    # for instance if the maximum of values is equal to 0.8, we want the point to be \n",
    "    # at 8/10th of the circle radius, not at the edge \n",
    "    values_normalized = [ max(values) * elt for elt in values]\n",
    "\n",
    "    # Nb of categories\n",
    "    num_categories = len(categories)\n",
    "    \n",
    "    angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "    \n",
    "    # Initialize graphic\n",
    "    fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))\n",
    "    \n",
    "    # we have to draw first a transparent line (alpha=0) of values to adjust the radius of the circle\n",
    "    # which is based on max(value)\n",
    "    ax.plot(angles + angles[:1], values + values[:1], color='skyblue', alpha=0, linewidth=1.5)\n",
    "    ax.plot(angles + angles[:1], values_normalized + values_normalized[:1], color='black', alpha = 0.5, linewidth=1.2)\n",
    "    \n",
    "    # fill the sector\n",
    "    ax.fill(angles, values_normalized, color='orange', alpha=0.4)\n",
    "    \n",
    "    # labels\n",
    "    ax.set_yticklabels([])\n",
    "    ax.set_xticks(angles)\n",
    "\n",
    "    # define tick labels\n",
    "    values_printed = [str(round(tvalues[i],2)) if categories[i] in var_not_perc else f\"{round(100 * tvalues[i],2)}%\" for i in range(len(categories))]\n",
    "    # ticks = [categories[i].replace(\"_\",\" \") + f\"\\n({round(100 * tvalues[i],2)}%)\" for i in range(len(categories))]\n",
    "    ticks = [categories[i].replace(\"_\",\" \") + f\"\\n({values_printed[i]})\" for i in range(len(categories))]\n",
    "\n",
    "    ax.set_xticklabels(ticks, color=\"black\")\n",
    "    \n",
    "    ax.spines['polar'].set_visible(False)\n",
    "    \n",
    "    plt.title(f'Characteristics of the segment {index+1}\\n')\n",
    "    \n",
    "    # plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "id": "2fe80072-90d1-4e17-b8a7-ddc3e3be1b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['60.66%', '37.07%', '12.71%', '15.2', '20.82%', '63.9%']"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var_not_perc = [\"time_to_open_med\", \"age\"]\n",
    "\n",
    "tvalues = list(X_test_segment_caract.loc[3,categories]) \n",
    "\n",
    "values_printed = [str(round(tvalues[i],2)) if categories[i] in var_not_perc else f\"{round(100 * tvalues[i],2)}%\" for i in range(len(categories))]\n",
    "values_printed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cd3cb227-28b2-461e-a921-cff721c356e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['share_tickets_internet',\n",
       " 'share_campaigns_opened',\n",
       " 'opt_in',\n",
       " 'time_to_open_med',\n",
       " 'share_of_women',\n",
       " 'country_fr',\n",
       " 'age']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(X_test_segment_caract.drop([\"segment\", \"share_known_gender\"], axis=1).columns)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9a550db7-ddd7-4d6f-bf98-cf0b2ea35d91",
   "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>segment</th>\n",
       "      <th>prop_purchases_internet</th>\n",
       "      <th>share_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>share_of_women</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.090439</td>\n",
       "      <td>0.141985</td>\n",
       "      <td>0.587075</td>\n",
       "      <td>0.358679</td>\n",
       "      <td>40.652136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.502232</td>\n",
       "      <td>0.271623</td>\n",
       "      <td>0.111611</td>\n",
       "      <td>0.353490</td>\n",
       "      <td>36.204792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.681753</td>\n",
       "      <td>0.299255</td>\n",
       "      <td>0.122377</td>\n",
       "      <td>0.284912</td>\n",
       "      <td>37.533425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.528249</td>\n",
       "      <td>0.349811</td>\n",
       "      <td>0.178660</td>\n",
       "      <td>0.229276</td>\n",
       "      <td>39.665371</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  prop_purchases_internet  share_campaigns_opened    opt_in  \\\n",
       "0       1                 0.090439                0.141985  0.587075   \n",
       "1       2                 0.502232                0.271623  0.111611   \n",
       "2       3                 0.681753                0.299255  0.122377   \n",
       "3       4                 0.528249                0.349811  0.178660   \n",
       "\n",
       "   share_of_women        age  \n",
       "0        0.358679  40.652136  \n",
       "1        0.353490  36.204792  \n",
       "2        0.284912  37.533425  \n",
       "3        0.229276  39.665371  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_segment_caract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "56cb026b-857f-42eb-baed-0ebdf5aee447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 110548 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "var_not_perc = [\"age\"]\n",
    "\n",
    "categories = list(X_test_segment_caract.drop([\"segment\"], axis=1).columns)\n",
    "#for i in range(4) :\n",
    "#    radar_mp_plot(df=X_test_segment_caract, categories=categories, index=i)\n",
    "radar_mp_plot(df=X_test_segment_caract, categories=categories, index=3, var_not_perc=var_not_perc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 739,
   "id": "5b3c4bac-396e-4117-a7d9-f39a3d8f95b4",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (4005960846.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[739], line 6\u001b[0;36m\u001b[0m\n\u001b[0;31m    file_name = \"spider_chart_\" + activity + \"_sgt_\" str(index)\u001b[0m\n\u001b[0m                                                     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# export to MinIo\n",
    "\n",
    "activity =  \"sport\"\n",
    "PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{activity}/\"\n",
    "\n",
    "file_name = \"spider_chart_\" + activity + \"_sgt_\" + str(index)\n",
    "FILE_PATH_OUT_S3 = PATH + file_name + \".csv\"\n",
    "\n",
    "\n",
    "radar_mp_plot(df=X_test_segment_caract, categories=categories, index=3)\n",
    "\n",
    "image_buffer = io.BytesIO()\n",
    "plt.savefig(image_buffer, format='png')\n",
    "image_buffer.seek(0)\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as s3_file:\n",
    "    s3_file.write(image_buffer.read())\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 740,
   "id": "276de9a5-d506-4c11-a7c2-a23ebbc59fe5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'projet-bdc2324-team1/Output_marketing_personae_analysis/sport/spider_chart_sport_sgt_3.csv'"
      ]
     },
     "execution_count": 740,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "activity =  \"sport\"\n",
    "PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{activity}/\"\n",
    "\n",
    "file_name = \"spider_chart_\" + activity + \"_sgt_\" + str(index)\n",
    "FILE_PATH_OUT_S3 = PATH + file_name + \".csv\"\n",
    "FILE_PATH_OUT_S3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "80e47dbc-3efd-4857-8055-876b308cbcb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# general function to have the 4 radars in one plot\n",
    "\n",
    "def radar_mp_plot_all(df, categories, var_not_perc) :\n",
    "    \n",
    "    nb_segments = df.shape[0]\n",
    "    categories = categories\n",
    "\n",
    "    # Initialize graphic\n",
    "    fig, ax = plt.subplots(2,2, figsize=(20, 21), subplot_kw=dict(polar=True))\n",
    "    \n",
    "    for index in range(nb_segments) :\n",
    "        row = index // 2  # Division entière pour obtenir le numéro de ligne\n",
    "        col = index % 2 \n",
    "            \n",
    "        # df = X_test_segment_caract\n",
    "    \n",
    "        # true values are used to print the true value in parenthesis\n",
    "        tvalues = list(df.loc[index,categories]) \n",
    "        \n",
    "        max_values = df[categories].max()\n",
    "        \n",
    "        # values are true values / max among the 4 segments, allows to \n",
    "        # put values in relation with the values for other segments\n",
    "        # if the point has a maximal abscisse it means that value is maximal for the segment considered\n",
    "        # , event if not equal to 1\n",
    "        \n",
    "        values = list(df.loc[index,categories]/max_values)\n",
    "                      \n",
    "        # values normalized are used to adjust the value around the circle\n",
    "        # for instance if the maximum of values is equal to 0.8, we want the point to be \n",
    "        # at 8/10th of the circle radius, not at the edge \n",
    "        values_normalized = [ max(values) * elt for elt in values]\n",
    "        \n",
    "        # Nb of categories\n",
    "        num_categories = len(categories)\n",
    "\n",
    "        angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "    \n",
    "        # we have to draw first a transparent line (alpha=0) of values to adjust the radius of the circle\n",
    "        # which is based on max(value)\n",
    "        ax[row, col].plot(angles + angles[:1], values + values[:1], color='skyblue', alpha=0, linewidth=1.5)\n",
    "        ax[row, col].plot(angles + angles[:1], values_normalized + values_normalized[:1], color='black', alpha = 0.5, linewidth=1.2)\n",
    "        \n",
    "        # fill the sector\n",
    "        ax[row, col].fill(angles, values_normalized, color='orange', alpha=0.4, label = index)\n",
    "        \n",
    "        # labels\n",
    "        ax[row, col].set_yticklabels([])\n",
    "        ax[row, col].set_xticks(angles)\n",
    "        \n",
    "        # define the ticks\n",
    "        values_printed = [str(round(tvalues[i],2)) if categories[i] in var_not_perc else f\"{round(100 * tvalues[i],2)}%\" for i in range(len(categories))]\n",
    "\n",
    "        # ticks = [categories[i].replace(\"_\",\" \") + f\"\\n({round(100 * tvalues[i],2)}%)\" for i in range(len(categories))]\n",
    "        ticks = [categories[i].replace(\"_\",\" \") + f\"\\n({values_printed[i]})\" for i in range(len(categories))]\n",
    "        ax[row, col].set_xticklabels(ticks, color=\"black\", size = 20)\n",
    "        \n",
    "        ax[row, col].spines['polar'].set_visible(False)\n",
    "        \n",
    "        # plt.title(f'Characteristics of the segment {index+1}\\n')\n",
    "        ax[row, col].set_title(f'Segment {index+1}\\n', size = 24)\n",
    "        \n",
    "    fig.suptitle(f\"Characteristics of marketing personae of {type_of_activity} companies\", size=32)\n",
    "\n",
    "    plt.tight_layout()\n",
    "\n",
    "    # plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "67d9a15b-bd93-4e63-a193-e9760d710906",
   "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>segment</th>\n",
       "      <th>share_tickets_internet</th>\n",
       "      <th>share_campaigns_opened</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>time_to_open_med</th>\n",
       "      <th>share_known_gender</th>\n",
       "      <th>share_of_women</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.527270</td>\n",
       "      <td>0.136565</td>\n",
       "      <td>0.730064</td>\n",
       "      <td>6.418056</td>\n",
       "      <td>0.523129</td>\n",
       "      <td>0.571869</td>\n",
       "      <td>0.339959</td>\n",
       "      <td>41.298584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.629648</td>\n",
       "      <td>0.194240</td>\n",
       "      <td>0.275860</td>\n",
       "      <td>8.031389</td>\n",
       "      <td>0.855391</td>\n",
       "      <td>0.182710</td>\n",
       "      <td>0.805862</td>\n",
       "      <td>39.293163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.654488</td>\n",
       "      <td>0.292206</td>\n",
       "      <td>0.054260</td>\n",
       "      <td>13.037500</td>\n",
       "      <td>0.903085</td>\n",
       "      <td>0.323075</td>\n",
       "      <td>0.701258</td>\n",
       "      <td>35.176503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.606618</td>\n",
       "      <td>0.370733</td>\n",
       "      <td>0.127051</td>\n",
       "      <td>15.197500</td>\n",
       "      <td>0.864373</td>\n",
       "      <td>0.208231</td>\n",
       "      <td>0.638972</td>\n",
       "      <td>41.320841</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  segment  share_tickets_internet  share_campaigns_opened    opt_in  \\\n",
       "0       1                0.527270                0.136565  0.730064   \n",
       "1       2                0.629648                0.194240  0.275860   \n",
       "2       3                0.654488                0.292206  0.054260   \n",
       "3       4                0.606618                0.370733  0.127051   \n",
       "\n",
       "   time_to_open_med  share_known_gender  share_of_women  country_fr        age  \n",
       "0          6.418056            0.523129        0.571869    0.339959  41.298584  \n",
       "1          8.031389            0.855391        0.182710    0.805862  39.293163  \n",
       "2         13.037500            0.903085        0.323075    0.701258  35.176503  \n",
       "3         15.197500            0.864373        0.208231    0.638972  41.320841  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_segment_caract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "edf76688-1b7e-469e-873f-4884d551be66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 674980 characters]",
      "text/plain": [
       "<Figure size 2000x2100 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "categories = list(X_test_segment_caract.drop([\"segment\"], axis=1).columns)\n",
    "var_not_perc = [\"age\"]\n",
    "radar_mp_plot_all(df=X_test_segment_caract, categories=categories, var_not_perc=var_not_perc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "c48136d1-c980-4f74-a69f-ed4304c83188",
   "metadata": {},
   "outputs": [],
   "source": [
    "# export to MinIo\n",
    "\n",
    "# activity =  \"sport\"\n",
    "# PATH = f\"projet-bdc2324-team1/Output_marketing_personae_analysis/{activity}/\"\n",
    "\n",
    "file_name = \"spider_chart_all_\" + activity\n",
    "FILE_PATH_OUT_S3 = PATH + file_name + \".png\"\n",
    "\n",
    "radar_mp_plot_all(df=X_test_segment_caract, categories=categories)\n",
    "\n",
    "image_buffer = io.BytesIO()\n",
    "plt.savefig(image_buffer, format='png', dpi=110)\n",
    "image_buffer.seek(0)\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as s3_file:\n",
    "    s3_file.write(image_buffer.read())\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "6e2bb9c7-e97e-424d-991b-d44ef2684c60",
   "metadata": {},
   "outputs": [],
   "source": [
    "def radar_mp_plot_all(df, type_of_activity) :\n",
    "    \n",
    "    # table summarizing variables relative to marketing personae\n",
    "    df_mp = df.groupby(\"segment\")[[\"gender_female\", \"gender_male\", \"gender_other\", \"age\"]].mean().reset_index()\n",
    "    #df_mp.insert(3, \"share_known_gender\", df_mp[\"gender_female\"]+df_mp[\"gender_male\"])\n",
    "    df_mp.insert(4, \"share_of_women\", df_mp[\"gender_female\"]/(df_mp[\"gender_female\"]+df_mp[\"gender_male\"]))\n",
    "\n",
    "    # table relative to purchasing behaviour\n",
    "    df_pb = df.groupby(\"segment\")[[\"prop_purchases_internet\", \"taux_ouverture_mail\", \"opt_in\"]].mean().reset_index()\n",
    "\n",
    "    # concatenation of tables to prepare the plot\n",
    "    df_used = pd.concat([df_pb, df_mp[[ 'share_of_women', 'age']]], axis=1)\n",
    "\n",
    "    # visualization\n",
    "    nb_segments = df_used.shape[0]\n",
    "    categories = list(df_used.drop(\"segment\", axis=1).columns)\n",
    "\n",
    "    var_not_perc = [\"age\"]\n",
    "\n",
    "    # Initialize graphic\n",
    "    fig, ax = plt.subplots(2,2, figsize=(20, 20), subplot_kw=dict(polar=True))\n",
    "    \n",
    "    for index in range(nb_segments) :\n",
    "        row = index // 2  # Division entière pour obtenir le numéro de ligne\n",
    "        col = index % 2 \n",
    "    \n",
    "        # true values are used to print the true value in parenthesis\n",
    "        tvalues = list(df_used.loc[index,categories]) \n",
    "        \n",
    "        max_values = df_used[categories].max()\n",
    "        \n",
    "        # values are true values / max among the 4 segments, allows to \n",
    "        # put values in relation with the values for other segments\n",
    "        # if the point has a maximal abscisse it means that value is maximal for the segment considered\n",
    "        # , event if not equal to 1\n",
    "\n",
    "        values = list(df_used.loc[index,categories]/max_values)\n",
    "                          \n",
    "        # values normalized are used to adjust the value around the circle\n",
    "        # for instance if the maximum of values is equal to 0.8, we want the point to be \n",
    "        # at 8/10th of the circle radius, not at the edge \n",
    "        values_normalized = [ max(values) * elt for elt in values]\n",
    "        \n",
    "        # Nb of categories\n",
    "        num_categories = len(categories)\n",
    "    \n",
    "        angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "    \n",
    "        # we have to draw first a transparent line (alpha=0) of values to adjust the radius of the circle\n",
    "        # which is based on max(value)\n",
    "        ax[row, col].plot(angles + angles[:1], values + values[:1], color='skyblue', alpha=0, linewidth=1.5)\n",
    "        ax[row, col].plot(angles + angles[:1], values_normalized + values_normalized[:1], color='black', alpha = 0.5,\n",
    "                          linewidth=1.2)\n",
    "        \n",
    "        # fill the sector\n",
    "        ax[row, col].fill(angles, values_normalized, color='orange', alpha=0.4, label = index)\n",
    "        \n",
    "        # labels\n",
    "        ax[row, col].set_yticklabels([])\n",
    "        ax[row, col].set_xticks(angles)\n",
    "\n",
    "        # define the ticks\n",
    "        values_printed = [round(tvalues[i],2) if categories[i] in var_not_perc else f\"{round(100 * tvalues[i],2)}%\" for i in range(len(categories))]       \n",
    "        print(values_printed)\n",
    "        ticks = [categories[i].replace(\"_\",\" \") + f\"\\n({values_printed[i]})\" for i in range(len(categories))]\n",
    "        ax[row, col].set_xticklabels(ticks, color=\"black\", size = 20)\n",
    "\n",
    "        ax[row, col].spines['polar'].set_visible(False)\n",
    "        \n",
    "        ax[row, col].set_title(f'Segment {index+1}\\n', size = 24)\n",
    "        \n",
    "    fig.suptitle(f\"Characteristics of marketing personae of {type_of_activity} companies\", size=32)\n",
    "\n",
    "    plt.tight_layout()\n",
    "    # plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "3f1318b0-0177-47ef-9b72-3dc6c1f47a60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['9.04%', '12.82%', '58.71%', '35.87%', 40.65]\n",
      "['50.22%', '19.06%', '11.16%', '35.35%', 36.2]\n",
      "['68.18%', '19.45%', '12.24%', '28.49%', 37.53]\n",
      "['52.82%', '20.28%', '17.87%', '22.93%', 39.67]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 690296 characters]",
      "text/plain": [
       "<Figure size 2000x2000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "radar_mp_plot_all(X_test_segment, type_of_activity)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2395680-69fe-4247-8deb-22f8ee15830b",
   "metadata": {},
   "source": [
    "## --- end of the main part --- here are just some attempts --- ##"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 489,
   "id": "7d9a2aca-d28d-43b3-9b72-5913b20c4f04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 103256 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "colors = plt.cm.Blues(np.linspace(0.1, 0.9, 4)) \n",
    "colors = [\"blue\", \"green\", \"orange\", \"red\"]\n",
    "\n",
    "# Initialisez le graphique en étoile\n",
    "fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))\n",
    "\n",
    "for i in range(4) :\n",
    "\n",
    "    # Caractéristiques et valeurs associées (exemple)\n",
    "    categories = ['share_known_gender', 'share_of_women', 'country_fr']\n",
    "    values = list(X_test_segment_mp.loc[i,categories])  # Exemple de valeurs, ajustez selon vos données\n",
    "    \n",
    "    values_normalized = [ max(values) * elt for elt in values]\n",
    "    \n",
    "    # Nombre de caractéristiques\n",
    "    num_categories = len(categories)\n",
    "    \n",
    "    # Créer un angle pour chaque axe\n",
    "    angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "    \n",
    "    \n",
    "    # Tracer uniquement le contour du polygone\n",
    "    ax.plot(angles + angles[:1], values + values[:1], color='skyblue', alpha=0, linewidth=1.5)\n",
    "    # ax.plot(angles + angles[:1], values_normalized + values_normalized[:1], color='blue', alpha = 0.3, linewidth=1.5)\n",
    "    \n",
    "    # Remplir le secteur central avec une couleur\n",
    "    ax.fill(angles, values_normalized, color=colors[i], alpha=0.2, label = str(i+1))\n",
    "\n",
    "# Étiqueter les axes\n",
    "ax.set_yticklabels([])\n",
    "ax.set_xticks(angles)\n",
    "ax.set_xticklabels(categories)\n",
    "ax.legend()\n",
    "\n",
    "# Titre du graphique\n",
    "plt.title('Résumé des caractéristiques')\n",
    "\n",
    "# Afficher le graphique\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "id": "96aa9ff5-c1ed-49eb-8fb7-2319ac0c40be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 98308 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# KEEP THIS CODE !!\n",
    "\n",
    "# Caractéristiques et valeurs associées (exemple)\n",
    "categories = ['Force', 'Vitesse', 'Agilité', 'Précision', 'Endurance']\n",
    "values = [8, 7, 6, 9, 7]  # Exemple de valeurs, ajustez selon vos données\n",
    "\n",
    "# Plage de valeurs maximales pour chaque caractéristique\n",
    "max_range = [20, 20, 20, 20, 20]\n",
    "\n",
    "values_normalized = [2 * max(values) * x / y for x, y in zip(values, max_range)]\n",
    "\n",
    "# Nombre de caractéristiques\n",
    "num_categories = len(categories)\n",
    "\n",
    "# Créer un angle pour chaque axe\n",
    "angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "\n",
    "# Initialisez le graphique en étoile\n",
    "fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))\n",
    "\n",
    "# Tracer uniquement le contour du polygone\n",
    "ax.plot(angles + angles[:1], values + values[:1], color='skyblue', alpha=0, linewidth=1.5)\n",
    "ax.plot(angles + angles[:1], values_normalized + values_normalized[:1], color='blue', linewidth=1.5)\n",
    "\n",
    "# Remplir le secteur central avec une couleur\n",
    "ax.fill(angles, values_normalized, color='skyblue', alpha=0.4)\n",
    "\n",
    "# Étiqueter les axes\n",
    "ax.set_yticklabels([])\n",
    "ax.set_xticks(angles)\n",
    "ax.set_xticklabels(categories)\n",
    "\n",
    "# Titre du graphique\n",
    "plt.title('Résumé des caractéristiques')\n",
    "\n",
    "# Afficher le graphique\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "adb7ccb3-7dea-4347-9298-37311a2f1fb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def radar_chart(values, categories, segment) :\n",
    "    # Caractéristiques et valeurs associées (exemple)\n",
    "    categories = categories\n",
    "    values = values  # Exemple de valeurs, ajustez selon vos données\n",
    "    \n",
    "    # Nombre de caractéristiques\n",
    "    num_categories = len(categories)\n",
    "    \n",
    "    # Créer un angle pour chaque axe\n",
    "    angles = np.linspace(0, 2 * np.pi, num_categories, endpoint=False).tolist()\n",
    "    \n",
    "    # Répéter le premier angle pour fermer la figure\n",
    "    values += values[:1]\n",
    "    angles += angles[:1]\n",
    "    \n",
    "    # Initialisez le graphique en étoile\n",
    "    fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))\n",
    "    \n",
    "    # Tracer les lignes radiales\n",
    "    ax.fill(angles, values, color='skyblue', alpha=0.4)\n",
    "    \n",
    "    # Tracer les points sur les axes radiaux\n",
    "    ax.plot(angles, values, color='blue', linewidth=2, linestyle='solid')\n",
    "\n",
    "    # Afficher les valeurs associées sous les noms de variables\n",
    "    \"\"\"\n",
    "    for i, angle in enumerate(angles[:-1]):\n",
    "        x = angle\n",
    "        y = values[i] + 0.2  # Ajustez la distance des valeurs par rapport au centre\n",
    "        plt.text(x, y, str(values[i]), color='black', ha='center', fontsize=10)\n",
    "        \"\"\"\n",
    "    \n",
    "    # Remplir le secteur central avec une couleur\n",
    "    # ax.fill(angles, values, color='skyblue', alpha=0.4)\n",
    "\n",
    "    \n",
    "    # Étiqueter les axes\n",
    "    ax.set_yticklabels([])\n",
    "    #ax.set_xticks(angles[:-1])\n",
    "    #ax.set_xticklabels(categories, # fontsize=12, ha='right', rotation=45\n",
    "    #                  )\n",
    "    # ax.set_xticklabels(categories, fontsize=10, color='black', ha='right')\n",
    "\n",
    "    labels = [f\"{category} = {round(100 *value,2)}%\" for category, value in zip(categories, values[:-1])]\n",
    "    ax.set_xticks(angles[:-1])\n",
    "    ax.set_xticklabels(labels, fontsize=10, color='black', ha='right')\n",
    "    \n",
    "    # Titre du graphique\n",
    "    plt.title(f'Caracteristics of segment {segment}')\n",
    "    \n",
    "    # Afficher le graphique\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "8793fb51-812c-4500-b252-2e2d61d6ff48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 107108 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "categories= [\"share_known_gender\",\"share_of_women\",\"country_fr\"]\n",
    "radar_chart(values=X_test_segment_mp.loc[0,categories].values.tolist(), categories= categories,\n",
    "            segment = \"1\")"
   ]
  }
 ],
 "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
}
