{
    "cells": [
        {
            "cell_type": "markdown",
            "id": "8750d15b",
            "metadata": {},
            "source": [
                "# Cours 2 : Algorithmes non supervisés "
            ]
        },
        {
            "cell_type": "markdown",
            "id": "f7c08ae5",
            "metadata": {},
            "source": [
                "## Préambule"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "ec7ecb4b",
            "metadata": {},
            "source": [
                "Les objectifs de cette séance (3h) sont :\n",
                "* Mettre en application un modèle non-supervisé (K-means et C.A.H)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "4e99c600",
            "metadata": {},
            "source": [
                "## Préparation du workspace"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "c1b01045",
            "metadata": {},
            "source": [
                "### Import de librairies "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "f6e62631",
            "metadata": {},
            "outputs": [],
            "source": [
                "# Données\n",
                "import numpy as np\n",
                "import pandas as pd\n",
                "\n",
                "# Graphiques\n",
                "import seaborn as sns\n",
                "\n",
                "sns.set()\n",
                "import matplotlib.pyplot as plt  # noqa: E402\n",
                "import plotly.express as px\n",
                "import plotly.graph_objects as gp\n",
                "from scipy.cluster.hierarchy import dendrogram, linkage  # noqa: E402\n",
                "\n",
                "# Statistiques\n",
                "from scipy.stats import chi2_contingency  # noqa: E402, F401\n",
                "\n",
                "# Machine Learning\n",
                "from sklearn.cluster import AgglomerativeClustering, KMeans  # noqa: E402\n"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "985e4e97",
            "metadata": {},
            "source": [
                "### Constantes"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 2,
            "id": "c9597b48",
            "metadata": {},
            "outputs": [],
            "source": [
                "input_path = \"./1_inputs\"\n",
                "output_path = \"./2_outputs\""
            ]
        },
        {
            "cell_type": "markdown",
            "id": "b2ff398d",
            "metadata": {},
            "source": [
                "## Exercice (implémentation des exercices du support de cours)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "id": "ea2a0164",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 12580 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Défintion de E\n",
                "x = [1, 2, 18, 20, 31]\n",
                "\n",
                "#Représentation graphique\n",
                "y=[0, 0, 0, 0, 0]\n",
                "plt.scatter(x, y)\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "5e4abc23",
            "metadata": {},
            "source": [
                "### K-means : Question 1 "
            ]
        },
        {
            "cell_type": "markdown",
            "id": "5dea6f90",
            "metadata": {},
            "source": [
                "**Déterminer la partition optimale par k-means en prenant pour centres initiaux les éléments 1, 2, 18**"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "41cc10ba",
            "metadata": {},
            "outputs": [],
            "source": [
                "#Définition des centres initiaux\n",
                "init_points = np.array([1, 2, 18])\n",
                "\n",
                "#Itinitialisation algo\n",
                "kmeans = KMeans(init=init_points.reshape(-1,1),\n",
                "                n_clusters=3,\n",
                "                n_init = 1)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 5,
            "id": "54857e7b",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<style>#sk-container-id-1 {\n",
                            "  /* Definition of color scheme common for light and dark mode */\n",
                            "  --sklearn-color-text: #000;\n",
                            "  --sklearn-color-text-muted: #666;\n",
                            "  --sklearn-color-line: gray;\n",
                            "  /* Definition of color scheme for unfitted estimators */\n",
                            "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
                            "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
                            "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
                            "  --sklearn-color-unfitted-level-3: chocolate;\n",
                            "  /* Definition of color scheme for fitted estimators */\n",
                            "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
                            "  --sklearn-color-fitted-level-1: #d4ebff;\n",
                            "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
                            "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
                            "\n",
                            "  /* Specific color for light theme */\n",
                            "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
                            "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
                            "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
                            "  --sklearn-color-icon: #696969;\n",
                            "\n",
                            "  @media (prefers-color-scheme: dark) {\n",
                            "    /* Redefinition of color scheme for dark theme */\n",
                            "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
                            "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
                            "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
                            "    --sklearn-color-icon: #878787;\n",
                            "  }\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 {\n",
                            "  color: var(--sklearn-color-text);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 pre {\n",
                            "  padding: 0;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 input.sk-hidden--visually {\n",
                            "  border: 0;\n",
                            "  clip: rect(1px 1px 1px 1px);\n",
                            "  clip: rect(1px, 1px, 1px, 1px);\n",
                            "  height: 1px;\n",
                            "  margin: -1px;\n",
                            "  overflow: hidden;\n",
                            "  padding: 0;\n",
                            "  position: absolute;\n",
                            "  width: 1px;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-dashed-wrapped {\n",
                            "  border: 1px dashed var(--sklearn-color-line);\n",
                            "  margin: 0 0.4em 0.5em 0.4em;\n",
                            "  box-sizing: border-box;\n",
                            "  padding-bottom: 0.4em;\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-container {\n",
                            "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
                            "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
                            "     so we also need the `!important` here to be able to override the\n",
                            "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
                            "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
                            "  display: inline-block !important;\n",
                            "  position: relative;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-text-repr-fallback {\n",
                            "  display: none;\n",
                            "}\n",
                            "\n",
                            "div.sk-parallel-item,\n",
                            "div.sk-serial,\n",
                            "div.sk-item {\n",
                            "  /* draw centered vertical line to link estimators */\n",
                            "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
                            "  background-size: 2px 100%;\n",
                            "  background-repeat: no-repeat;\n",
                            "  background-position: center center;\n",
                            "}\n",
                            "\n",
                            "/* Parallel-specific style estimator block */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel-item::after {\n",
                            "  content: \"\";\n",
                            "  width: 100%;\n",
                            "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
                            "  flex-grow: 1;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel {\n",
                            "  display: flex;\n",
                            "  align-items: stretch;\n",
                            "  justify-content: center;\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "  position: relative;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel-item {\n",
                            "  display: flex;\n",
                            "  flex-direction: column;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
                            "  align-self: flex-end;\n",
                            "  width: 50%;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
                            "  align-self: flex-start;\n",
                            "  width: 50%;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
                            "  width: 0;\n",
                            "}\n",
                            "\n",
                            "/* Serial-specific style estimator block */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-serial {\n",
                            "  display: flex;\n",
                            "  flex-direction: column;\n",
                            "  align-items: center;\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "  padding-right: 1em;\n",
                            "  padding-left: 1em;\n",
                            "}\n",
                            "\n",
                            "\n",
                            "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
                            "clickable and can be expanded/collapsed.\n",
                            "- Pipeline and ColumnTransformer use this feature and define the default style\n",
                            "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
                            "*/\n",
                            "\n",
                            "/* Pipeline and ColumnTransformer style (default) */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-toggleable {\n",
                            "  /* Default theme specific background. It is overwritten whether we have a\n",
                            "  specific estimator or a Pipeline/ColumnTransformer */\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "}\n",
                            "\n",
                            "/* Toggleable label */\n",
                            "#sk-container-id-1 label.sk-toggleable__label {\n",
                            "  cursor: pointer;\n",
                            "  display: flex;\n",
                            "  width: 100%;\n",
                            "  margin-bottom: 0;\n",
                            "  padding: 0.5em;\n",
                            "  box-sizing: border-box;\n",
                            "  text-align: center;\n",
                            "  align-items: start;\n",
                            "  justify-content: space-between;\n",
                            "  gap: 0.5em;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 label.sk-toggleable__label .caption {\n",
                            "  font-size: 0.6rem;\n",
                            "  font-weight: lighter;\n",
                            "  color: var(--sklearn-color-text-muted);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
                            "  /* Arrow on the left of the label */\n",
                            "  content: \"▸\";\n",
                            "  float: left;\n",
                            "  margin-right: 0.25em;\n",
                            "  color: var(--sklearn-color-icon);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
                            "  color: var(--sklearn-color-text);\n",
                            "}\n",
                            "\n",
                            "/* Toggleable content - dropdown */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-toggleable__content {\n",
                            "  max-height: 0;\n",
                            "  max-width: 0;\n",
                            "  overflow: hidden;\n",
                            "  text-align: left;\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-0);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-0);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-toggleable__content pre {\n",
                            "  margin: 0.2em;\n",
                            "  border-radius: 0.25em;\n",
                            "  color: var(--sklearn-color-text);\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-0);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-0);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
                            "  /* Expand drop-down */\n",
                            "  max-height: 200px;\n",
                            "  max-width: 100%;\n",
                            "  overflow: auto;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
                            "  content: \"▾\";\n",
                            "}\n",
                            "\n",
                            "/* Pipeline/ColumnTransformer-specific style */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
                            "  color: var(--sklearn-color-text);\n",
                            "  background-color: var(--sklearn-color-unfitted-level-2);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
                            "  background-color: var(--sklearn-color-fitted-level-2);\n",
                            "}\n",
                            "\n",
                            "/* Estimator-specific style */\n",
                            "\n",
                            "/* Colorize estimator box */\n",
                            "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-2);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-2);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
                            "#sk-container-id-1 div.sk-label label {\n",
                            "  /* The background is the default theme color */\n",
                            "  color: var(--sklearn-color-text-on-default-background);\n",
                            "}\n",
                            "\n",
                            "/* On hover, darken the color of the background */\n",
                            "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
                            "  color: var(--sklearn-color-text);\n",
                            "  background-color: var(--sklearn-color-unfitted-level-2);\n",
                            "}\n",
                            "\n",
                            "/* Label box, darken color on hover, fitted */\n",
                            "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
                            "  color: var(--sklearn-color-text);\n",
                            "  background-color: var(--sklearn-color-fitted-level-2);\n",
                            "}\n",
                            "\n",
                            "/* Estimator label */\n",
                            "\n",
                            "#sk-container-id-1 div.sk-label label {\n",
                            "  font-family: monospace;\n",
                            "  font-weight: bold;\n",
                            "  display: inline-block;\n",
                            "  line-height: 1.2em;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-label-container {\n",
                            "  text-align: center;\n",
                            "}\n",
                            "\n",
                            "/* Estimator-specific */\n",
                            "#sk-container-id-1 div.sk-estimator {\n",
                            "  font-family: monospace;\n",
                            "  border: 1px dotted var(--sklearn-color-border-box);\n",
                            "  border-radius: 0.25em;\n",
                            "  box-sizing: border-box;\n",
                            "  margin-bottom: 0.5em;\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-0);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-estimator.fitted {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-0);\n",
                            "}\n",
                            "\n",
                            "/* on hover */\n",
                            "#sk-container-id-1 div.sk-estimator:hover {\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-2);\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-2);\n",
                            "}\n",
                            "\n",
                            "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
                            "\n",
                            "/* Common style for \"i\" and \"?\" */\n",
                            "\n",
                            ".sk-estimator-doc-link,\n",
                            "a:link.sk-estimator-doc-link,\n",
                            "a:visited.sk-estimator-doc-link {\n",
                            "  float: right;\n",
                            "  font-size: smaller;\n",
                            "  line-height: 1em;\n",
                            "  font-family: monospace;\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "  border-radius: 1em;\n",
                            "  height: 1em;\n",
                            "  width: 1em;\n",
                            "  text-decoration: none !important;\n",
                            "  margin-left: 0.5em;\n",
                            "  text-align: center;\n",
                            "  /* unfitted */\n",
                            "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
                            "  color: var(--sklearn-color-unfitted-level-1);\n",
                            "}\n",
                            "\n",
                            ".sk-estimator-doc-link.fitted,\n",
                            "a:link.sk-estimator-doc-link.fitted,\n",
                            "a:visited.sk-estimator-doc-link.fitted {\n",
                            "  /* fitted */\n",
                            "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
                            "  color: var(--sklearn-color-fitted-level-1);\n",
                            "}\n",
                            "\n",
                            "/* On hover */\n",
                            "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
                            ".sk-estimator-doc-link:hover,\n",
                            "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
                            ".sk-estimator-doc-link:hover {\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-3);\n",
                            "  color: var(--sklearn-color-background);\n",
                            "  text-decoration: none;\n",
                            "}\n",
                            "\n",
                            "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
                            ".sk-estimator-doc-link.fitted:hover,\n",
                            "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
                            ".sk-estimator-doc-link.fitted:hover {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-3);\n",
                            "  color: var(--sklearn-color-background);\n",
                            "  text-decoration: none;\n",
                            "}\n",
                            "\n",
                            "/* Span, style for the box shown on hovering the info icon */\n",
                            ".sk-estimator-doc-link span {\n",
                            "  display: none;\n",
                            "  z-index: 9999;\n",
                            "  position: relative;\n",
                            "  font-weight: normal;\n",
                            "  right: .2ex;\n",
                            "  padding: .5ex;\n",
                            "  margin: .5ex;\n",
                            "  width: min-content;\n",
                            "  min-width: 20ex;\n",
                            "  max-width: 50ex;\n",
                            "  color: var(--sklearn-color-text);\n",
                            "  box-shadow: 2pt 2pt 4pt #999;\n",
                            "  /* unfitted */\n",
                            "  background: var(--sklearn-color-unfitted-level-0);\n",
                            "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
                            "}\n",
                            "\n",
                            ".sk-estimator-doc-link.fitted span {\n",
                            "  /* fitted */\n",
                            "  background: var(--sklearn-color-fitted-level-0);\n",
                            "  border: var(--sklearn-color-fitted-level-3);\n",
                            "}\n",
                            "\n",
                            ".sk-estimator-doc-link:hover span {\n",
                            "  display: block;\n",
                            "}\n",
                            "\n",
                            "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
                            "\n",
                            "#sk-container-id-1 a.estimator_doc_link {\n",
                            "  float: right;\n",
                            "  font-size: 1rem;\n",
                            "  line-height: 1em;\n",
                            "  font-family: monospace;\n",
                            "  background-color: var(--sklearn-color-background);\n",
                            "  border-radius: 1rem;\n",
                            "  height: 1rem;\n",
                            "  width: 1rem;\n",
                            "  text-decoration: none;\n",
                            "  /* unfitted */\n",
                            "  color: var(--sklearn-color-unfitted-level-1);\n",
                            "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
                            "  /* fitted */\n",
                            "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
                            "  color: var(--sklearn-color-fitted-level-1);\n",
                            "}\n",
                            "\n",
                            "/* On hover */\n",
                            "#sk-container-id-1 a.estimator_doc_link:hover {\n",
                            "  /* unfitted */\n",
                            "  background-color: var(--sklearn-color-unfitted-level-3);\n",
                            "  color: var(--sklearn-color-background);\n",
                            "  text-decoration: none;\n",
                            "}\n",
                            "\n",
                            "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
                            "  /* fitted */\n",
                            "  background-color: var(--sklearn-color-fitted-level-3);\n",
                            "}\n",
                            "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KMeans(init=array([[ 1],\n",
                            "       [ 2],\n",
                            "       [18]]), n_clusters=3, n_init=1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>KMeans</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.6/modules/generated/sklearn.cluster.KMeans.html\">?<span>Documentation for KMeans</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\"><pre>KMeans(init=array([[ 1],\n",
                            "       [ 2],\n",
                            "       [18]]), n_clusters=3, n_init=1)</pre></div> </div></div></div></div>"
                        ],
                        "text/plain": [
                            "KMeans(init=array([[ 1],\n",
                            "       [ 2],\n",
                            "       [18]]), n_clusters=3, n_init=1)"
                        ]
                    },
                    "execution_count": 5,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "#Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_x = np.array(x)\n",
                "data_x = data_x.reshape(-1,1)\n",
                "\n",
                "# Fitting\n",
                "kmeans.fit(data_x)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "id": "72efd783",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array([[ 1.],\n",
                            "       [ 2.],\n",
                            "       [23.]])"
                        ]
                    },
                    "execution_count": 6,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "#Centroides finaux\n",
                "final_centroids =  kmeans.cluster_centers_\n",
                "labels = kmeans.labels_\n",
                "\n",
                "final_centroids"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "id": "3110c8ca",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 13684 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Représentation Graphique\n",
                "plt.scatter(x, y, c=labels, cmap='viridis')\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "a24927bc",
            "metadata": {},
            "source": [
                "### K-means : Question 2"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "c18297ba",
            "metadata": {},
            "source": [
                "**Déterminer la partition optimale par k-means en prenant pour centres initiaux les éléments 18, 20, 31**"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "id": "d0ccbcf3-a06f-4757-bdd8-2cc3bd1626c6",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array([[ 1.5],\n",
                            "       [19. ],\n",
                            "       [31. ]])"
                        ]
                    },
                    "execution_count": 8,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "#Définition des centres initiaux\n",
                "init_points = np.array([18, 20, 31])\n",
                "\n",
                "#Itinitialisation algo\n",
                "kmeans = KMeans(init=init_points.reshape(-1,1),\n",
                "                n_clusters=3,\n",
                "                n_init = 1)\n",
                "\n",
                "#Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_x = np.array(x)\n",
                "data_x = data_x.reshape(-1,1)\n",
                "\n",
                "# Fitting\n",
                "kmeans.fit(data_x)\n",
                "\n",
                "#Centroides finaux\n",
                "final_centroids =  kmeans.cluster_centers_\n",
                "labels = kmeans.labels_\n",
                "\n",
                "final_centroids"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "b957bbe8",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 13716 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Représentation Graphique\n",
                "plt.scatter(x, y, c=labels, cmap='viridis')\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "2b85bc73",
            "metadata": {},
            "source": [
                "### K-means : Question 3"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "0c085473",
            "metadata": {},
            "source": [
                "**Déterminer la partition optimale par k-means en prenant comme partition initiale {{1},{2,18},{20,31}}**"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "id": "0047b80a",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array([[ 1.5],\n",
                            "       [19. ],\n",
                            "       [31. ]])"
                        ]
                    },
                    "execution_count": 10,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "#Définition des centres initiaux\n",
                "init_points = np.array([1, np.mean([2, 18]), np.mean([20, 31])])\n",
                "\n",
                "#Itinitialisation algo\n",
                "kmeans = KMeans(init=init_points.reshape(-1,1),\n",
                "                n_clusters=3,\n",
                "                n_init=1)\n",
                "\n",
                "#Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_x = np.array(x)\n",
                "data_x = data_x.reshape(-1,1)\n",
                "\n",
                "# Fitting\n",
                "kmeans.fit(data_x)\n",
                "\n",
                "#Centroides finaux\n",
                "final_centroids =  kmeans.cluster_centers_\n",
                "labels = kmeans.labels_\n",
                "\n",
                "final_centroids"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "id": "d0776ee3",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 13716 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Représentation Graphique\n",
                "plt.scatter(x, y, c=labels, cmap='viridis')\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "5eaad20e",
            "metadata": {},
            "source": [
                "### Classification Ascendante Hiérarchique avec le lien simple"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "id": "1ebaaa05",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 13864 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Défintion de E\n",
                "x = [0, 2, 5, 8, 9, 13, 18]\n",
                "\n",
                "#Représentation graphique\n",
                "y=[0, 0, 0, 0, 0, 0, 0]\n",
                "plt.scatter(x, y)\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "id": "5e96f7f3",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 10004 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Calcul de CAH avec lien simple\n",
                "data = list(zip(x, strict=False))\n",
                "\n",
                "linkage_data = linkage(data, method='single', metric='euclidean')\n",
                "dendrogram(linkage_data, labels=x)\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "874c878c",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[0 0 0 0 0 2 1]\n"
                    ]
                },
                {
                    "data": {
                        "image/png": "[encoded data, 15588 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Calcul de la partition de l'espace\n",
                "hierarchical_cluster = AgglomerativeClustering(n_clusters=3, metric='euclidean', linkage='single')\n",
                "\n",
                "labels = hierarchical_cluster.fit_predict(data)\n",
                "print(labels)\n",
                "\n",
                "#Représentation Graphique\n",
                "plt.scatter(x, y, c=labels, cmap='viridis')\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "75420ae4",
            "metadata": {},
            "source": [
                "### Classification Ascendante Hiérarchique avec le lien complet"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 15,
            "id": "8f098bc3",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 15116 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Calcul de CAH avec lien complet\n",
                "data = list(zip(x, strict=False))\n",
                "\n",
                "linkage_data = linkage(data, method='complete', metric='euclidean')\n",
                "dendrogram(linkage_data, labels=x)\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "68ccdf5c",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "[2 2 1 1 1 0 0]\n"
                    ]
                },
                {
                    "data": {
                        "image/png": "[encoded data, 15556 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Calcul de la partition de l'espace\n",
                "hierarchical_cluster = AgglomerativeClustering(n_clusters=3, metric='euclidean', linkage='complete')\n",
                "\n",
                "labels = hierarchical_cluster.fit_predict(data)\n",
                "print(labels)\n",
                "\n",
                "#Représentation Graphique\n",
                "plt.scatter(x, y, c=labels, cmap='viridis')\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "99bc3508",
            "metadata": {},
            "source": [
                "## K-means: Cas pratique"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "b2b035d2",
            "metadata": {},
            "source": [
                "### Import des données"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 17,
            "id": "8051b5f4",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.microsoft.datawrangler.viewer.v0+json": {
                            "columns": [
                                {
                                    "name": "index",
                                    "rawType": "int64",
                                    "type": "integer"
                                },
                                {
                                    "name": "ZONE_RISQUE",
                                    "rawType": "object",
                                    "type": "string"
                                },
                                {
                                    "name": "NB",
                                    "rawType": "int64",
                                    "type": "integer"
                                },
                                {
                                    "name": "CHARGE",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "EXPO",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "FREQ",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "CM",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "FREQxCHARGE",
                                    "rawType": "float64",
                                    "type": "float"
                                }
                            ],
                            "ref": "96564cd0-30e4-4347-a31a-4e7519cd4c0b",
                            "rows": [
                                [
                                    "0",
                                    "A",
                                    "236",
                                    "1174911.57",
                                    "986430.8907103825",
                                    "0.00023924636000606546",
                                    "4978.438855932203",
                                    "281.0933164515116"
                                ],
                                [
                                    "1",
                                    "B",
                                    "181",
                                    "1146823.23",
                                    "909243.3551912569",
                                    "0.00019906661837735087",
                                    "6336.039944751381",
                                    "228.2942222726909"
                                ],
                                [
                                    "2",
                                    "C",
                                    "284",
                                    "1926300.0",
                                    "1356654.5300546447",
                                    "0.00020933848205892237",
                                    "6782.7464788732395",
                                    "403.24871799010214"
                                ],
                                [
                                    "3",
                                    "D",
                                    "83",
                                    "455800.64",
                                    "314767.9918032787",
                                    "0.00026368627738957876",
                                    "5491.573975903615",
                                    "120.18837399338753"
                                ],
                                [
                                    "4",
                                    "E",
                                    "20",
                                    "181747.82",
                                    "137874.1612021858",
                                    "0.00014505981269884912",
                                    "9087.391",
                                    "26.364304727624145"
                                ],
                                [
                                    "5",
                                    "F",
                                    "1",
                                    "19418.42",
                                    "12129.715846994535",
                                    "8.244216209300377e-05",
                                    "19418.42",
                                    "1.6008965292300261"
                                ],
                                [
                                    "6",
                                    "G",
                                    "5",
                                    "29820.87",
                                    "26935.5",
                                    "0.000185628631360101",
                                    "5964.174",
                                    "5.535607284067495"
                                ],
                                [
                                    "7",
                                    "H",
                                    "14",
                                    "86476.9",
                                    "46174.15300546448",
                                    "0.00030319993088651065",
                                    "6176.921428571428",
                                    "26.219790103279692"
                                ],
                                [
                                    "8",
                                    "I",
                                    "4",
                                    "32686.63",
                                    "13981.37431693989",
                                    "0.0002860949080773543",
                                    "8171.6575",
                                    "9.351478405208491"
                                ],
                                [
                                    "9",
                                    "J",
                                    "12",
                                    "75580.99",
                                    "28698.71857923497",
                                    "0.00041813713622330966",
                                    "6298.4158333333335",
                                    "31.603218711522608"
                                ],
                                [
                                    "10",
                                    "K",
                                    "17",
                                    "87411.64",
                                    "72896.8743169399",
                                    "0.00023320615814181092",
                                    "5141.861176470588",
                                    "20.384932741275044"
                                ],
                                [
                                    "11",
                                    "L",
                                    "7",
                                    "22684.079999999998",
                                    "24877.61202185792",
                                    "0.0002813774888783406",
                                    "3240.582857142857",
                                    "6.382789467915388"
                                ],
                                [
                                    "12",
                                    "M",
                                    "1",
                                    "6352.66",
                                    "1010.7759562841533",
                                    "0.0009893389269727308",
                                    "6352.66",
                                    "6.284933827822588"
                                ],
                                [
                                    "13",
                                    "R",
                                    "0",
                                    "0.0",
                                    "729.1994535519125",
                                    "0.0",
                                    "0.0",
                                    "0.0"
                                ],
                                [
                                    "14",
                                    "S",
                                    "0",
                                    "0.0",
                                    "1783.620218579235",
                                    "0.0",
                                    "0.0",
                                    "0.0"
                                ],
                                [
                                    "15",
                                    "T",
                                    "1",
                                    "3053.17",
                                    "3044.155737704918",
                                    "0.00032849830500259836",
                                    "3053.17",
                                    "1.0029611698847833"
                                ],
                                [
                                    "16",
                                    "X",
                                    "0",
                                    "0.0",
                                    "1627.1803278688521",
                                    "0.0",
                                    "0.0",
                                    "0.0"
                                ]
                            ],
                            "shape": {
                                "columns": 7,
                                "rows": 17
                            }
                        },
                        "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>ZONE_RISQUE</th>\n",
                            "      <th>NB</th>\n",
                            "      <th>CHARGE</th>\n",
                            "      <th>EXPO</th>\n",
                            "      <th>FREQ</th>\n",
                            "      <th>CM</th>\n",
                            "      <th>FREQxCHARGE</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>A</td>\n",
                            "      <td>236</td>\n",
                            "      <td>1174911.57</td>\n",
                            "      <td>9.864309e+05</td>\n",
                            "      <td>0.000239</td>\n",
                            "      <td>4978.438856</td>\n",
                            "      <td>281.093316</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>B</td>\n",
                            "      <td>181</td>\n",
                            "      <td>1146823.23</td>\n",
                            "      <td>9.092434e+05</td>\n",
                            "      <td>0.000199</td>\n",
                            "      <td>6336.039945</td>\n",
                            "      <td>228.294222</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>C</td>\n",
                            "      <td>284</td>\n",
                            "      <td>1926300.00</td>\n",
                            "      <td>1.356655e+06</td>\n",
                            "      <td>0.000209</td>\n",
                            "      <td>6782.746479</td>\n",
                            "      <td>403.248718</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>D</td>\n",
                            "      <td>83</td>\n",
                            "      <td>455800.64</td>\n",
                            "      <td>3.147680e+05</td>\n",
                            "      <td>0.000264</td>\n",
                            "      <td>5491.573976</td>\n",
                            "      <td>120.188374</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>E</td>\n",
                            "      <td>20</td>\n",
                            "      <td>181747.82</td>\n",
                            "      <td>1.378742e+05</td>\n",
                            "      <td>0.000145</td>\n",
                            "      <td>9087.391000</td>\n",
                            "      <td>26.364305</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>5</th>\n",
                            "      <td>F</td>\n",
                            "      <td>1</td>\n",
                            "      <td>19418.42</td>\n",
                            "      <td>1.212972e+04</td>\n",
                            "      <td>0.000082</td>\n",
                            "      <td>19418.420000</td>\n",
                            "      <td>1.600897</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>6</th>\n",
                            "      <td>G</td>\n",
                            "      <td>5</td>\n",
                            "      <td>29820.87</td>\n",
                            "      <td>2.693550e+04</td>\n",
                            "      <td>0.000186</td>\n",
                            "      <td>5964.174000</td>\n",
                            "      <td>5.535607</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>7</th>\n",
                            "      <td>H</td>\n",
                            "      <td>14</td>\n",
                            "      <td>86476.90</td>\n",
                            "      <td>4.617415e+04</td>\n",
                            "      <td>0.000303</td>\n",
                            "      <td>6176.921429</td>\n",
                            "      <td>26.219790</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>8</th>\n",
                            "      <td>I</td>\n",
                            "      <td>4</td>\n",
                            "      <td>32686.63</td>\n",
                            "      <td>1.398137e+04</td>\n",
                            "      <td>0.000286</td>\n",
                            "      <td>8171.657500</td>\n",
                            "      <td>9.351478</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>9</th>\n",
                            "      <td>J</td>\n",
                            "      <td>12</td>\n",
                            "      <td>75580.99</td>\n",
                            "      <td>2.869872e+04</td>\n",
                            "      <td>0.000418</td>\n",
                            "      <td>6298.415833</td>\n",
                            "      <td>31.603219</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>10</th>\n",
                            "      <td>K</td>\n",
                            "      <td>17</td>\n",
                            "      <td>87411.64</td>\n",
                            "      <td>7.289687e+04</td>\n",
                            "      <td>0.000233</td>\n",
                            "      <td>5141.861176</td>\n",
                            "      <td>20.384933</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>11</th>\n",
                            "      <td>L</td>\n",
                            "      <td>7</td>\n",
                            "      <td>22684.08</td>\n",
                            "      <td>2.487761e+04</td>\n",
                            "      <td>0.000281</td>\n",
                            "      <td>3240.582857</td>\n",
                            "      <td>6.382789</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>12</th>\n",
                            "      <td>M</td>\n",
                            "      <td>1</td>\n",
                            "      <td>6352.66</td>\n",
                            "      <td>1.010776e+03</td>\n",
                            "      <td>0.000989</td>\n",
                            "      <td>6352.660000</td>\n",
                            "      <td>6.284934</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>13</th>\n",
                            "      <td>R</td>\n",
                            "      <td>0</td>\n",
                            "      <td>0.00</td>\n",
                            "      <td>7.291995e+02</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>14</th>\n",
                            "      <td>S</td>\n",
                            "      <td>0</td>\n",
                            "      <td>0.00</td>\n",
                            "      <td>1.783620e+03</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>15</th>\n",
                            "      <td>T</td>\n",
                            "      <td>1</td>\n",
                            "      <td>3053.17</td>\n",
                            "      <td>3.044156e+03</td>\n",
                            "      <td>0.000328</td>\n",
                            "      <td>3053.170000</td>\n",
                            "      <td>1.002961</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>16</th>\n",
                            "      <td>X</td>\n",
                            "      <td>0</td>\n",
                            "      <td>0.00</td>\n",
                            "      <td>1.627180e+03</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "      <td>0.000000</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "   ZONE_RISQUE   NB      CHARGE          EXPO      FREQ            CM  \\\n",
                            "0            A  236  1174911.57  9.864309e+05  0.000239   4978.438856   \n",
                            "1            B  181  1146823.23  9.092434e+05  0.000199   6336.039945   \n",
                            "2            C  284  1926300.00  1.356655e+06  0.000209   6782.746479   \n",
                            "3            D   83   455800.64  3.147680e+05  0.000264   5491.573976   \n",
                            "4            E   20   181747.82  1.378742e+05  0.000145   9087.391000   \n",
                            "5            F    1    19418.42  1.212972e+04  0.000082  19418.420000   \n",
                            "6            G    5    29820.87  2.693550e+04  0.000186   5964.174000   \n",
                            "7            H   14    86476.90  4.617415e+04  0.000303   6176.921429   \n",
                            "8            I    4    32686.63  1.398137e+04  0.000286   8171.657500   \n",
                            "9            J   12    75580.99  2.869872e+04  0.000418   6298.415833   \n",
                            "10           K   17    87411.64  7.289687e+04  0.000233   5141.861176   \n",
                            "11           L    7    22684.08  2.487761e+04  0.000281   3240.582857   \n",
                            "12           M    1     6352.66  1.010776e+03  0.000989   6352.660000   \n",
                            "13           R    0        0.00  7.291995e+02  0.000000      0.000000   \n",
                            "14           S    0        0.00  1.783620e+03  0.000000      0.000000   \n",
                            "15           T    1     3053.17  3.044156e+03  0.000328   3053.170000   \n",
                            "16           X    0        0.00  1.627180e+03  0.000000      0.000000   \n",
                            "\n",
                            "    FREQxCHARGE  \n",
                            "0    281.093316  \n",
                            "1    228.294222  \n",
                            "2    403.248718  \n",
                            "3    120.188374  \n",
                            "4     26.364305  \n",
                            "5      1.600897  \n",
                            "6      5.535607  \n",
                            "7     26.219790  \n",
                            "8      9.351478  \n",
                            "9     31.603219  \n",
                            "10    20.384933  \n",
                            "11     6.382789  \n",
                            "12     6.284934  \n",
                            "13     0.000000  \n",
                            "14     0.000000  \n",
                            "15     1.002961  \n",
                            "16     0.000000  "
                        ]
                    },
                    "execution_count": 17,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "path = input_path + '/base_retraitee.csv'\n",
                "data_retraitee = pd.read_csv(path, sep=\",\", decimal=\".\")\n",
                "\n",
                "# Group by ZONE_RISQUE and aggregate the necessary columns\n",
                "data = data_retraitee.groupby([\"ZONE_RISQUE\"], as_index=False).agg({\n",
                "    'NB': 'sum',\n",
                "    'CHARGE': 'sum',\n",
                "    'EXPO': 'sum'\n",
                "})\n",
                "\n",
                "# Calculate derived metrics\n",
                "data[\"FREQ\"] = data[\"NB\"] / data[\"EXPO\"]\n",
                "data[\"FREQ\"] = data[\"FREQ\"].fillna(0)\n",
                "\n",
                "data[\"CM\"] = data[\"CHARGE\"] / data[\"NB\"]\n",
                "data[\"CM\"] = data[\"CM\"].fillna(0)\n",
                "\n",
                "data[\"FREQxCHARGE\"] = data[\"FREQ\"] * data[\"CHARGE\"]\n",
                "data[\"FREQxCHARGE\"] = data[\"FREQxCHARGE\"].fillna(0)\n",
                "\n",
                "data"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "aeff9cff",
            "metadata": {},
            "source": [
                "**Exercice :** Regrouper les zones géographiques en 5 zones homogènes en termes :\n",
                "* Fréquence de sinistres (La fréquence est égale au Nombre de sinistres divisé par l'exposition)\n",
                "* Charge \n",
                "* Fréquence de sinistres x Charge  \n",
                "  \n",
                "A chaque fois :\n",
                "* Afficher les coordonnées des centroïdes\n",
                "* Représenter graphiquement la partition obtenue"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "1c4333b8",
            "metadata": {},
            "source": [
                "### Regroupement de zones selon la fréquence"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 18,
            "id": "6e35f286",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Coordonnées des centroïdes : \n",
                        "[[1.45059813e-04]\n",
                        " [2.06105405e-05]\n",
                        " [9.89338927e-04]\n",
                        " [3.13499008e-04]\n",
                        " [2.13297250e-04]]\n",
                        "Nombre d'itérations réalisées : \n",
                        "3\n",
                        "Partition : \n",
                        "[4 4 4 3 0 1 4 3 3 3 4 3 2 1 1 3 1]\n"
                    ]
                }
            ],
            "source": [
                "# Initialisation de l'algorithme\n",
                "kmeans_FREQ = KMeans(init='random', n_clusters=5, n_init=1, random_state=42, max_iter=300)\n",
                "\n",
                "# Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_freq = data[\"FREQ\"].to_numpy()\n",
                "data_freq = data_freq.reshape(-1, 1)\n",
                "\n",
                "# Fitting\n",
                "kmeans_FREQ.fit(data_freq)\n",
                "\n",
                "# Centroïdes\n",
                "print(\"Coordonnées des centroïdes : \")\n",
                "print(kmeans_FREQ.cluster_centers_)\n",
                "# Itérations nécessaire pour la résolution\n",
                "print(\"Nombre d'itérations réalisées : \")\n",
                "print(kmeans_FREQ.n_iter_)\n",
                "# Classes\n",
                "print(\"Partition : \")\n",
                "print(kmeans_FREQ.labels_)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 19,
            "id": "277e3d46",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.plotly.v1+json": {
                            "config": {
                                "plotlyServerURL": "https://plot.ly"
                            },
                            "data": [
                                {
                                    "hovertemplate": "Cluster_FREQ=4<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "4",
                                    "marker": {
                                        "color": "#636efa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "4",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "DKJ1lcZbLz+MRDc7kRcqP8dbVN47cCs/rNRBzalUKD/efhelGZEuPw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAAA=",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=3<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "3",
                                    "marker": {
                                        "color": "#EF553B",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "3",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "uSYT8OtHMT/+84zJ2d4zP04cFEbgvzI/BVFIUy1nOz8N79Acu3AyP6oYDnpJhzU/",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAAAA",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=0<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "0",
                                    "marker": {
                                        "color": "#00cc96",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "0",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ER+STWYDIz8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=1<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "1",
                                    "marker": {
                                        "color": "#ab63fa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "1",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "3mpcj5mcFT8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=2<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "2",
                                    "marker": {
                                        "color": "#FFA15A",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "2",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ESaBlZY1UD8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                }
                            ],
                            "layout": {
                                "legend": {
                                    "title": {
                                        "text": "Cluster_FREQ"
                                    },
                                    "tracegroupgap": 0
                                },
                                "margin": {
                                    "t": 60
                                },
                                "template": {
                                    "data": {
                                        "bar": [
                                            {
                                                "error_x": {
                                                    "color": "#2a3f5f"
                                                },
                                                "error_y": {
                                                    "color": "#2a3f5f"
                                                },
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "bar"
                                            }
                                        ],
                                        "barpolar": [
                                            {
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "barpolar"
                                            }
                                        ],
                                        "carpet": [
                                            {
                                                "aaxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "baxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "type": "carpet"
                                            }
                                        ],
                                        "choropleth": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "choropleth"
                                            }
                                        ],
                                        "contour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "contour"
                                            }
                                        ],
                                        "contourcarpet": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "contourcarpet"
                                            }
                                        ],
                                        "heatmap": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "heatmap"
                                            }
                                        ],
                                        "histogram": [
                                            {
                                                "marker": {
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "histogram"
                                            }
                                        ],
                                        "histogram2d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2d"
                                            }
                                        ],
                                        "histogram2dcontour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2dcontour"
                                            }
                                        ],
                                        "mesh3d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "mesh3d"
                                            }
                                        ],
                                        "parcoords": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "parcoords"
                                            }
                                        ],
                                        "pie": [
                                            {
                                                "automargin": true,
                                                "type": "pie"
                                            }
                                        ],
                                        "scatter": [
                                            {
                                                "fillpattern": {
                                                    "fillmode": "overlay",
                                                    "size": 10,
                                                    "solidity": 0.2
                                                },
                                                "type": "scatter"
                                            }
                                        ],
                                        "scatter3d": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatter3d"
                                            }
                                        ],
                                        "scattercarpet": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattercarpet"
                                            }
                                        ],
                                        "scattergeo": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergeo"
                                            }
                                        ],
                                        "scattergl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergl"
                                            }
                                        ],
                                        "scattermap": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermap"
                                            }
                                        ],
                                        "scattermapbox": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermapbox"
                                            }
                                        ],
                                        "scatterpolar": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolar"
                                            }
                                        ],
                                        "scatterpolargl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolargl"
                                            }
                                        ],
                                        "scatterternary": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterternary"
                                            }
                                        ],
                                        "surface": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "surface"
                                            }
                                        ],
                                        "table": [
                                            {
                                                "cells": {
                                                    "fill": {
                                                        "color": "#EBF0F8"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "header": {
                                                    "fill": {
                                                        "color": "#C8D4E3"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "type": "table"
                                            }
                                        ]
                                    },
                                    "layout": {
                                        "annotationdefaults": {
                                            "arrowcolor": "#2a3f5f",
                                            "arrowhead": 0,
                                            "arrowwidth": 1
                                        },
                                        "autotypenumbers": "strict",
                                        "coloraxis": {
                                            "colorbar": {
                                                "outlinewidth": 0,
                                                "ticks": ""
                                            }
                                        },
                                        "colorscale": {
                                            "diverging": [
                                                [
                                                    0,
                                                    "#8e0152"
                                                ],
                                                [
                                                    0.1,
                                                    "#c51b7d"
                                                ],
                                                [
                                                    0.2,
                                                    "#de77ae"
                                                ],
                                                [
                                                    0.3,
                                                    "#f1b6da"
                                                ],
                                                [
                                                    0.4,
                                                    "#fde0ef"
                                                ],
                                                [
                                                    0.5,
                                                    "#f7f7f7"
                                                ],
                                                [
                                                    0.6,
                                                    "#e6f5d0"
                                                ],
                                                [
                                                    0.7,
                                                    "#b8e186"
                                                ],
                                                [
                                                    0.8,
                                                    "#7fbc41"
                                                ],
                                                [
                                                    0.9,
                                                    "#4d9221"
                                                ],
                                                [
                                                    1,
                                                    "#276419"
                                                ]
                                            ],
                                            "sequential": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ],
                                            "sequentialminus": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ]
                                        },
                                        "colorway": [
                                            "#636efa",
                                            "#EF553B",
                                            "#00cc96",
                                            "#ab63fa",
                                            "#FFA15A",
                                            "#19d3f3",
                                            "#FF6692",
                                            "#B6E880",
                                            "#FF97FF",
                                            "#FECB52"
                                        ],
                                        "font": {
                                            "color": "#2a3f5f"
                                        },
                                        "geo": {
                                            "bgcolor": "white",
                                            "lakecolor": "white",
                                            "landcolor": "#E5ECF6",
                                            "showlakes": true,
                                            "showland": true,
                                            "subunitcolor": "white"
                                        },
                                        "hoverlabel": {
                                            "align": "left"
                                        },
                                        "hovermode": "closest",
                                        "mapbox": {
                                            "style": "light"
                                        },
                                        "paper_bgcolor": "white",
                                        "plot_bgcolor": "#E5ECF6",
                                        "polar": {
                                            "angularaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "radialaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "scene": {
                                            "xaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "yaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "zaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            }
                                        },
                                        "shapedefaults": {
                                            "line": {
                                                "color": "#2a3f5f"
                                            }
                                        },
                                        "ternary": {
                                            "aaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "baxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "caxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "title": {
                                            "x": 0.05
                                        },
                                        "xaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        },
                                        "yaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        }
                                    }
                                },
                                "xaxis": {
                                    "anchor": "y",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "FREQ"
                                    }
                                },
                                "yaxis": {
                                    "anchor": "x",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "Y"
                                    }
                                }
                            }
                        }
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Visualisation\n",
                "data[\"Cluster_FREQ\"] = kmeans_FREQ.labels_\n",
                "data[\"Cluster_FREQ\"] = data[\"Cluster_FREQ\"].astype(str)\n",
                "data[\"Y\"] = 0\n",
                "\n",
                "fig = px.scatter(data, x=\"FREQ\", y=\"Y\", color=\"Cluster_FREQ\")\n",
                "fig.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "9c738659",
            "metadata": {},
            "source": [
                "### Regroupement de zones selon le coût moyen"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 20,
            "id": "f461bfb8",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Coordonnées des centroïdes : \n",
                        "[[ 5946.98129932]\n",
                        " [    0.        ]\n",
                        " [ 8629.52425   ]\n",
                        " [ 3146.87642857]\n",
                        " [19418.42      ]]\n",
                        "Nombre d'itérations réalisées : \n",
                        "6\n",
                        "Partition : \n",
                        "[0 0 0 0 2 4 0 0 2 0 0 3 0 1 1 3 1]\n"
                    ]
                }
            ],
            "source": [
                "# Initialisation de l'algorithme\n",
                "kmeans_CM = KMeans(init='random', n_clusters=5, n_init=1, random_state=42)\n",
                "\n",
                "# Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_cm = data['CM'].to_numpy()\n",
                "data_cm = data_cm.reshape(-1,1)\n",
                "\n",
                "# Fitting\n",
                "kmeans_CM.fit(data_cm)\n",
                "\n",
                "# Centroïdes\n",
                "print(\"Coordonnées des centroïdes : \")\n",
                "print(kmeans_CM.cluster_centers_)\n",
                "# Itérations nécessaire pour la résolution\n",
                "print(\"Nombre d'itérations réalisées : \")\n",
                "print(kmeans_CM.n_iter_)\n",
                "# Classes\n",
                "print(\"Partition : \")\n",
                "print(kmeans_CM.labels_)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 21,
            "id": "98b2a0bf",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.plotly.v1+json": {
                            "config": {
                                "plotlyServerURL": "https://plot.ly"
                            },
                            "data": [
                                {
                                    "hovertemplate": "Cluster_CM=0<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "0",
                                    "marker": {
                                        "color": "#636efa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "0",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "DKJ1lcZbLz+MRDc7kRcqP8dbVN47cCs/uSYT8OtHMT+s1EHNqVQoP/7zjMnZ3jM/BVFIUy1nOz/efhelGZEuPxEmgZWWNVA/",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAAAAAAAA",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_CM=2<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "2",
                                    "marker": {
                                        "color": "#EF553B",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "2",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ER+STWYDIz9OHBRG4L8yPw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAA=",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_CM=4<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "4",
                                    "marker": {
                                        "color": "#00cc96",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "4",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "3mpcj5mcFT8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_CM=3<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "3",
                                    "marker": {
                                        "color": "#ab63fa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "3",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "De/QHLtwMj+qGA56SYc1Pw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAA=",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_CM=1<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "1",
                                    "marker": {
                                        "color": "#FFA15A",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "1",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAA",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                }
                            ],
                            "layout": {
                                "legend": {
                                    "title": {
                                        "text": "Cluster_CM"
                                    },
                                    "tracegroupgap": 0
                                },
                                "margin": {
                                    "t": 60
                                },
                                "template": {
                                    "data": {
                                        "bar": [
                                            {
                                                "error_x": {
                                                    "color": "#2a3f5f"
                                                },
                                                "error_y": {
                                                    "color": "#2a3f5f"
                                                },
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "bar"
                                            }
                                        ],
                                        "barpolar": [
                                            {
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "barpolar"
                                            }
                                        ],
                                        "carpet": [
                                            {
                                                "aaxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "baxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "type": "carpet"
                                            }
                                        ],
                                        "choropleth": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "choropleth"
                                            }
                                        ],
                                        "contour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "contour"
                                            }
                                        ],
                                        "contourcarpet": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "contourcarpet"
                                            }
                                        ],
                                        "heatmap": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "heatmap"
                                            }
                                        ],
                                        "histogram": [
                                            {
                                                "marker": {
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "histogram"
                                            }
                                        ],
                                        "histogram2d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2d"
                                            }
                                        ],
                                        "histogram2dcontour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2dcontour"
                                            }
                                        ],
                                        "mesh3d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "mesh3d"
                                            }
                                        ],
                                        "parcoords": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "parcoords"
                                            }
                                        ],
                                        "pie": [
                                            {
                                                "automargin": true,
                                                "type": "pie"
                                            }
                                        ],
                                        "scatter": [
                                            {
                                                "fillpattern": {
                                                    "fillmode": "overlay",
                                                    "size": 10,
                                                    "solidity": 0.2
                                                },
                                                "type": "scatter"
                                            }
                                        ],
                                        "scatter3d": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatter3d"
                                            }
                                        ],
                                        "scattercarpet": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattercarpet"
                                            }
                                        ],
                                        "scattergeo": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergeo"
                                            }
                                        ],
                                        "scattergl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergl"
                                            }
                                        ],
                                        "scattermap": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermap"
                                            }
                                        ],
                                        "scattermapbox": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermapbox"
                                            }
                                        ],
                                        "scatterpolar": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolar"
                                            }
                                        ],
                                        "scatterpolargl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolargl"
                                            }
                                        ],
                                        "scatterternary": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterternary"
                                            }
                                        ],
                                        "surface": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "surface"
                                            }
                                        ],
                                        "table": [
                                            {
                                                "cells": {
                                                    "fill": {
                                                        "color": "#EBF0F8"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "header": {
                                                    "fill": {
                                                        "color": "#C8D4E3"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "type": "table"
                                            }
                                        ]
                                    },
                                    "layout": {
                                        "annotationdefaults": {
                                            "arrowcolor": "#2a3f5f",
                                            "arrowhead": 0,
                                            "arrowwidth": 1
                                        },
                                        "autotypenumbers": "strict",
                                        "coloraxis": {
                                            "colorbar": {
                                                "outlinewidth": 0,
                                                "ticks": ""
                                            }
                                        },
                                        "colorscale": {
                                            "diverging": [
                                                [
                                                    0,
                                                    "#8e0152"
                                                ],
                                                [
                                                    0.1,
                                                    "#c51b7d"
                                                ],
                                                [
                                                    0.2,
                                                    "#de77ae"
                                                ],
                                                [
                                                    0.3,
                                                    "#f1b6da"
                                                ],
                                                [
                                                    0.4,
                                                    "#fde0ef"
                                                ],
                                                [
                                                    0.5,
                                                    "#f7f7f7"
                                                ],
                                                [
                                                    0.6,
                                                    "#e6f5d0"
                                                ],
                                                [
                                                    0.7,
                                                    "#b8e186"
                                                ],
                                                [
                                                    0.8,
                                                    "#7fbc41"
                                                ],
                                                [
                                                    0.9,
                                                    "#4d9221"
                                                ],
                                                [
                                                    1,
                                                    "#276419"
                                                ]
                                            ],
                                            "sequential": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ],
                                            "sequentialminus": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ]
                                        },
                                        "colorway": [
                                            "#636efa",
                                            "#EF553B",
                                            "#00cc96",
                                            "#ab63fa",
                                            "#FFA15A",
                                            "#19d3f3",
                                            "#FF6692",
                                            "#B6E880",
                                            "#FF97FF",
                                            "#FECB52"
                                        ],
                                        "font": {
                                            "color": "#2a3f5f"
                                        },
                                        "geo": {
                                            "bgcolor": "white",
                                            "lakecolor": "white",
                                            "landcolor": "#E5ECF6",
                                            "showlakes": true,
                                            "showland": true,
                                            "subunitcolor": "white"
                                        },
                                        "hoverlabel": {
                                            "align": "left"
                                        },
                                        "hovermode": "closest",
                                        "mapbox": {
                                            "style": "light"
                                        },
                                        "paper_bgcolor": "white",
                                        "plot_bgcolor": "#E5ECF6",
                                        "polar": {
                                            "angularaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "radialaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "scene": {
                                            "xaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "yaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "zaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            }
                                        },
                                        "shapedefaults": {
                                            "line": {
                                                "color": "#2a3f5f"
                                            }
                                        },
                                        "ternary": {
                                            "aaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "baxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "caxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "title": {
                                            "x": 0.05
                                        },
                                        "xaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        },
                                        "yaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        }
                                    }
                                },
                                "xaxis": {
                                    "anchor": "y",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "FREQ"
                                    }
                                },
                                "yaxis": {
                                    "anchor": "x",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "Y"
                                    }
                                }
                            }
                        }
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Visualisation\n",
                "data[\"Cluster_CM\"] = kmeans_CM.labels_\n",
                "data[\"Cluster_CM\"] = data[\"Cluster_CM\"].astype(str)\n",
                "data[\"Y\"] = 0\n",
                "\n",
                "fig = px.scatter(data, x=\"FREQ\", y=\"Y\", color=\"Cluster_CM\")\n",
                "fig.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "6b154f4a",
            "metadata": {},
            "source": [
                "### Regroupement de zones selon (fréquence; le coût moyen)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 22,
            "id": "1d89f70e",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Coordonnées des centroïdes : \n",
                        "[[  6.88870225]\n",
                        " [  0.52077154]\n",
                        " [ 26.14306157]\n",
                        " [120.18837399]\n",
                        " [304.21208557]]\n",
                        "Nombre d'itérations réalisées : \n",
                        "4\n",
                        "Partition : \n",
                        "[4 4 4 3 2 1 0 2 0 2 2 0 0 1 1 1 1]\n"
                    ]
                }
            ],
            "source": [
                "# Initialisation de l'algorithme\n",
                "kmeans_FREQ_CM = KMeans(init='random', n_clusters=5, n_init=1, random_state=42, max_iter=300)\n",
                "\n",
                "# Transformation des données : plusieurs échantillons de 1 dimension\n",
                "data_x = data['FREQxCHARGE'].to_numpy()\n",
                "data_x = data_x.reshape(-1,1)\n",
                "\n",
                "# Fitting\n",
                "kmeans_FREQ_CM.fit(data_x)\n",
                "\n",
                "# Centroïdes\n",
                "print(\"Coordonnées des centroïdes : \")\n",
                "print(kmeans_FREQ_CM.cluster_centers_)\n",
                "# Itérations nécessaire pour la résolution\n",
                "print(\"Nombre d'itérations réalisées : \")\n",
                "print(kmeans_FREQ_CM.n_iter_)\n",
                "# Classes\n",
                "print(\"Partition : \")\n",
                "print(kmeans_FREQ_CM.labels_)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 23,
            "id": "48aa493b",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.plotly.v1+json": {
                            "config": {
                                "plotlyServerURL": "https://plot.ly"
                            },
                            "data": [
                                {
                                    "hovertemplate": "Cluster_FREQ_CM=4<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "4",
                                    "marker": {
                                        "color": "#636efa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "4",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "DKJ1lcZbLz+MRDc7kRcqP8dbVN47cCs/",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "H4XrkX/tMUGuR+E6x38xQQAAAACcZD1B",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CM=3<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "3",
                                    "marker": {
                                        "color": "#EF553B",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "3",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "uSYT8OtHMT8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "9ihcj+LRG0E=",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CM=2<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "2",
                                    "marker": {
                                        "color": "#00cc96",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "2",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ER+STWYDIz/+84zJ2d4zPwVRSFMtZzs/3n4XpRmRLj8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "9ihcj54vBkFmZmZmzhz1QHE9CtfPc/JA16NwPTpX9UA=",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CM=1<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "1",
                                    "marker": {
                                        "color": "#ab63fa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "1",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "3mpcj5mcFT8AAAAAAAAAAAAAAAAAAAAAqhgOekmHNT8AAAAAAAAAAA==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "FK5H4Zr20kAAAAAAAAAAAAAAAAAAAAAApHA9Clfap0AAAAAAAAAAAA==",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CM=0<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "0",
                                    "marker": {
                                        "color": "#FFA15A",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "0",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "rNRBzalUKD9OHBRG4L8yPw3v0By7cDI/ESaBlZY1UD8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "4XoUrjcf3UAfhetRqOvfQOtRuB4FJ9ZAXI/C9ajQuEA=",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                }
                            ],
                            "layout": {
                                "legend": {
                                    "title": {
                                        "text": "Cluster_FREQ_CM"
                                    },
                                    "tracegroupgap": 0
                                },
                                "margin": {
                                    "t": 60
                                },
                                "template": {
                                    "data": {
                                        "bar": [
                                            {
                                                "error_x": {
                                                    "color": "#2a3f5f"
                                                },
                                                "error_y": {
                                                    "color": "#2a3f5f"
                                                },
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "bar"
                                            }
                                        ],
                                        "barpolar": [
                                            {
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "barpolar"
                                            }
                                        ],
                                        "carpet": [
                                            {
                                                "aaxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "baxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "type": "carpet"
                                            }
                                        ],
                                        "choropleth": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "choropleth"
                                            }
                                        ],
                                        "contour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "contour"
                                            }
                                        ],
                                        "contourcarpet": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "contourcarpet"
                                            }
                                        ],
                                        "heatmap": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "heatmap"
                                            }
                                        ],
                                        "histogram": [
                                            {
                                                "marker": {
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "histogram"
                                            }
                                        ],
                                        "histogram2d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2d"
                                            }
                                        ],
                                        "histogram2dcontour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2dcontour"
                                            }
                                        ],
                                        "mesh3d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "mesh3d"
                                            }
                                        ],
                                        "parcoords": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "parcoords"
                                            }
                                        ],
                                        "pie": [
                                            {
                                                "automargin": true,
                                                "type": "pie"
                                            }
                                        ],
                                        "scatter": [
                                            {
                                                "fillpattern": {
                                                    "fillmode": "overlay",
                                                    "size": 10,
                                                    "solidity": 0.2
                                                },
                                                "type": "scatter"
                                            }
                                        ],
                                        "scatter3d": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatter3d"
                                            }
                                        ],
                                        "scattercarpet": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattercarpet"
                                            }
                                        ],
                                        "scattergeo": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergeo"
                                            }
                                        ],
                                        "scattergl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergl"
                                            }
                                        ],
                                        "scattermap": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermap"
                                            }
                                        ],
                                        "scattermapbox": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermapbox"
                                            }
                                        ],
                                        "scatterpolar": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolar"
                                            }
                                        ],
                                        "scatterpolargl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolargl"
                                            }
                                        ],
                                        "scatterternary": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterternary"
                                            }
                                        ],
                                        "surface": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "surface"
                                            }
                                        ],
                                        "table": [
                                            {
                                                "cells": {
                                                    "fill": {
                                                        "color": "#EBF0F8"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "header": {
                                                    "fill": {
                                                        "color": "#C8D4E3"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "type": "table"
                                            }
                                        ]
                                    },
                                    "layout": {
                                        "annotationdefaults": {
                                            "arrowcolor": "#2a3f5f",
                                            "arrowhead": 0,
                                            "arrowwidth": 1
                                        },
                                        "autotypenumbers": "strict",
                                        "coloraxis": {
                                            "colorbar": {
                                                "outlinewidth": 0,
                                                "ticks": ""
                                            }
                                        },
                                        "colorscale": {
                                            "diverging": [
                                                [
                                                    0,
                                                    "#8e0152"
                                                ],
                                                [
                                                    0.1,
                                                    "#c51b7d"
                                                ],
                                                [
                                                    0.2,
                                                    "#de77ae"
                                                ],
                                                [
                                                    0.3,
                                                    "#f1b6da"
                                                ],
                                                [
                                                    0.4,
                                                    "#fde0ef"
                                                ],
                                                [
                                                    0.5,
                                                    "#f7f7f7"
                                                ],
                                                [
                                                    0.6,
                                                    "#e6f5d0"
                                                ],
                                                [
                                                    0.7,
                                                    "#b8e186"
                                                ],
                                                [
                                                    0.8,
                                                    "#7fbc41"
                                                ],
                                                [
                                                    0.9,
                                                    "#4d9221"
                                                ],
                                                [
                                                    1,
                                                    "#276419"
                                                ]
                                            ],
                                            "sequential": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ],
                                            "sequentialminus": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ]
                                        },
                                        "colorway": [
                                            "#636efa",
                                            "#EF553B",
                                            "#00cc96",
                                            "#ab63fa",
                                            "#FFA15A",
                                            "#19d3f3",
                                            "#FF6692",
                                            "#B6E880",
                                            "#FF97FF",
                                            "#FECB52"
                                        ],
                                        "font": {
                                            "color": "#2a3f5f"
                                        },
                                        "geo": {
                                            "bgcolor": "white",
                                            "lakecolor": "white",
                                            "landcolor": "#E5ECF6",
                                            "showlakes": true,
                                            "showland": true,
                                            "subunitcolor": "white"
                                        },
                                        "hoverlabel": {
                                            "align": "left"
                                        },
                                        "hovermode": "closest",
                                        "mapbox": {
                                            "style": "light"
                                        },
                                        "paper_bgcolor": "white",
                                        "plot_bgcolor": "#E5ECF6",
                                        "polar": {
                                            "angularaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "radialaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "scene": {
                                            "xaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "yaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "zaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            }
                                        },
                                        "shapedefaults": {
                                            "line": {
                                                "color": "#2a3f5f"
                                            }
                                        },
                                        "ternary": {
                                            "aaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "baxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "caxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "title": {
                                            "x": 0.05
                                        },
                                        "xaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        },
                                        "yaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        }
                                    }
                                },
                                "xaxis": {
                                    "anchor": "y",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "FREQ"
                                    }
                                },
                                "yaxis": {
                                    "anchor": "x",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "CHARGE"
                                    }
                                }
                            }
                        }
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Visualisation\n",
                "data[\"Cluster_FREQ_CM\"] = kmeans_FREQ_CM.labels_\n",
                "data[\"Cluster_FREQ_CM\"] = data[\"Cluster_FREQ_CM\"].astype(str)\n",
                "\n",
                "fig = px.scatter(data, x=\"FREQ\", y=\"CHARGE\", color=\"Cluster_FREQ_CM\")\n",
                "fig.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "f1cac03f",
            "metadata": {},
            "source": [
                "## C.A.H : Cas pratique"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "bffff328",
            "metadata": {},
            "source": [
                "**Exercice :** Comparer les résultats obtenus via K-means à ceux d'une C.A.H (lien simple) pour la fréquence et (fréquence; coût moyen)\n",
                "  \n",
                "A chaque fois :\n",
                "* Tracer le dendrogramme associé\n",
                "* Représenter graphiquement la partition obtenue"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "8453bf02",
            "metadata": {},
            "source": [
                "### Regroupement de zones selon la fréquence"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 28,
            "id": "341bf2b2",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 17380 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "#Calcul de CAH avec lien simple\n",
                "data_x = data['FREQ'].to_numpy()\n",
                "data_x = data_x.reshape(-1,1)\n",
                "\n",
                "linkage_data = linkage(data_x, method='single', metric='euclidean')\n",
                "dendrogram(linkage_data, labels=np.array(data['ZONE_RISQUE']))\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "1f0c9650",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "   ZONE_RISQUE  Cluster\n",
                        "0            A        0\n",
                        "1            B        0\n",
                        "2            C        0\n",
                        "3            D        0\n",
                        "4            E        0\n",
                        "5            F        4\n",
                        "6            G        0\n",
                        "7            H        0\n",
                        "8            I        0\n",
                        "9            J        2\n",
                        "10           K        0\n",
                        "11           L        0\n",
                        "12           M        3\n",
                        "13           R        1\n",
                        "14           S        1\n",
                        "15           T        0\n",
                        "16           X        1\n"
                    ]
                }
            ],
            "source": [
                "# Calcul de la partition de l'espace\n",
                "hierarchical_cluster = AgglomerativeClustering(\n",
                "    n_clusters=5, metric=\"euclidean\", linkage=\"single\"\n",
                ")\n",
                "\n",
                "labels = hierarchical_cluster.fit_predict(data_x)\n",
                "print(pd.DataFrame({\"ZONE_RISQUE\": data['ZONE_RISQUE'], \"Cluster\": labels}))\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 34,
            "id": "3d77b67c",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.plotly.v1+json": {
                            "config": {
                                "plotlyServerURL": "https://plot.ly"
                            },
                            "data": [
                                {
                                    "hovertemplate": "Cluster_FREQ=0<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "0",
                                    "marker": {
                                        "color": "#636efa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "0",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "DKJ1lcZbLz+MRDc7kRcqP8dbVN47cCs/uSYT8OtHMT8RH5JNZgMjP6zUQc2pVCg//vOMydneMz9OHBRG4L8yP95+F6UZkS4/De/QHLtwMj+qGA56SYc1Pw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAAAAAAAAAAA=",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=4<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "4",
                                    "marker": {
                                        "color": "#EF553B",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "4",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "3mpcj5mcFT8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=2<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "2",
                                    "marker": {
                                        "color": "#00cc96",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "2",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "BVFIUy1nOz8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=3<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "3",
                                    "marker": {
                                        "color": "#ab63fa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "3",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ESaBlZY1UD8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AA==",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ=1<br>FREQ=%{x}<br>Y=%{y}<extra></extra>",
                                    "legendgroup": "1",
                                    "marker": {
                                        "color": "#FFA15A",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "1",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAA",
                                        "dtype": "i1"
                                    },
                                    "yaxis": "y"
                                }
                            ],
                            "layout": {
                                "legend": {
                                    "title": {
                                        "text": "Cluster_FREQ"
                                    },
                                    "tracegroupgap": 0
                                },
                                "margin": {
                                    "t": 60
                                },
                                "template": {
                                    "data": {
                                        "bar": [
                                            {
                                                "error_x": {
                                                    "color": "#2a3f5f"
                                                },
                                                "error_y": {
                                                    "color": "#2a3f5f"
                                                },
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "bar"
                                            }
                                        ],
                                        "barpolar": [
                                            {
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "barpolar"
                                            }
                                        ],
                                        "carpet": [
                                            {
                                                "aaxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "baxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "type": "carpet"
                                            }
                                        ],
                                        "choropleth": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "choropleth"
                                            }
                                        ],
                                        "contour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "contour"
                                            }
                                        ],
                                        "contourcarpet": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "contourcarpet"
                                            }
                                        ],
                                        "heatmap": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "heatmap"
                                            }
                                        ],
                                        "histogram": [
                                            {
                                                "marker": {
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "histogram"
                                            }
                                        ],
                                        "histogram2d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2d"
                                            }
                                        ],
                                        "histogram2dcontour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2dcontour"
                                            }
                                        ],
                                        "mesh3d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "mesh3d"
                                            }
                                        ],
                                        "parcoords": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "parcoords"
                                            }
                                        ],
                                        "pie": [
                                            {
                                                "automargin": true,
                                                "type": "pie"
                                            }
                                        ],
                                        "scatter": [
                                            {
                                                "fillpattern": {
                                                    "fillmode": "overlay",
                                                    "size": 10,
                                                    "solidity": 0.2
                                                },
                                                "type": "scatter"
                                            }
                                        ],
                                        "scatter3d": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatter3d"
                                            }
                                        ],
                                        "scattercarpet": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattercarpet"
                                            }
                                        ],
                                        "scattergeo": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergeo"
                                            }
                                        ],
                                        "scattergl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergl"
                                            }
                                        ],
                                        "scattermap": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermap"
                                            }
                                        ],
                                        "scattermapbox": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermapbox"
                                            }
                                        ],
                                        "scatterpolar": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolar"
                                            }
                                        ],
                                        "scatterpolargl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolargl"
                                            }
                                        ],
                                        "scatterternary": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterternary"
                                            }
                                        ],
                                        "surface": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "surface"
                                            }
                                        ],
                                        "table": [
                                            {
                                                "cells": {
                                                    "fill": {
                                                        "color": "#EBF0F8"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "header": {
                                                    "fill": {
                                                        "color": "#C8D4E3"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "type": "table"
                                            }
                                        ]
                                    },
                                    "layout": {
                                        "annotationdefaults": {
                                            "arrowcolor": "#2a3f5f",
                                            "arrowhead": 0,
                                            "arrowwidth": 1
                                        },
                                        "autotypenumbers": "strict",
                                        "coloraxis": {
                                            "colorbar": {
                                                "outlinewidth": 0,
                                                "ticks": ""
                                            }
                                        },
                                        "colorscale": {
                                            "diverging": [
                                                [
                                                    0,
                                                    "#8e0152"
                                                ],
                                                [
                                                    0.1,
                                                    "#c51b7d"
                                                ],
                                                [
                                                    0.2,
                                                    "#de77ae"
                                                ],
                                                [
                                                    0.3,
                                                    "#f1b6da"
                                                ],
                                                [
                                                    0.4,
                                                    "#fde0ef"
                                                ],
                                                [
                                                    0.5,
                                                    "#f7f7f7"
                                                ],
                                                [
                                                    0.6,
                                                    "#e6f5d0"
                                                ],
                                                [
                                                    0.7,
                                                    "#b8e186"
                                                ],
                                                [
                                                    0.8,
                                                    "#7fbc41"
                                                ],
                                                [
                                                    0.9,
                                                    "#4d9221"
                                                ],
                                                [
                                                    1,
                                                    "#276419"
                                                ]
                                            ],
                                            "sequential": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ],
                                            "sequentialminus": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ]
                                        },
                                        "colorway": [
                                            "#636efa",
                                            "#EF553B",
                                            "#00cc96",
                                            "#ab63fa",
                                            "#FFA15A",
                                            "#19d3f3",
                                            "#FF6692",
                                            "#B6E880",
                                            "#FF97FF",
                                            "#FECB52"
                                        ],
                                        "font": {
                                            "color": "#2a3f5f"
                                        },
                                        "geo": {
                                            "bgcolor": "white",
                                            "lakecolor": "white",
                                            "landcolor": "#E5ECF6",
                                            "showlakes": true,
                                            "showland": true,
                                            "subunitcolor": "white"
                                        },
                                        "hoverlabel": {
                                            "align": "left"
                                        },
                                        "hovermode": "closest",
                                        "mapbox": {
                                            "style": "light"
                                        },
                                        "paper_bgcolor": "white",
                                        "plot_bgcolor": "#E5ECF6",
                                        "polar": {
                                            "angularaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "radialaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "scene": {
                                            "xaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "yaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "zaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            }
                                        },
                                        "shapedefaults": {
                                            "line": {
                                                "color": "#2a3f5f"
                                            }
                                        },
                                        "ternary": {
                                            "aaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "baxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "caxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "title": {
                                            "x": 0.05
                                        },
                                        "xaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        },
                                        "yaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        }
                                    }
                                },
                                "xaxis": {
                                    "anchor": "y",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "FREQ"
                                    }
                                },
                                "yaxis": {
                                    "anchor": "x",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "Y"
                                    }
                                }
                            }
                        }
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Visualisation\n",
                "data[\"Cluster_FREQ\"] = labels\n",
                "data[\"Cluster_FREQ\"] = data[\"Cluster_FREQ\"].astype(str)\n",
                "data[\"Y\"] = 0\n",
                "\n",
                "fig = px.scatter(data, x=\"FREQ\", y=\"Y\", color=\"Cluster_FREQ\")\n",
                "fig.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "6ace7bc5",
            "metadata": {},
            "source": [
                "### Regroupement de zones selon (fréquence; le coût moyen)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 35,
            "id": "16103b5b",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "image/png": "[encoded data, 15392 characters]",
                        "text/plain": [
                            "<Figure size 640x480 with 1 Axes>"
                        ]
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Calcul de CAH avec lien simple\n",
                "data_x = data[[\"FREQ\", \"CM\"]].to_numpy()\n",
                "data_x = data_x.reshape(-1, 2)\n",
                "\n",
                "linkage_data = linkage(data_x, method=\"single\", metric=\"euclidean\")\n",
                "dendrogram(linkage_data, labels=np.array(data[\"ZONE_RISQUE\"]))\n",
                "\n",
                "plt.show()\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "67bf3800",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "   ZONE_RISQUE  Cluster\n",
                        "0            A        1\n",
                        "1            B        1\n",
                        "2            C        1\n",
                        "3            D        1\n",
                        "4            E        0\n",
                        "5            F        3\n",
                        "6            G        1\n",
                        "7            H        1\n",
                        "8            I        0\n",
                        "9            J        1\n",
                        "10           K        1\n",
                        "11           L        4\n",
                        "12           M        1\n",
                        "13           R        2\n",
                        "14           S        2\n",
                        "15           T        4\n",
                        "16           X        2\n"
                    ]
                }
            ],
            "source": [
                "# Calcul de la partition de l'espace\n",
                "hierarchical_cluster = AgglomerativeClustering(\n",
                "    n_clusters=5, metric=\"euclidean\", linkage=\"single\"\n",
                ")\n",
                "\n",
                "labels = hierarchical_cluster.fit_predict(data_x)\n",
                "print(pd.DataFrame({\"ZONE_RISQUE\": data[\"ZONE_RISQUE\"], \"Cluster\": labels}))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "0efdf7c1",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.plotly.v1+json": {
                            "config": {
                                "plotlyServerURL": "https://plot.ly"
                            },
                            "data": [
                                {
                                    "hovertemplate": "Cluster_FREQ_CHARGE=1<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "1",
                                    "marker": {
                                        "color": "#636efa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "1",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "DKJ1lcZbLz+MRDc7kRcqP8dbVN47cCs/uSYT8OtHMT+s1EHNqVQoP/7zjMnZ3jM/BVFIUy1nOz/efhelGZEuPxEmgZWWNVA/",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "H4XrkX/tMUGuR+E6x38xQQAAAACcZD1B9ihcj+LRG0HhehSuNx/dQGZmZmbOHPVAcT0K189z8kDXo3A9Olf1QFyPwvWo0LhA",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CHARGE=0<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "0",
                                    "marker": {
                                        "color": "#EF553B",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "0",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "ER+STWYDIz9OHBRG4L8yPw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "9ihcj54vBkEfhetRqOvfQA==",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CHARGE=3<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "3",
                                    "marker": {
                                        "color": "#00cc96",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "3",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "3mpcj5mcFT8=",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "FK5H4Zr20kA=",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CHARGE=4<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "4",
                                    "marker": {
                                        "color": "#ab63fa",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "4",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "De/QHLtwMj+qGA56SYc1Pw==",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "61G4HgUn1kCkcD0KV9qnQA==",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                },
                                {
                                    "hovertemplate": "Cluster_FREQ_CHARGE=2<br>FREQ=%{x}<br>CHARGE=%{y}<extra></extra>",
                                    "legendgroup": "2",
                                    "marker": {
                                        "color": "#FFA15A",
                                        "symbol": "circle"
                                    },
                                    "mode": "markers",
                                    "name": "2",
                                    "orientation": "v",
                                    "showlegend": true,
                                    "type": "scatter",
                                    "x": {
                                        "bdata": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                                        "dtype": "f8"
                                    },
                                    "xaxis": "x",
                                    "y": {
                                        "bdata": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                                        "dtype": "f8"
                                    },
                                    "yaxis": "y"
                                }
                            ],
                            "layout": {
                                "legend": {
                                    "title": {
                                        "text": "Cluster_FREQ_CHARGE"
                                    },
                                    "tracegroupgap": 0
                                },
                                "margin": {
                                    "t": 60
                                },
                                "template": {
                                    "data": {
                                        "bar": [
                                            {
                                                "error_x": {
                                                    "color": "#2a3f5f"
                                                },
                                                "error_y": {
                                                    "color": "#2a3f5f"
                                                },
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "bar"
                                            }
                                        ],
                                        "barpolar": [
                                            {
                                                "marker": {
                                                    "line": {
                                                        "color": "#E5ECF6",
                                                        "width": 0.5
                                                    },
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "barpolar"
                                            }
                                        ],
                                        "carpet": [
                                            {
                                                "aaxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "baxis": {
                                                    "endlinecolor": "#2a3f5f",
                                                    "gridcolor": "white",
                                                    "linecolor": "white",
                                                    "minorgridcolor": "white",
                                                    "startlinecolor": "#2a3f5f"
                                                },
                                                "type": "carpet"
                                            }
                                        ],
                                        "choropleth": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "choropleth"
                                            }
                                        ],
                                        "contour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "contour"
                                            }
                                        ],
                                        "contourcarpet": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "contourcarpet"
                                            }
                                        ],
                                        "heatmap": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "heatmap"
                                            }
                                        ],
                                        "histogram": [
                                            {
                                                "marker": {
                                                    "pattern": {
                                                        "fillmode": "overlay",
                                                        "size": 10,
                                                        "solidity": 0.2
                                                    }
                                                },
                                                "type": "histogram"
                                            }
                                        ],
                                        "histogram2d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2d"
                                            }
                                        ],
                                        "histogram2dcontour": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "histogram2dcontour"
                                            }
                                        ],
                                        "mesh3d": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "type": "mesh3d"
                                            }
                                        ],
                                        "parcoords": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "parcoords"
                                            }
                                        ],
                                        "pie": [
                                            {
                                                "automargin": true,
                                                "type": "pie"
                                            }
                                        ],
                                        "scatter": [
                                            {
                                                "fillpattern": {
                                                    "fillmode": "overlay",
                                                    "size": 10,
                                                    "solidity": 0.2
                                                },
                                                "type": "scatter"
                                            }
                                        ],
                                        "scatter3d": [
                                            {
                                                "line": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatter3d"
                                            }
                                        ],
                                        "scattercarpet": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattercarpet"
                                            }
                                        ],
                                        "scattergeo": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergeo"
                                            }
                                        ],
                                        "scattergl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattergl"
                                            }
                                        ],
                                        "scattermap": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermap"
                                            }
                                        ],
                                        "scattermapbox": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scattermapbox"
                                            }
                                        ],
                                        "scatterpolar": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolar"
                                            }
                                        ],
                                        "scatterpolargl": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterpolargl"
                                            }
                                        ],
                                        "scatterternary": [
                                            {
                                                "marker": {
                                                    "colorbar": {
                                                        "outlinewidth": 0,
                                                        "ticks": ""
                                                    }
                                                },
                                                "type": "scatterternary"
                                            }
                                        ],
                                        "surface": [
                                            {
                                                "colorbar": {
                                                    "outlinewidth": 0,
                                                    "ticks": ""
                                                },
                                                "colorscale": [
                                                    [
                                                        0,
                                                        "#0d0887"
                                                    ],
                                                    [
                                                        0.1111111111111111,
                                                        "#46039f"
                                                    ],
                                                    [
                                                        0.2222222222222222,
                                                        "#7201a8"
                                                    ],
                                                    [
                                                        0.3333333333333333,
                                                        "#9c179e"
                                                    ],
                                                    [
                                                        0.4444444444444444,
                                                        "#bd3786"
                                                    ],
                                                    [
                                                        0.5555555555555556,
                                                        "#d8576b"
                                                    ],
                                                    [
                                                        0.6666666666666666,
                                                        "#ed7953"
                                                    ],
                                                    [
                                                        0.7777777777777778,
                                                        "#fb9f3a"
                                                    ],
                                                    [
                                                        0.8888888888888888,
                                                        "#fdca26"
                                                    ],
                                                    [
                                                        1,
                                                        "#f0f921"
                                                    ]
                                                ],
                                                "type": "surface"
                                            }
                                        ],
                                        "table": [
                                            {
                                                "cells": {
                                                    "fill": {
                                                        "color": "#EBF0F8"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "header": {
                                                    "fill": {
                                                        "color": "#C8D4E3"
                                                    },
                                                    "line": {
                                                        "color": "white"
                                                    }
                                                },
                                                "type": "table"
                                            }
                                        ]
                                    },
                                    "layout": {
                                        "annotationdefaults": {
                                            "arrowcolor": "#2a3f5f",
                                            "arrowhead": 0,
                                            "arrowwidth": 1
                                        },
                                        "autotypenumbers": "strict",
                                        "coloraxis": {
                                            "colorbar": {
                                                "outlinewidth": 0,
                                                "ticks": ""
                                            }
                                        },
                                        "colorscale": {
                                            "diverging": [
                                                [
                                                    0,
                                                    "#8e0152"
                                                ],
                                                [
                                                    0.1,
                                                    "#c51b7d"
                                                ],
                                                [
                                                    0.2,
                                                    "#de77ae"
                                                ],
                                                [
                                                    0.3,
                                                    "#f1b6da"
                                                ],
                                                [
                                                    0.4,
                                                    "#fde0ef"
                                                ],
                                                [
                                                    0.5,
                                                    "#f7f7f7"
                                                ],
                                                [
                                                    0.6,
                                                    "#e6f5d0"
                                                ],
                                                [
                                                    0.7,
                                                    "#b8e186"
                                                ],
                                                [
                                                    0.8,
                                                    "#7fbc41"
                                                ],
                                                [
                                                    0.9,
                                                    "#4d9221"
                                                ],
                                                [
                                                    1,
                                                    "#276419"
                                                ]
                                            ],
                                            "sequential": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ],
                                            "sequentialminus": [
                                                [
                                                    0,
                                                    "#0d0887"
                                                ],
                                                [
                                                    0.1111111111111111,
                                                    "#46039f"
                                                ],
                                                [
                                                    0.2222222222222222,
                                                    "#7201a8"
                                                ],
                                                [
                                                    0.3333333333333333,
                                                    "#9c179e"
                                                ],
                                                [
                                                    0.4444444444444444,
                                                    "#bd3786"
                                                ],
                                                [
                                                    0.5555555555555556,
                                                    "#d8576b"
                                                ],
                                                [
                                                    0.6666666666666666,
                                                    "#ed7953"
                                                ],
                                                [
                                                    0.7777777777777778,
                                                    "#fb9f3a"
                                                ],
                                                [
                                                    0.8888888888888888,
                                                    "#fdca26"
                                                ],
                                                [
                                                    1,
                                                    "#f0f921"
                                                ]
                                            ]
                                        },
                                        "colorway": [
                                            "#636efa",
                                            "#EF553B",
                                            "#00cc96",
                                            "#ab63fa",
                                            "#FFA15A",
                                            "#19d3f3",
                                            "#FF6692",
                                            "#B6E880",
                                            "#FF97FF",
                                            "#FECB52"
                                        ],
                                        "font": {
                                            "color": "#2a3f5f"
                                        },
                                        "geo": {
                                            "bgcolor": "white",
                                            "lakecolor": "white",
                                            "landcolor": "#E5ECF6",
                                            "showlakes": true,
                                            "showland": true,
                                            "subunitcolor": "white"
                                        },
                                        "hoverlabel": {
                                            "align": "left"
                                        },
                                        "hovermode": "closest",
                                        "mapbox": {
                                            "style": "light"
                                        },
                                        "paper_bgcolor": "white",
                                        "plot_bgcolor": "#E5ECF6",
                                        "polar": {
                                            "angularaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "radialaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "scene": {
                                            "xaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "yaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            },
                                            "zaxis": {
                                                "backgroundcolor": "#E5ECF6",
                                                "gridcolor": "white",
                                                "gridwidth": 2,
                                                "linecolor": "white",
                                                "showbackground": true,
                                                "ticks": "",
                                                "zerolinecolor": "white"
                                            }
                                        },
                                        "shapedefaults": {
                                            "line": {
                                                "color": "#2a3f5f"
                                            }
                                        },
                                        "ternary": {
                                            "aaxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "baxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            },
                                            "bgcolor": "#E5ECF6",
                                            "caxis": {
                                                "gridcolor": "white",
                                                "linecolor": "white",
                                                "ticks": ""
                                            }
                                        },
                                        "title": {
                                            "x": 0.05
                                        },
                                        "xaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        },
                                        "yaxis": {
                                            "automargin": true,
                                            "gridcolor": "white",
                                            "linecolor": "white",
                                            "ticks": "",
                                            "title": {
                                                "standoff": 15
                                            },
                                            "zerolinecolor": "white",
                                            "zerolinewidth": 2
                                        }
                                    }
                                },
                                "xaxis": {
                                    "anchor": "y",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "FREQ"
                                    }
                                },
                                "yaxis": {
                                    "anchor": "x",
                                    "domain": [
                                        0,
                                        1
                                    ],
                                    "title": {
                                        "text": "CHARGE"
                                    }
                                }
                            }
                        }
                    },
                    "metadata": {},
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# Visualisation\n",
                "data[\"Cluster_FREQ_CHARGE\"] = labels\n",
                "data[\"Cluster_FREQ_CHARGE\"] = data[\"Cluster_FREQ_CHARGE\"].astype(str)\n",
                "\n",
                "fig = px.scatter(data, x=\"FREQ\", y=\"CHARGE\", color=\"Cluster_FREQ_CHARGE\")\n",
                "fig.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "12961201",
            "metadata": {},
            "source": [
                "## Application  : création de Model Points (K-means)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "6567c970",
            "metadata": {},
            "source": [
                "Dans certains cas, il se peut que la modélisation ligne par ligne ne soit pas adaptée. C'est le cas des produits collectifs en assurance ou lorsque le nombre d'individus est trop important.  \n",
                "Dans ce cas de figure, il est nécessaire d'agréger l'information afin d'avoir des \"individus type\". Chacun de ces individus est appelé *Model Point*.  \n",
                "L'algorithme des k-means peut s'avérer utile pour le regroupement d'individus sous forme de *Mode Points* lorsque les variables explicatives sont numériques.  \n",
                "  \n",
                "Afin d'illustre ce propos, nous agrègerons la base de données selon les variables ANNEE_CTR, AGE_ASSURE_PRINCIPAL, ANCIENNETE_PERMIS et ANNEE_CONSTRUCTION afin de créer 100 Model Points.  "
            ]
        },
        {
            "cell_type": "markdown",
            "id": "a250bff9",
            "metadata": {},
            "source": [
                "**Exercice :** Construire la nouvelle base de modélisation (les nouveaux individus deviennent les Model Points et chacune de modalités devient le centroïde de la classe)."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 38,
            "id": "5b42c2b5",
            "metadata": {},
            "outputs": [],
            "source": [
                "# Sélection des variables\n",
                "data_source_MP = data_retraitee[\n",
                "    [\n",
                "        \"ANNEE_CTR\",\n",
                "        \"AGE_ASSURE_PRINCIPAL\",\n",
                "        \"ANCIENNETE_PERMIS\",\n",
                "        \"ANNEE_CONSTRUCTION\",\n",
                "        \"NB\",\n",
                "        \"EXPO\",\n",
                "        \"CHARGE\",\n",
                "    ]\n",
                "]\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 39,
            "id": "0777373f",
            "metadata": {},
            "outputs": [
                {
                    "name": "stdout",
                    "output_type": "stream",
                    "text": [
                        "Nombre d'itérations réalisées : \n",
                        "41\n"
                    ]
                }
            ],
            "source": [
                "# Agrégation sous forme de MP\n",
                "# Itinitialisation\n",
                "kmeans = KMeans(n_clusters=100, max_iter=500)\n",
                "\n",
                "# Sélection des données\n",
                "data_x = data_source_MP[\n",
                "    [\"ANNEE_CTR\", \"AGE_ASSURE_PRINCIPAL\", \"ANCIENNETE_PERMIS\", \"ANNEE_CONSTRUCTION\"]\n",
                "]\n",
                "# Fitting\n",
                "kmeans.fit(data_x)\n",
                "\n",
                "# Centroïdes\n",
                "print(\"Nombre d'itérations réalisées : \")\n",
                "print(kmeans.n_iter_)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 40,
            "id": "5fd7d737",
            "metadata": {},
            "outputs": [],
            "source": [
                "# Construction de la nouvelle base de modélisation\n",
                "\n",
                "# Nouvelles variables explicatives\n",
                "base_MP = pd.DataFrame(\n",
                "    kmeans.cluster_centers_,\n",
                "    columns=[\n",
                "        \"ANNEE_CTR\",\n",
                "        \"AGE_ASSURE_PRINCIPAL\",\n",
                "        \"ANCIENNETE_PERMIS\",\n",
                "        \"ANNEE_CONSTRUCTION\",\n",
                "    ],\n",
                ")\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 41,
            "id": "39e674dd",
            "metadata": {},
            "outputs": [
                {
                    "name": "stderr",
                    "output_type": "stream",
                    "text": [
                        "/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_60807/1945959780.py:2: SettingWithCopyWarning:\n",
                        "\n",
                        "\n",
                        "A value is trying to be set on a copy of a slice from a DataFrame.\n",
                        "Try using .loc[row_indexer,col_indexer] = value instead\n",
                        "\n",
                        "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
                        "\n"
                    ]
                }
            ],
            "source": [
                "# Ajout du nombre de sinistres, exposition et CHARGE par classe\n",
                "data_source_MP[\"Cluster\"] = kmeans.labels_\n",
                "data_sinistralite_MP = data_source_MP[[\"Cluster\", \"NB\", \"EXPO\", \"CHARGE\"]]\n",
                "\n",
                "data_sinistralite_MP = data_sinistralite_MP.groupby([\"Cluster\"], as_index=False).sum()\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 42,
            "id": "8115717e",
            "metadata": {},
            "outputs": [
                {
                    "data": {
                        "application/vnd.microsoft.datawrangler.viewer.v0+json": {
                            "columns": [
                                {
                                    "name": "index",
                                    "rawType": "int64",
                                    "type": "integer"
                                },
                                {
                                    "name": "Cluster",
                                    "rawType": "int32",
                                    "type": "integer"
                                },
                                {
                                    "name": "ANNEE_CTR",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "AGE_ASSURE_PRINCIPAL",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "ANCIENNETE_PERMIS",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "ANNEE_CONSTRUCTION",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "NB",
                                    "rawType": "int64",
                                    "type": "integer"
                                },
                                {
                                    "name": "EXPO",
                                    "rawType": "float64",
                                    "type": "float"
                                },
                                {
                                    "name": "CHARGE",
                                    "rawType": "float64",
                                    "type": "float"
                                }
                            ],
                            "ref": "7165b46e-7eb6-49d2-a196-5f1042af8126",
                            "rows": [
                                [
                                    "0",
                                    "0",
                                    "2019.307392996109",
                                    "26.02334630350193",
                                    "13.019455252918299",
                                    "2017.307392996109",
                                    "14",
                                    "70582.72677595628",
                                    "76623.37"
                                ],
                                [
                                    "1",
                                    "1",
                                    "2019.185628742515",
                                    "56.634730538922156",
                                    "34.64071856287425",
                                    "2017.3233532934132",
                                    "13",
                                    "48258.04644808743",
                                    "94446.05"
                                ],
                                [
                                    "2",
                                    "2",
                                    "2019.251282051282",
                                    "45.44615384615385",
                                    "63.061538461538476",
                                    "2017.2205128205128",
                                    "18",
                                    "52378.40983606557",
                                    "180511.41"
                                ],
                                [
                                    "3",
                                    "3",
                                    "2019.4968944099378",
                                    "43.17391304347826",
                                    "27.515527950310556",
                                    "2017.2422360248447",
                                    "8",
                                    "43450.14480874316",
                                    "70994.73999999999"
                                ],
                                [
                                    "4",
                                    "4",
                                    "2018.0091743119267",
                                    "63.30275229357798",
                                    "48.211009174311926",
                                    "2014.4587155963302",
                                    "3",
                                    "31148.94262295082",
                                    "14771.7"
                                ]
                            ],
                            "shape": {
                                "columns": 8,
                                "rows": 5
                            }
                        },
                        "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>Cluster</th>\n",
                            "      <th>ANNEE_CTR</th>\n",
                            "      <th>AGE_ASSURE_PRINCIPAL</th>\n",
                            "      <th>ANCIENNETE_PERMIS</th>\n",
                            "      <th>ANNEE_CONSTRUCTION</th>\n",
                            "      <th>NB</th>\n",
                            "      <th>EXPO</th>\n",
                            "      <th>CHARGE</th>\n",
                            "    </tr>\n",
                            "  </thead>\n",
                            "  <tbody>\n",
                            "    <tr>\n",
                            "      <th>0</th>\n",
                            "      <td>0</td>\n",
                            "      <td>2019.307393</td>\n",
                            "      <td>26.023346</td>\n",
                            "      <td>13.019455</td>\n",
                            "      <td>2017.307393</td>\n",
                            "      <td>14</td>\n",
                            "      <td>70582.726776</td>\n",
                            "      <td>76623.37</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>1</th>\n",
                            "      <td>1</td>\n",
                            "      <td>2019.185629</td>\n",
                            "      <td>56.634731</td>\n",
                            "      <td>34.640719</td>\n",
                            "      <td>2017.323353</td>\n",
                            "      <td>13</td>\n",
                            "      <td>48258.046448</td>\n",
                            "      <td>94446.05</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>2</th>\n",
                            "      <td>2</td>\n",
                            "      <td>2019.251282</td>\n",
                            "      <td>45.446154</td>\n",
                            "      <td>63.061538</td>\n",
                            "      <td>2017.220513</td>\n",
                            "      <td>18</td>\n",
                            "      <td>52378.409836</td>\n",
                            "      <td>180511.41</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>3</th>\n",
                            "      <td>3</td>\n",
                            "      <td>2019.496894</td>\n",
                            "      <td>43.173913</td>\n",
                            "      <td>27.515528</td>\n",
                            "      <td>2017.242236</td>\n",
                            "      <td>8</td>\n",
                            "      <td>43450.144809</td>\n",
                            "      <td>70994.74</td>\n",
                            "    </tr>\n",
                            "    <tr>\n",
                            "      <th>4</th>\n",
                            "      <td>4</td>\n",
                            "      <td>2018.009174</td>\n",
                            "      <td>63.302752</td>\n",
                            "      <td>48.211009</td>\n",
                            "      <td>2014.458716</td>\n",
                            "      <td>3</td>\n",
                            "      <td>31148.942623</td>\n",
                            "      <td>14771.70</td>\n",
                            "    </tr>\n",
                            "  </tbody>\n",
                            "</table>\n",
                            "</div>"
                        ],
                        "text/plain": [
                            "   Cluster    ANNEE_CTR  AGE_ASSURE_PRINCIPAL  ANCIENNETE_PERMIS  \\\n",
                            "0        0  2019.307393             26.023346          13.019455   \n",
                            "1        1  2019.185629             56.634731          34.640719   \n",
                            "2        2  2019.251282             45.446154          63.061538   \n",
                            "3        3  2019.496894             43.173913          27.515528   \n",
                            "4        4  2018.009174             63.302752          48.211009   \n",
                            "\n",
                            "   ANNEE_CONSTRUCTION  NB          EXPO     CHARGE  \n",
                            "0         2017.307393  14  70582.726776   76623.37  \n",
                            "1         2017.323353  13  48258.046448   94446.05  \n",
                            "2         2017.220513  18  52378.409836  180511.41  \n",
                            "3         2017.242236   8  43450.144809   70994.74  \n",
                            "4         2014.458716   3  31148.942623   14771.70  "
                        ]
                    },
                    "execution_count": 42,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Joiture des tables\n",
                "base_MP = base_MP.merge(data_sinistralite_MP, left_index=True, right_on=\"Cluster\")\n",
                "\n",
                "base_MP = base_MP.iloc[:, [4, 0, 1, 2, 3, 5, 6, 7]]\n",
                "base_MP.head()\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "b99a8316",
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "studies",
            "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.13.3"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}
