{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "13b7eb33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.         0.0352566  0.04549539]\n",
      " [0.         0.248588   0.26211434]\n",
      " [0.         0.36971798 0.44593303]\n",
      " ...\n",
      " [0.         0.17770385 0.18372765]\n",
      " [0.         0.47954405 0.60826988]\n",
      " [0.         0.20406416 0.21673267]]\n",
      "epsilon :  4.542\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 14080 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "from sklearn.neighbors import NearestNeighbors\n",
    "from scipy.io import arff\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "### 3 DBSCAN\n",
    "\n",
    "name = \"2d-4c\"\n",
    "file_path = './clustering-benchmark-master/src/main/resources/datasets/artificial/' + name + '.arff'\n",
    "\n",
    "# Charge le jeu de donnée : 2sp2glob\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",
    "nbrs = NearestNeighbors(n_neighbors=3, algorithm='ball_tree').fit(npliste)\n",
    "distances, indices = nbrs.kneighbors(npliste)\n",
    "print(distances)\n",
    "\n",
    "# Garde uniquement la colonne correspondant aux distances\n",
    "liste2 = []\n",
    "for i in range(0, len(distances)-1):\n",
    "    liste2 += [distances[i][1]]\n",
    "# Transforme la liste de donnée en une liste de liste utilisable par numpy\n",
    "npliste2 = np.array([x for x in liste2])\n",
    "#print(npliste2)\n",
    "\n",
    "epsilon = 0.98*max(npliste2)\n",
    "print(\"epsilon : \",round(epsilon,3))\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "pred = DBSCAN(eps=epsilon, min_samples=100).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(\"Clusters in \" + name)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e576f7bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43004 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 = DBSCAN(eps=0.5, min_samples=100).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(\"Clusters in diamond9\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "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.117, 0.119, 0.122, 0.193, 0.138, 0.141, 0.134, 0.137, 0.133, 0.136, 0.133]\n",
      "Clusters :  9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13620 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": 57,
   "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": null,
   "id": "ebcbbbf2",
   "metadata": {},
   "outputs": [],
   "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": 62,
   "id": "6f1ecaf0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55500 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56800 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 55528 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 52980 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.005, 0.005, 0.005, 0.005, 0.006, 0.006, 0.006, 0.006, 0.005, 0.005, 0.006, 0.008, 0.006, 0.005, 0.005, 0.005, 0.005, 0.005]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12960 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": 60,
   "id": "124f3f72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36152 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36908 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36196 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 36000 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.073, 0.073, 0.076, 0.074, 0.076, 0.076, 0.074, 0.076, 0.072, 0.076, 0.079, 0.071, 0.073, 0.071, 0.077, 0.076, 0.071, 0.071]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 13164 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",
    "\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": 61,
   "id": "d8f1c382",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47648 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 47728 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 48156 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 50324 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.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001]\n",
      "Clusters :  2\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 12092 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
}
