tp_clustering.ipynb 6.06 KB
Newer Older
TheophilePACE's avatar
TheophilePACE committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TP Clustering / non-supervisé"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Aujourd'hui, on utilisera les données proposées par scikit learn. Ces données sont déjà nettoyées, déjà prêtes dans la libraries. \n",
15
    "Pour commencer, importez les libraries : numpy, matplotlib et le module datasets de scikit-learn"
TheophilePACE's avatar
TheophilePACE committed
16 17 18 19 20 21 22 23 24 25 26 27 28
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
29 30
    "Maintenant, on va utiliser les fonctions suivantes de la librairie `dataset` : `datasets.make_circles`, `datasets.make_moons`, `datasets.make_blobs`, `datasets.make_blobs`.\n",
    "Consultez la documentation intégrée de ces méthodes.\n",
TheophilePACE's avatar
TheophilePACE committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    "Vous pouvez trouver des exemples d'utilisation de ces méthodes sur https://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_comparison.html "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Sylvain Marchienne's avatar
Sylvain Marchienne committed
45
    "Générer 4 datasets, avec les paramètres suivants: `n = 100`, `noise = .05`, `random_state = 8`, `cluster_std=[1.0, 2.5, 0.5]`."
TheophilePACE's avatar
TheophilePACE committed
46 47 48 49 50 51 52 53 54 55 56 57 58
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Sylvain Marchienne's avatar
Sylvain Marchienne committed
59 60 61 62 63 64
    "Tracer les données générées dans le plan ($R^2$). Utiliser:\n",
    "```\n",
    "fig, ax = plt.subplots()\n",
    "plt.scatter(X[:, 0], X[:, 1], s=10)\n",
    "plt.show()\n",
    "```\n",
TheophilePACE's avatar
TheophilePACE committed
65
    "\n",
Sylvain Marchienne's avatar
Sylvain Marchienne committed
66
    "Commenter la difficulté du clustering."
TheophilePACE's avatar
TheophilePACE committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# K-means\n",
81
    "Utilisez les k-means sur chacun des datasets. __Conseil:__ : Une cellule par dataset pour plus de clarté."
TheophilePACE's avatar
TheophilePACE committed
82 83 84 85 86 87 88 89 90 91 92 93 94
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
95
    "Comment avez-vous choisi K ? Faites quelques tests, commentez les résultats. Gardez le meilleur hyperparmètre K. "
TheophilePACE's avatar
TheophilePACE committed
96 97 98 99 100 101 102 103 104 105 106 107 108
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
109
    "Relancez plusieurs fois pour chaque dataset. Obtenez-vous les mêmes résultats? Si non, pourquoi?"
TheophilePACE's avatar
TheophilePACE committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ACP / PCA / Principal Components Analysis\n",
    "On a vu la PCA ce matin. La fonction Scikit pour cette transformation est `sklearn.decomposition.PCA`. À vrai dire, c'est un objet. Consulter la documentation rapidemment : https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html ou la doc intégrée.\n",
125
    "Effectuez une PCA sur vos données avec 2 composantes. "
TheophilePACE's avatar
TheophilePACE committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Commentez la variance que vous avez pu expliquer. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
153
    "Chargez le jeu de données iris. Ce jeu de données est un dataset très connu, assez facile. "
TheophilePACE's avatar
TheophilePACE committed
154 155 156 157 158 159 160 161 162 163 164 165 166
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
167
    "Effectuez une PCA. Quelle est la variance expliquée?"
TheophilePACE's avatar
TheophilePACE committed
168 169 170 171 172 173 174 175 176 177 178 179 180
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
181
    "Finalement, tracez ce jeu de données dans $R^2$. Est-il facile de retrouver des clusters?"
TheophilePACE's avatar
TheophilePACE committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Si vous vous ennuyez\n",
196
    "Si vous avez le temps, essayez de lancer un k-means sur les iris. Ensuite, effectuez une PCA, puis relancez des k-means."
TheophilePACE's avatar
TheophilePACE committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Est-ce que vos résultats sont différents ? \n",
211
    "Essayez de tracer vos résultats, en indiquant les clusters trouvés."
TheophilePACE's avatar
TheophilePACE committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
Sylvain Marchienne's avatar
Sylvain Marchienne committed
238
   "version": "3.7.1"
Sylvain Marchienne's avatar
Sylvain Marchienne committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "navigate_num": "#000000",
    "navigate_text": "#333333",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700",
    "sidebar_border": "#EEEEEE",
    "wrapper_background": "#FFFFFF"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "102px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
TheophilePACE's avatar
TheophilePACE committed
263 264 265 266 267
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}