{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "13b7eb33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps de calcul : 0.103 secondes\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 113440 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 sklearn import metrics\n",
    "from scipy.io import arff\n",
    "import numpy as np\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import hdbscan\n",
    "\n",
    "### 4 HDBSCAN\n",
    "\n",
    "name = \"cluto-t5-8k\"\n",
    "file_path = './clustering-benchmark-master/src/main/resources/datasets/artificial/' + name + \".arff\"\n",
    "\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 divers min_cluster\n",
    "# temps = []\n",
    "# mesures = []\n",
    "# nb_cluster_size = range(2,50)\n",
    "# for i in nb_cluster_size:\n",
    "#     t1 = time.time()\n",
    "#     ### Appel de la méthode de clusterization\n",
    "#     model = hdbscan.HDBSCAN(min_cluster_size=i).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",
    "# print(\"Mesures :\",[round(num, 3) for num in mesures])\n",
    "# print(\"Temps :\",[round(num, 3) for num in temps])\n",
    "# ### Trouve le nombre de cluster maximisant Silhouette-Score\n",
    "# min_cluster_size_max_score = mesures.index(max(mesures))+2\n",
    "# print(\"Min_cluster_size maximisant Silhouette : \",min_cluster_size_max_score)\n",
    "# Calcul les clusters pour le nombre maximisant silhouette-score\n",
    "t1 = time.time()\n",
    "# pred = hdbscan.HDBSCAN(min_cluster_size=min_cluster_size_max_score,min_samples=15).fit_predict(npliste)\n",
    "pred = hdbscan.HDBSCAN(min_samples=None).fit_predict(npliste)\n",
    "t2 = time.time()\n",
    "print(\"Temps de calcul : \" + str(round(t2-t1,3)) + \" secondes\")\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": 63,
   "id": "a9fddea2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epsilon :  18.255\n",
      "min_sample :  120\n",
      "Temps de calcul : 0.05 secondes\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 101324 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 time\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "### 4 DBSCAN\n",
    "\n",
    "name = \"cluto-t5-8k\"\n",
    "#name = \"dense-disk-5000\"\n",
    "file_path = './clustering-benchmark-master/src/main/resources/datasets/artificial/' + name + \".arff\"\n",
    "\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",
    "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",
    "min_sample = 120\n",
    "print(\"min_sample : \",min_sample)\n",
    "\n",
    "# Calcul les clusters pour le nombre attendus de cluster\n",
    "t1 = time.time()\n",
    "pred = DBSCAN(eps=epsilon, min_samples=min_sample).fit_predict(npliste)\n",
    "t2 = time.time()\n",
    "print(\"Temps de calcul : \" + str(round(t2-t1,3)) + \" secondes\")\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": 174,
   "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",
    "import csv\n",
    "import pandas as pd\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "def clusteringOnFolderAgglo(name,n,m):\n",
    "    \n",
    "    ### Charge le jeu de donnée\n",
    "#     rawdata = pd.read_csv(file_path, sep=\"\\t\")\n",
    "#     data = rawdata.drop(columns=['C'])\n",
    "#     print(data)\n",
    "    csvfile = open('dataset/' + name + '.data','r')\n",
    "    data = []\n",
    "    try:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = '\\t')]\n",
    "    except Exception:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = ' ')]\n",
    "    \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 le temps d'exécution\n",
    "    t1 = time.time()\n",
    "    ### Appel de la méthode de clusterization\n",
    "    pred = AgglomerativeClustering(n_clusters=n, affinity='euclidean',linkage=m,connectivity=None).fit_predict(npliste)\n",
    "    t2 = time.time()\n",
    "    temps = round(t2-t1,3)\n",
    "    print(\"Temps : \",temps)\n",
    "\n",
    "    ### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "    ### Appel de la méthode de clusterization\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": 175,
   "id": "ebcbbbf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'h', 't', 'tr', 'zgn', 'zgo']\n",
      "Temps :  0.008\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 59680 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.002\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29892 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.004\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 65268 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  1.68\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25380 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.001\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 40116 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.001\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25012 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "l=os.listdir('./dataset/')\n",
    "li=[i for i in l if 'data' in i]\n",
    "lis=[x.split('.')[0] for x in l]\n",
    "print(lis)\n",
    "nb_clusters = [2,5,4,4,3,5]\n",
    "modes = ['complete','ward','ward','ward','single','single']\n",
    "n = 0\n",
    "for i in lis:\n",
    "    try:\n",
    "        clusteringOnFolderAgglo(i,nb_clusters[n],modes[n])\n",
    "        n += 1\n",
    "    except Exception:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "6f1ecaf0",
   "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",
    "from sklearn.cluster import KMeans\n",
    "import time\n",
    "import os\n",
    "import csv\n",
    "import pandas as pd\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "def clusteringOnFolderKmeans(name,n):\n",
    "    \n",
    "    ### Charge le jeu de donnée\n",
    "#     rawdata = pd.read_csv(file_path, sep=\"\\t\")\n",
    "#     data = rawdata.drop(columns=['C'])\n",
    "#     print(data)\n",
    "    csvfile = open('dataset/' + name + '.data','r')\n",
    "    data = []\n",
    "    try:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = '\\t')]\n",
    "    except Exception:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = ' ')]\n",
    "    \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 le temps d'exécution\n",
    "    t1 = time.time()\n",
    "    ### Appel de la méthode de clusterization\n",
    "    pred = KMeans(n_clusters=n, random_state=0).fit_predict(npliste)\n",
    "    t2 = time.time()\n",
    "    temps = round(t2-t1,3)\n",
    "    print(\"Temps : \",temps)\n",
    "\n",
    "    ### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "    ### Appel de la méthode de clusterization\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": 182,
   "id": "124f3f72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'h', 't', 'tr', 'zgn', 'zgo']\n",
      "Temps :  0.021\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 56900 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.013\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29860 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.024\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 64416 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.046\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24728 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.016\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 38688 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.021\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24296 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "l=os.listdir('./dataset/')\n",
    "li=[i for i in l if 'data' in i]\n",
    "lis=[x.split('.')[0] for x in l]\n",
    "print(lis)\n",
    "nb_clusters = [2,5,4,4,3,5]\n",
    "n = 0\n",
    "for i in lis:\n",
    "    clusteringOnFolderKmeans(i,nb_clusters[n])\n",
    "    n += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "d8f1c382",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import metrics\n",
    "from sklearn.metrics import pairwise_distances\n",
    "from sklearn import datasets\n",
    "from sklearn.cluster import DBSCAN\n",
    "from sklearn.neighbors import NearestNeighbors\n",
    "import time\n",
    "import os\n",
    "import csv\n",
    "import pandas as pd\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "def clusteringOnFolderDBSCAN(name,ms):\n",
    "    \n",
    "    ### Charge le jeu de donnée\n",
    "#     rawdata = pd.read_csv(file_path, sep=\"\\t\")\n",
    "#     data = rawdata.drop(columns=['C'])\n",
    "#     print(data)\n",
    "    csvfile = open('dataset/' + name + '.data','r')\n",
    "    data = []\n",
    "    try:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = '\\t')]\n",
    "    except Exception:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = ' ')]\n",
    "    \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",
    "\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",
    "    \n",
    "    ### Calcule le temps d'exécution\n",
    "    t1 = time.time()\n",
    "    ### Appel de la méthode de clusterization\n",
    "    pred = DBSCAN(eps=epsilon, min_samples=ms).fit_predict(npliste)\n",
    "    t2 = time.time()\n",
    "    temps = round(t2-t1,3)\n",
    "    print(\"Temps : \",temps)\n",
    "\n",
    "    ### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "    ### Appel de la méthode de clusterization\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": 231,
   "id": "b95ac57f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'h', 't', 'tr', 'zgn', 'zgo']\n",
      "Temps :  0.004\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 62920 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.001\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 30468 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.002\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 66024 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.035\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26864 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.002\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.001\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24788 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "l=os.listdir('./dataset/')\n",
    "li=[i for i in l if 'data' in i]\n",
    "lis=[x.split('.')[0] for x in l]\n",
    "print(lis)\n",
    "min_samples = [100,2,10,1,10,5]\n",
    "n = 0\n",
    "for i in lis:\n",
    "    clusteringOnFolderDBSCAN(i,min_samples[n])\n",
    "    n += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "6303619e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import metrics\n",
    "from sklearn.metrics import pairwise_distances\n",
    "from sklearn import datasets\n",
    "from sklearn.cluster import DBSCAN\n",
    "from sklearn.neighbors import NearestNeighbors\n",
    "import time\n",
    "import os\n",
    "import csv\n",
    "import pandas as pd\n",
    "\n",
    "### 2 Clustering aggloméré (metrics)\n",
    "\n",
    "def clusteringOnFolderHDBSCAN(name,ms):\n",
    "    \n",
    "    ### Charge le jeu de donnée\n",
    "#     rawdata = pd.read_csv(file_path, sep=\"\\t\")\n",
    "#     data = rawdata.drop(columns=['C'])\n",
    "#     print(data)\n",
    "    csvfile = open('dataset/' + name + '.data','r')\n",
    "    data = []\n",
    "    try:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = '\\t')]\n",
    "    except Exception:\n",
    "        data += [list(map(float, point)) for point in csv.reader(csvfile, delimiter = ' ')]\n",
    "    \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 le temps d'exécution\n",
    "    t1 = time.time()\n",
    "    ### Appel de la méthode de clusterization\n",
    "    pred = hdbscan.HDBSCAN(min_samples=ms).fit_predict(npliste)\n",
    "    t2 = time.time()\n",
    "    temps = round(t2-t1,3)\n",
    "    print(\"Temps : \",temps)\n",
    "\n",
    "    ### Calcule les clusters pour le nombre maximisant le Silhouette-Score\n",
    "    ### Appel de la méthode de clusterization\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": 299,
   "id": "6aff1e86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'h', 't', 'tr', 'zgn', 'zgo']\n",
      "Temps :  0.011\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 62500 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.003\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29132 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.005\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 67428 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.15\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25000 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.004\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39472 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temps :  0.004\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 21736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "l=os.listdir('./dataset/')\n",
    "li=[i for i in l if 'data' in i]\n",
    "lis=[x.split('.')[0] for x in l]\n",
    "print(lis)\n",
    "min_samples = [40,5,20,70,10,5]\n",
    "n = 0\n",
    "for i in lis:\n",
    "    clusteringOnFolderHDBSCAN(i,min_samples[n])\n",
    "    n += 1"
   ]
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
