{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13b7eb33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30212 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from scipy.io import arff\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "### 2 Clustering aggloméré\n",
    "\n",
    "# Charge le jeu de donnée : 2sp2glob\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/2sp2glob.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "#plt.figure(figsize=(10, 10))\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred)\n",
    "plt.title(\"3 clusters in 2sp2glob\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e576f7bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43016 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.cluster import AgglomerativeClustering\n",
    "\n",
    "### 2 Clustering k-means (suite)\n",
    "\n",
    "# Charge le jeu de donnée : diamond9\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/diamond9.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred = AgglomerativeClustering(n_clusters=9, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "#plt.figure(figsize=(10, 10))\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred)\n",
    "plt.title(\"9 clusters in diamond9\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a9fddea2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.352, 0.382, 0.387, 0.408, 0.411, 0.446, 0.494, 0.548, 0.517, 0.489, 0.456]\n",
      "Temps : [0.199, 0.185, 0.198, 0.18, 0.194, 0.177, 0.178, 0.189, 0.184, 0.209, 0.205]\n",
      "Clusters :  9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14856 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 42916 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import pairwise_distances\n",
    "from sklearn import datasets\n",
    "import time\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "### Charge le jeu de donnée : diamond9\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/diamond9.arff','r'))[0]\n",
    "### Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "### Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "### Calcule les Silhouette-score et les temps d'exécution pour 2 à 13 clusters\n",
    "temps = []\n",
    "mesures = []\n",
    "nb_clusters = range(2, 13)\n",
    "for i in nb_clusters:\n",
    "    t1 = time.time()\n",
    "    ### Appel de la méthode de clusterization\n",
    "    model = AgglomerativeClustering(n_clusters=i, affinity='euclidean',linkage='ward',connectivity=None).fit(npliste)\n",
    "    t2 = time.time()\n",
    "    labels = model.labels_\n",
    "    mesure = metrics.silhouette_score(npliste, labels, metric='euclidean')\n",
    "    temps += [t2-t1]\n",
    "    mesures += [mesure]\n",
    "\n",
    "print(\"Mesures :\",[round(num, 3) for num in mesures])\n",
    "print(\"Temps :\",[round(num, 3) for num in temps])\n",
    "\n",
    "### Trouve le nombre de cluster maximisant Silhouette-Score\n",
    "nb_cluster_max_score = mesures.index(max(mesures))+2\n",
    "print(\"Clusters : \",nb_cluster_max_score)\n",
    "\n",
    "### Affiche les résultats \n",
    "### Graphes des metrics\n",
    "fig, figs = plt.subplots(nrows=1, ncols=2)\n",
    "figs[0].scatter(nb_clusters, temps)\n",
    "figs[0].grid()\n",
    "figs[0].title.set_text('Temps')\n",
    "figs[1].scatter(nb_clusters, mesures)\n",
    "figs[1].grid()\n",
    "figs[1].title.set_text('Mesures')\n",
    "fig.tight_layout()\n",
    "\n",
    "### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "### Appel de la méthode de clusterization\n",
    "pred = AgglomerativeClustering(n_clusters=nb_cluster_max_score, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "### Affiche la clusterisations\n",
    "#plt.figure(figsize=(10, 10))\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred)\n",
    "plt.title(\"Clusters in diamond9\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d75ca794",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import pairwise_distances\n",
    "from sklearn import datasets\n",
    "import time\n",
    "import os\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "def metricsandclusterisationof(name):\n",
    "    path = './clustering-benchmark-master/src/main/resources/datasets/artificial/'\n",
    "    file_path = path + name + '.arff'\n",
    "    ### Charge le jeu de donnée\n",
    "    data = arff.loadarff(open(file_path,'r'))[0]\n",
    "    ### Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "    liste = []\n",
    "    for i in range(0, len(data)-1):\n",
    "        liste += [[data[i][0],data[i][1]]]\n",
    "    ### Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "    npliste = np.array([x for x in liste])\n",
    "\n",
    "    ### Calcule les Silhouette-score et les temps d'exécution pour 2 à 13 clusters\n",
    "    temps = []\n",
    "    mesures = []\n",
    "    nb_clusters = range(2, 20)\n",
    "    for i in nb_clusters:\n",
    "        t1 = time.time()\n",
    "        ### Appel de la méthode de clusterization\n",
    "        model = AgglomerativeClustering(n_clusters=i, affinity='euclidean',linkage='single',connectivity=None).fit(npliste)\n",
    "        t2 = time.time()\n",
    "        labels = model.labels_\n",
    "        mesure = metrics.silhouette_score(npliste, labels, metric='euclidean')\n",
    "        temps += [t2-t1]\n",
    "        mesures += [mesure]\n",
    "\n",
    "    print(\"Mesures :\",[round(num, 3) for num in mesures])\n",
    "    print(\"Temps :\",[round(num, 3) for num in temps])\n",
    "\n",
    "    ### Trouve le nombre de cluster maximisant Silhouette-Score\n",
    "    nb_cluster_max_score = mesures.index(max(mesures))+2\n",
    "    print(\"Clusters : \",nb_cluster_max_score)\n",
    "\n",
    "    ### Affiche les résultats \n",
    "    ### Graphes des metrics\n",
    "    fig, figs = plt.subplots(nrows=1, ncols=2)\n",
    "    figs[0].scatter(nb_clusters, temps)\n",
    "    figs[0].grid()\n",
    "    figs[0].title.set_text('Temps')\n",
    "    figs[1].scatter(nb_clusters, mesures)\n",
    "    figs[1].grid()\n",
    "    figs[1].title.set_text('Mesures')\n",
    "    fig.tight_layout()\n",
    "\n",
    "    ### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "    ### Appel de la méthode de clusterization\n",
    "    pred = AgglomerativeClustering(n_clusters=nb_cluster_max_score, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "    ### Affiche la clusterisations\n",
    "    #plt.figure(figsize=(10, 10))\n",
    "    plt.figure()\n",
    "    plt.scatter(npliste[:, 0], npliste[:, 1], c=pred)\n",
    "    cluster_name = \"Clusters in \" + name\n",
    "    plt.title(cluster_name)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ebcbbbf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.432, 0.618, 0.648, 0.734, 0.778, 0.753, 0.777, 0.737, 0.7, 0.642, 0.598, 0.474, 0.446, 0.433, 0.422, 0.419, 0.415, 0.385]\n",
      "Temps : [0.068, 0.071, 0.083, 0.08, 0.077, 0.068, 0.069, 0.079, 0.07, 0.067, 0.083, 0.078, 0.068, 0.08, 0.07, 0.069, 0.077, 0.06]\n",
      "Clusters :  6\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12284 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 21628 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.272, 0.112, 0.148, 0.109, 0.07, 0.045, 0.107, 0.106, 0.097, 0.309, 0.256, 0.213, 0.251, 0.292, 0.39, 0.409, 0.381, 0.414]\n",
      "Temps : [0.018, 0.023, 0.023, 0.023, 0.019, 0.025, 0.025, 0.023, 0.023, 0.019, 0.025, 0.023, 0.017, 0.023, 0.022, 0.021, 0.023, 0.023]\n",
      "Clusters :  19\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13772 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46836 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.395, 0.268, -0.017, -0.01, -0.033, -0.15, -0.364, -0.389, -0.4, -0.4, -0.364, -0.366, -0.369, -0.371, -0.371, -0.369, -0.371, -0.368]\n",
      "Temps : [0.005, 0.005, 0.008, 0.005, 0.007, 0.007, 0.005, 0.007, 0.005, 0.005, 0.005, 0.007, 0.007, 0.006, 0.005, 0.006, 0.004, 0.007]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14000 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 48948 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.603, 0.578, 0.498, 0.369, 0.366, 0.346, 0.33, 0.316, 0.336, 0.318, 0.288, 0.283, 0.281, 0.232, 0.234, 0.232, 0.233, 0.232]\n",
      "Temps : [0.009, 0.01, 0.008, 0.006, 0.007, 0.01, 0.009, 0.008, 0.006, 0.009, 0.006, 0.008, 0.007, 0.009, 0.007, 0.006, 0.009, 0.007]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13836 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 40780 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.496, 0.424, 0.556, 0.503, 0.431, 0.379, 0.3, 0.193, 0.178, 0.138, 0.075, 0.055, 0.045, 0.044, 0.01, -0.043, -0.078, -0.079]\n",
      "Temps : [0.008, 0.007, 0.01, 0.009, 0.007, 0.008, 0.01, 0.011, 0.01, 0.008, 0.011, 0.007, 0.008, 0.008, 0.008, 0.01, 0.01, 0.009]\n",
      "Clusters :  4\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14188 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46528 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.56, 0.867, 0.867, 0.782, 0.733, 0.69, 0.641, 0.628, 0.612, 0.597, 0.592, 0.59, 0.575, 0.461, 0.461, 0.455, 0.44, 0.439]\n",
      "Temps : [0.013, 0.053, 0.022, 0.022, 0.022, 0.023, 0.021, 0.013, 0.018, 0.02, 0.018, 0.019, 0.017, 0.018, 0.019, 0.015, 0.013, 0.014]\n",
      "Clusters :  4\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11712 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14172 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.393, 0.205, 0.15, 0.058, -0.002, -0.032, -0.041, -0.047, -0.054, -0.122, -0.135, -0.142, -0.134, -0.135, -0.136, -0.154, -0.153, -0.212]\n",
      "Temps : [0.01, 0.011, 0.014, 0.01, 0.011, 0.013, 0.013, 0.011, 0.011, 0.011, 0.013, 0.01, 0.013, 0.011, 0.011, 0.014, 0.014, 0.01]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13564 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56856 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.662, 0.696, 0.571, 0.555, 0.25, 0.222, 0.169, 0.144, 0.138, 0.117, 0.103, 0.101, 0.095, 0.078, 0.066, 0.051, 0.047, 0.031]\n",
      "Temps : [0.031, 0.035, 0.035, 0.035, 0.044, 0.035, 0.033, 0.046, 0.034, 0.042, 0.038, 0.03, 0.036, 0.036, 0.03, 0.035, 0.037, 0.035]\n",
      "Clusters :  3\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13524 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 29584 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.025, 0.001, -0.079, -0.151, -0.158, -0.191, -0.225, -0.226, -0.231, -0.238, -0.213, -0.21, -0.22, -0.219, -0.218, -0.219, -0.226, -0.229]\n",
      "Temps : [0.001, 0.002, 0.001, 0.001, 0.001, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.001]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11856 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 43644 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.011, 0.285, 0.47, 0.412, 0.319, 0.283, 0.223, 0.217, 0.203, 0.053, 0.021, 0.031, 0.025, 0.019, 0.018, 0.013, 0.003, -0.065]\n",
      "Temps : [0.006, 0.006, 0.007, 0.007, 0.008, 0.005, 0.007, 0.005, 0.005, 0.005, 0.007, 0.007, 0.005, 0.009, 0.006, 0.005, 0.008, 0.005]\n",
      "Clusters :  4\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13112 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 82360 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.689, 0.649, 0.627, 0.825, 0.725, 0.757, 0.728, 0.727, 0.719, 0.625, 0.62, 0.618, 0.601, 0.588, 0.5, 0.436, 0.437, 0.423]\n",
      "Temps : [0.011, 0.01, 0.01, 0.008, 0.011, 0.009, 0.007, 0.008, 0.007, 0.007, 0.008, 0.008, 0.007, 0.006, 0.007, 0.007, 0.008, 0.008]\n",
      "Clusters :  5\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12564 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22132 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.281, 0.049, 0.054, -0.007, -0.01, 0.08, 0.109, 0.09, 0.049, 0.046, 0.013, 0.133, 0.158, 0.117, 0.111, 0.055, 0.047, 0.046]\n",
      "Temps : [0.007, 0.008, 0.007, 0.008, 0.009, 0.009, 0.007, 0.008, 0.009, 0.009, 0.009, 0.007, 0.008, 0.007, 0.009, 0.006, 0.008, 0.007]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12860 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 60556 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.367, 0.074, 0.008, -0.106, -0.109, -0.211, -0.214, -0.221, -0.222, -0.234, -0.412, -0.455, -0.53, -0.569, -0.569, -0.571, -0.571, -0.571]\n",
      "Temps : [0.154, 0.156, 0.144, 0.148, 0.141, 0.147, 0.144, 0.145, 0.164, 0.145, 0.158, 0.177, 0.163, 0.159, 0.153, 0.147, 0.174, 0.165]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12440 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35296 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-86b5a8459821>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlis\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m         \u001b[0mmetricsandclusterisationof\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-5-009b5b9b3d51>\u001b[0m in \u001b[0;36mmetricsandclusterisationof\u001b[1;34m(name)\u001b[0m\n\u001b[0;32m     30\u001b[0m         \u001b[0mt2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m         \u001b[0mlabels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlabels_\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m         \u001b[0mmesure\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmetrics\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msilhouette_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnpliste\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmetric\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'euclidean'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     33\u001b[0m         \u001b[0mtemps\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mt2\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mt1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m         \u001b[0mmesures\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mmesure\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36minner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m     61\u001b[0m             \u001b[0mextra_args\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mall_args\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     62\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mextra_args\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 63\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     64\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     65\u001b[0m             \u001b[1;31m# extra_args > 0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\metrics\\cluster\\_unsupervised.py\u001b[0m in \u001b[0;36msilhouette_score\u001b[1;34m(X, labels, metric, sample_size, random_state, **kwds)\u001b[0m\n\u001b[0;32m    115\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    116\u001b[0m             \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindices\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindices\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 117\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msilhouette_samples\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmetric\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmetric\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    118\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    119\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36minner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m     61\u001b[0m             \u001b[0mextra_args\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mall_args\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     62\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mextra_args\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 63\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     64\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     65\u001b[0m             \u001b[1;31m# extra_args > 0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\metrics\\cluster\\_unsupervised.py\u001b[0m in \u001b[0;36msilhouette_samples\u001b[1;34m(X, labels, metric, **kwds)\u001b[0m\n\u001b[0;32m    232\u001b[0m     reduce_func = functools.partial(_silhouette_reduce,\n\u001b[0;32m    233\u001b[0m                                     labels=labels, label_freqs=label_freqs)\n\u001b[1;32m--> 234\u001b[1;33m     results = zip(*pairwise_distances_chunked(X, reduce_func=reduce_func,\n\u001b[0m\u001b[0;32m    235\u001b[0m                                               **kwds))\n\u001b[0;32m    236\u001b[0m     \u001b[0mintra_clust_dists\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minter_clust_dists\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresults\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\metrics\\pairwise.py\u001b[0m in \u001b[0;36mpairwise_distances_chunked\u001b[1;34m(X, Y, reduce_func, metric, n_jobs, working_memory, **kwds)\u001b[0m\n\u001b[0;32m   1632\u001b[0m             \u001b[0mchunk_size\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mD_chunk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1633\u001b[0m             \u001b[0mD_chunk\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mreduce_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD_chunk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1634\u001b[1;33m             \u001b[0m_check_chunk_size\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD_chunk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunk_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1635\u001b[0m         \u001b[1;32myield\u001b[0m \u001b[0mD_chunk\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1636\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\metrics\\pairwise.py\u001b[0m in \u001b[0;36m_check_chunk_size\u001b[1;34m(reduced, chunk_size)\u001b[0m\n\u001b[0;32m   1417\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1418\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1419\u001b[1;33m \u001b[1;32mdef\u001b[0m \u001b[0m_check_chunk_size\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mreduced\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchunk_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1420\u001b[0m     \"\"\"Checks chunk is a sequence of expected size or a tuple of same.\n\u001b[0;32m   1421\u001b[0m     \"\"\"\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "l=os.listdir('./clustering-benchmark-master/src/main/resources/datasets/artificial/')\n",
    "li=[i for i in l if 'arff' in i]\n",
    "lis=[x.split('.')[0] for x in l]\n",
    "for i in lis:\n",
    "    try:\n",
    "        metricsandclusterisationof(i)\n",
    "    except Exception:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0bcc79c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14272 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.56, 0.867, 0.867, 0.782, 0.733, 0.69, 0.641, 0.628, 0.612, 0.597, 0.592, 0.59, 0.575, 0.461, 0.461, 0.455, 0.44, 0.439]\n",
      "Temps : [0.007, 0.007, 0.007, 0.008, 0.008, 0.008, 0.01, 0.008, 0.007, 0.01, 0.009, 0.008, 0.009, 0.007, 0.007, 0.007, 0.007, 0.008]\n",
      "Clusters :  4\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12000 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14172 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Charge le jeu de donnée : 2d-4c\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/2d-4c.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred1 = AgglomerativeClustering(n_clusters=4, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "pred2 = AgglomerativeClustering(n_clusters=4, affinity='euclidean',linkage='complete',connectivity=None).fit_predict(npliste)\n",
    "pred3 = AgglomerativeClustering(n_clusters=4, affinity='euclidean',linkage='average',connectivity=None).fit_predict(npliste)\n",
    "pred4 = AgglomerativeClustering(n_clusters=4, affinity='euclidean',linkage='single',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred1)\n",
    "plt.title(\"4 clusters in 2d-4c\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred2)\n",
    "plt.title(\"4 clusters in 2d-4c\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred3)\n",
    "plt.title(\"4 clusters in 2d-4c\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred4)\n",
    "plt.title(\"4 clusters in 2d-4c\")\n",
    "plt.show()\n",
    "\n",
    "metricsandclusterisationof(\"2d-4c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6f1ecaf0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55444 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56388 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 55448 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52788 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.291, 0.248, 0.201, 0.202, 0.182, 0.179, 0.173, 0.152, 0.149, 0.143, 0.096, 0.103, 0.113, -0.013, -0.012, -0.001, -0.002, -0.007]\n",
      "Temps : [0.009, 0.011, 0.012, 0.013, 0.012, 0.012, 0.013, 0.013, 0.01, 0.01, 0.012, 0.013, 0.012, 0.012, 0.009, 0.009, 0.013, 0.013]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14268 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 54436 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Charge le jeu de donnée : long1\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/long1.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred1 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "pred2 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='complete',connectivity=None).fit_predict(npliste)\n",
    "pred3 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='average',connectivity=None).fit_predict(npliste)\n",
    "pred4 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='single',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred1)\n",
    "plt.title(\"2 clusters in long1, mode : ward\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred2)\n",
    "plt.title(\"2 clusters in long1, mode : complete\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred3)\n",
    "plt.title(\"2 clusters in long1, mode : average\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred4)\n",
    "plt.title(\"2 clusters in long1, mode : single\")\n",
    "plt.show()\n",
    "\n",
    "metricsandclusterisationof(\"long1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "124f3f72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36160 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36992 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36156 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35968 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.367, 0.074, 0.008, -0.106, -0.109, -0.211, -0.214, -0.221, -0.222, -0.234, -0.412, -0.455, -0.53, -0.569, -0.569, -0.571, -0.571, -0.571]\n",
      "Temps : [0.156, 0.144, 0.162, 0.154, 0.159, 0.168, 0.181, 0.147, 0.199, 0.151, 0.161, 0.168, 0.157, 0.162, 0.148, 0.161, 0.151, 0.174]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 10912 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35296 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Charge le jeu de donnée : banana\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/banana.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred1 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "pred2 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='complete',connectivity=None).fit_predict(npliste)\n",
    "pred3 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='average',connectivity=None).fit_predict(npliste)\n",
    "pred4 = AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='single',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred1)\n",
    "plt.title(\"2 clusters in banana, mode : ward\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred2)\n",
    "plt.title(\"2 clusters in banana, mode : complete\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred3)\n",
    "plt.title(\"2 clusters in banana, mode : average\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred4)\n",
    "plt.title(\"2 clusters in banana, mode : single\")\n",
    "plt.show()\n",
    "\n",
    "metricsandclusterisationof(\"banana\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d8f1c382",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47620 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 47876 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 48236 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 50352 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mesures : [0.135, -0.005, -0.243, -0.274, -0.272, -0.308, -0.311, -0.331, -0.358, -0.396, -0.441, -0.456, -0.462, -0.479, -0.493, -0.459, -0.455, -0.199]\n",
      "Temps : [0.002, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.003, 0.002, 0.002, 0.002, 0.003, 0.002, 0.002, 0.002, 0.002, 0.002]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11600 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46536 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Charge le jeu de donnée : blobs\n",
    "data = arff.loadarff(open('./clustering-benchmark-master/src/main/resources/datasets/artificial/blobs.arff','r'))[0]\n",
    "# Garde uniquement les 2 premières colonnes du jeu de donnée\n",
    "liste = []\n",
    "for i in range(0, len(data)-1):\n",
    "    liste += [[data[i][0],data[i][1]]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste = np.array([x for x in liste])\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred1 = AgglomerativeClustering(n_clusters=3, affinity='euclidean',linkage='ward',connectivity=None).fit_predict(npliste)\n",
    "pred2 = AgglomerativeClustering(n_clusters=3, affinity='euclidean',linkage='complete',connectivity=None).fit_predict(npliste)\n",
    "pred3 = AgglomerativeClustering(n_clusters=3, affinity='euclidean',linkage='average',connectivity=None).fit_predict(npliste)\n",
    "pred4 = AgglomerativeClustering(n_clusters=3, affinity='euclidean',linkage='single',connectivity=None).fit_predict(npliste)\n",
    "\n",
    "\n",
    "# Affiche le résultat de la clusterisation\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred1)\n",
    "plt.title(\"3 clusters in blobs, mode : ward\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred2)\n",
    "plt.title(\"3 clusters in blobs, mode : complete\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred3)\n",
    "plt.title(\"3 clusters in blobs, mode : average\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(npliste[:, 0], npliste[:, 1], c=pred4)\n",
    "plt.title(\"3 clusters in blobs, mode : single\")\n",
    "plt.show()\n",
    "\n",
    "metricsandclusterisationof(\"blobs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b95ac57f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
