slt-ce-2.ipynb 16.5 KB
Newer Older
Ami's avatar
Ami committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SLT-CE-2: Deterministic Annealing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ol>\n",
    "<li> Sections II.A.1 (principled derivation of deterministic annealing) and II.A.3 (Mass-constrained clustering) of 'Deterministic annealing for clustering, compression, classification, regression, and related optimization problems', Kenneth Rose, 1998, http://ieeexplore.ieee.org/document/726788/ \n",
    "</li>\n",
    "\n",
    "<li>\n",
    "The wine data set, http://www3.dsi.uminho.pt/pcortez/wine5.pdf\n",
    "</li>\n",
    "    \n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn as skl\n",
    "from sklearn.utils.validation import check_is_fitted\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.datasets import make_blobs\n",
    "import sklearn.svm as svm\n",
    "from sklearn import cluster\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from treelib import Tree\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import cm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"background-color:#f0b375;\">\n",
    "Section 4.0\n",
    "<span style=font-size:50%> Complete all problems in this and previous sections to get a grade of 4.0 </span>\n",
    "</h2>\n",
    "\n",
    "<p style=\"background-color:#adebad;\">\n",
    "    For this exercise, it is of utmost importance to read reference [1] about deterministic annealing clustering (DAC). Our implementation will be based on this reference. Please shortly summarize what they refer to as the <i>preferred implementation</i> of the DAC algorithm.\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Put your markdown text here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#adebad;\">\n",
    "    In order to avoid headaches with numerical instabilities, we first try our algorithm on a simple artificially generated data as below. Run the bloc below to have a look at the data. Later when we have everything implemented, we will examine some real world data. \n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_clusters = 4\n",
    "ran_s = 42\n",
    "\n",
    "# Generate artificial dataset\n",
    "X, y_true = make_blobs(n_samples=7000, centers=4,\n",
    "                       cluster_std=0.3, random_state=ran_s,\n",
    "                       center_box=(-8.0, 8.0),\n",
    "                       shuffle=False)\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y_true, train_size=6000, random_state=42)\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, s=40, cmap='viridis')\n",
    "plt.title(\"Training data\")\n",
    "\n",
    "plt.figure()\n",
    "plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, s=40, cmap='viridis')\n",
    "plt.title(\"Test data\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#adebad;\">\n",
    "    Implement the <b>fit method</b> for the template class DeterministicAnnealing, according to the contract outlined in its docstring. (The template class DeterministicAnnealing is in file <b>DA.py</b> which you can open in your favourite IDE) For the implementation, it may help to take a look at both <b>get_distance method</b> and <b>fit _calculate_cluster_probs method</b> and implement them as well. Of course you are free to change all these methods or/and write additional methods for your purpose.\n",
    "    You can add more class methods as necessary.\n",
    "    See http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html for complementary information.\n",
    "</p>\n",
    "<p style=\"background-color:#adebad;\">\n",
    "    While implementing, you can run the bloc below to test your implementation.\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from DA import DeterministicAnnealingClustering\n",
    "\n",
    "DAC = DeterministicAnnealingClustering(\n",
    "    n_clusters=n_clusters, random_state=ran_s)\n",
    "DAC.fit(X_train)\n",
    "y_DAC = DAC.predict(X_test)\n",
    "y_DAC_hard = np.argmax(y_DAC, axis=1)\n",
    "plt.figure()\n",
    "plt.scatter(X_test[:, 0], X_test[:, 1], c=y_DAC_hard, s=40, cmap='viridis')\n",
    "plt.title(\"DA clustering\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"background-color:#f0b375;\">\n",
    "Section 4.5\n",
    "<span style=font-size:50%> Complete all problems in this section to get an additional (+0.5) point to the previous points. Note that you can have a maximum of 6 points at the end.</span>\n",
    "</h2>\n",
    "\n",
    "<p style=\"background-color:#adebad;\">\n",
    "    In this section we implement a plot which will help us better understand the DA method, and could also be a help for better debugging of your implementation.\n",
    "    \n",
    " <ul style=\"background-color:#adebad;\">\n",
    "        <li> \n",
    "            Modify your implementation of <b>fit</b> function such that <b>plot_phase_diagram</b> method will produce a plot similar to the phase diagram plot shown in Figure 2 of the reference paper.\n",
    "        </li>\n",
    "</ul>  \n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ul style=\"background-color:#adebad;\">\n",
    "            Produce a phase diagram plot of the expected distortion D, as shown in figure 2 of reference [1]. For this, extend DAC.fit to save the expected distortion during annealing as an additional attribute self.distortion.\n",
    "            You might also want to save the number of effective clusters and the temperature along the way.\n",
    "    </ul>\n",
    "</p>\n",
    "\n",
    "#### extend DAC.fit(self, X):\n",
    "    # ...\n",
    "    # Save information for each (n-th) annealing step:\n",
    "    # self.distortion = [d0, d1, d2, ...]\n",
    "    # self.n_eff_clusters = [e0, e1, e2, ...]\n",
    "    # self.temp = [t0, t1, t2, ...]\n",
    "    # ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DAC.plot_phase_diagram()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"background-color:#f0b375;\">\n",
    "Section 5.0\n",
    "<span style=font-size:50%> Complete all problems in this section to get an additional (+0.5) point to the previous points.</span>\n",
    "</h2>\n",
    "<ul style=\"background-color:#adebad;\">\n",
    "Here we implement another plot which helps better undetrstad the dynamics of the algorithm.\n",
    "        <li>\n",
    "        Implement DAC.plot_bifurcation, which should create a bifurcation plot.<br>\n",
    "        Modify DAC.fit to keep track of the distances, using the tree object DAC.bifurcation_tree. When a cluster splits, it creates two child nodes. Each node should store its centroid vector, and the distance to the parent centroid vector. After splitting, the parent node is not updated anymore.<br>\n",
    "        In the bifurcation plot, the horizontal distance of a child node to its parent node should be exactly the distance to the parent centroid vector. The two child nodes should move in opposite directions, i.e. one to the left of the parent and one to the right.\n",
    "        </li>\n",
    "</ul>\n",
    "\n",
    "This section could bit a bit annoying, you can also jump to the next sections and come back here later. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DAC.plot_bifurcation()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"background-color:#f0b375;\">\n",
    "Section 5.5\n",
    "<span style=font-size:50%> Complete all problems in this section to get an additional (+0.5) point to the previous points.</span>\n",
    "</h2>\n",
    "\n",
    "<p style=\"background-color:#adebad;\">\n",
    "Now we are ready to use some real world data. This might need some tweaking and handling of numberical instabilities. Please make sure your understand the data.\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#adebad;\">\n",
    "Read the wine data [3], which contains 11 physiochemical attributes, and two labels (quality and color).\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#adebad;\">\n",
    "    Create an instance of your DAC class with n_clusters = 2 and <b>fit the first 6000 samples</b> of the wine data set. Record the execution time. Furthermore, create an instance of the sklearn k-means class, and fit it with the same parameters. Again record the execution time. Make sure that the hyper parameters (initial temperature, min temperature, convergence criteria, noise, etc.) make sense and lead to a reasonable clustering\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from DA import read_data_csv\n",
    "X, y = read_data_csv(\"wine-data.csv\", y_names=[\"quality\", \"color\"])\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y[\"color\"], train_size=6000, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "DAC = DeterministicAnnealingClustering(n_clusters=2, random_state=42)\n",
    "DAC.fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "kmeans = cluster.KMeans(n_clusters=2,random_state=42)\n",
    "kmeans.fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "y_kmeans = kmeans.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "y_DAC = DAC.predict(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"background-color:#f0b375;\">\n",
    "Section 6.0\n",
    "<span style=font-size:50%> Complete all problems in this section to get an additional (+0.5) point to the previous points.</span>\n",
    "</h2>\n",
    "<ul style=\"background-color:#adebad;\">\n",
    "<li> Before we can compute the confusion matrix, we need to perform some post-processing on the DAC cluster assignments.\n",
    "    Explain what the function postprocess (defined below) does, and why we need it. To do so, complete the docstring of the function postprocess.\n",
    "        </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def postprocess(y_DAC, y_kmeans):\n",
    "    \"\"\"TODO: Add explanation\"\"\"\n",
    "    \n",
    "    y_DAC_hard = np.argmax(y_DAC, axis=1)\n",
    "    \n",
    "    n_clusters = len(np.unique(y_DAC_hard))\n",
    "    dac2kmeans = []\n",
    "    for cluster in range(n_clusters):\n",
    "        argmax = np.argmax(y_DAC[:, cluster])\n",
    "        dac2kmeans.append(y_kmeans[argmax])\n",
    "        \n",
    "    y_DAC_new = []\n",
    "    for dac_label in y_DAC_hard:\n",
    "        y_DAC_new.append(dac2kmeans[dac_label])\n",
    "        \n",
    "    return np.array(y_DAC_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "skl.metrics.confusion_matrix(y_kmeans, postprocess(y_DAC, y_kmeans))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "</h2>\n",
    "\n",
    "<ul style=\"background-color:#adebad;\">\n",
    "        <li> Read the docstring of <b>transform method</b> and understand what it does.\n",
    "        </li>\n",
    "        <li>\n",
    "        Use DAC.transform and kmeans.transform to transform both, X_train and X_test. \n",
    "        </li>\n",
    "       \n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "X_train_DAC = DAC.transform(X_train)\n",
    "X_test_DAC = DAC.transform(X_test)\n",
    "\n",
    "X_train_kmeans = kmeans.transform(X_train)\n",
    "X_test_kmeans = kmeans.transform(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ul style=\"background-color:#adebad;\">\n",
    "        <li>\n",
    "        Fit an SVM classifier with default parameters to the untransformed data, and to the transformed data.\n",
    "        Compare the performance of predicting whether the color of a wine is red or white.\n",
    "        </li>\n",
    "    </ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "svm = svm.SVC(random_state=42)\n",
    "svm.fit(X_train, y_train)\n",
    "svm.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "svm_DAC = svm.SVC(random_state=42)\n",
    "svm_DAC.fit(X_train_DAC, y_train)\n",
    "svm_DAC.score(X_test_DAC, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "svm = svm.SVC(random_state=42)\n",
    "svm.fit(X_train_kmeans, y_train)\n",
    "svm.score(X_test_kmeans, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ul style=\"background-color:#adebad;\">\n",
    "        <li>\n",
    "        Produce two scatter plots, one for X_train_DAC and one for X_train_kmeans.<br>\n",
    "        Make the marker color indicate the wine color.\n",
    "        </li>\n",
    "    </ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ul style=\"background-color:#adebad;\">\n",
    "    <li>\n",
    "        Create a fixed 2D embedding (e.g. with LLE, t-SNE, MDS) of the wine data and color the markers according to quality and color. Fit and transform X_train with DAC(n_clusters=2,3,4,5,6,7,8,...). Produce a plot of the SVM score svm_DAC.score(X_test_DAC, y_test) as a function of n_clusters.. Each time use marker shapes to display the cluster memberships, and compare to the labels color and quality.\n",
    "    </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    %%time\n",
    "    lle = skl.manifold.LocallyLinearEmbedding(random_state=...)\n",
    "    lle.fit(...)\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<ul style=\"background-color:#adebad;\">\n",
    "        <li>\n",
    "            So far, our implementation of DAC assumed that our data is compatible with the euclidian metric. Argue why this assumption is not justified for the wine-data. Suggest a better alternative (no implementation required!).\n",
    "        </li>\n",
    "    </ul>\n",
    "</p>\n",
    "\n"
   ]
  },
  {
   "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",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}