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 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "6fa9abb7",
"metadata": {},
"outputs": [],
"source": [
"# Digit classification is a classic problem in machine learning.\n",
"# In this, a neural network is tasked at recognizing digits from \n",
"# a dataset of 8x8 greyscale images.\n",
"# \n",
"# Digital root classification is another classic problem, though\n",
"# somewhat less well known. In this task, the neural network must\n",
"# recognize the digital roots of a 64-bit integer.\n",
"#\n",
"# In this notebook, we explore the joint task \"digital classification\"\n",
"# using the MNIST database for labelled digit images, and the \n",
"# FEMINIST database for labelled 64-bit integers.\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn import datasets, svm, metrics\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d4d8492",
"metadata": {},
"outputs": [],
"source": [
"# We fetch the digits dataset. This will be altered to train the neural\n",
"# network for digital classification. Thanks, MNIST!\n",
"digits = datasets.load_digits()\n",
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, label in zip(axes, digits.images, digits.target):\n",
" ax.set_axis_off()\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(\"Dataset: %i\" % label)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e8c52dd",
"metadata": {},
"outputs": [],
"source": [
"# We initialize the digital roots dataset. Thanks, FEMINIST!\n",
"bits = np.load(\"bits\", allow_pickle=True)\n",
"roots = np.load(\"roots\", allow_pickle=True)\n",
"\n",
"for i in range(4):\n",
" print(f\"Number:\\n{bits[i]}\\nDigital root:\\n\\t{roots[i]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1aafc18e",
"metadata": {},
"outputs": [],
"source": [
"# We interleave the digital root dataset into the \n",
"# least significant bit of the digits dataset.\n",
"n_samples = len(digits.images)\n",
"data = digits.images.reshape((n_samples, -1))\n",
"altered = data // 2 * 2 + bits\n",
"\n",
"clf = svm.SVC(gamma=0.001)\n",
"\n",
"# Let's train with 80% training data, and 20% testing.\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" altered, roots, test_size=0.80, shuffle=False\n",
")\n",
"\n",
"# Learn the digits on the train subset\n",
"clf.fit(X_train, y_train)\n",
"\n",
"# Predict the value of the digit on the test subset\n",
"predicted = clf.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27cc0c5d",
"metadata": {},
"outputs": [],
"source": [
"# Observe the model's predictions!\n",
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, prediction in zip(axes, X_test, predicted):\n",
" ax.set_axis_off()\n",
" image = image.reshape(8, 8)\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(f\"Prediction: {prediction}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "007eb980",
"metadata": {},
"outputs": [],
"source": [
"# Let's assess the precision of the model on the dataset!\n",
"print(\n",
" f\"Classification report for classifier {clf}:\\n\"\n",
" f\"{metrics.classification_report(y_test, predicted)}\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9a6a199",
"metadata": {},
"outputs": [],
"source": [
"# O-oh. That's, uh, not very good. That's not good at all.\n",
"# That's actually equivalent to random guessing.\n",
"# Hold on, what is the model actually doing?\n",
"disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted)\n",
"disp.figure_.suptitle(\"Confusion Matrix\")\n",
"print(f\"Confusion matrix:\\n{disp.confusion_matrix}\")\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af63087a",
"metadata": {},
"outputs": [],
"source": [
"# What on earth are you doing? This isn't what you're supposed to do!\n",
"# You were supposed to classify digital roots! This is just the\n",
"# same distribution with little influence from the input!\n",
"#\n",
"# Gosh, what now?\n",
"# Maybe the least significant bit is not significant enough. Let's\n",
"# put that bit somewhere else, then???\n",
"altered2 = data // 4 * 4 + 2 * bits + data % 2\n",
"\n",
"clf2 = svm.SVC(gamma=0.001)\n",
"X_train2, X_test2, y_train2, y_test2 = train_test_split(\n",
" altered2, roots, test_size=0.80, shuffle=False\n",
")\n",
"clf2.fit(X_train2, y_train2)\n",
"predicted2 = clf2.predict(X_test2)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a57915b",
"metadata": {},
"outputs": [],
"source": [
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, prediction in zip(axes, X_test2, predicted2):\n",
" ax.set_axis_off()\n",
" image = image.reshape(8, 8)\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(f\"Prediction: {prediction}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5de80395",
"metadata": {},
"outputs": [],
"source": [
"# Show me what's wrong this time!\n",
"print(\n",
" f\"Classification report for classifier {clf2}:\\n\"\n",
" f\"{metrics.classification_report(y_test2, predicted2)}\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e8bd438",
"metadata": {},
"outputs": [],
"source": [
"# Aaaaaaaaaaaaaaaaaargh\n",
"# But I read through the tutorial! This is supposed to work!\n",
"# Sure, this data is totally unfit for machine learning, but\n",
"# it should work because neural networks are supposed to learn!\n",
"# \n",
"# Maybe it just needs a different kind of model? Yeah! \n",
"# Let's pick some other thing from the, whew, staggeringly\n",
"# long list of technical sounding terms on sklearn's website!\n",
"from sklearn import ensemble\n",
"clf3 = ensemble.RandomForestClassifier(n_estimators=40)\n",
"\n",
"bitbits = bits.astype(bool)\n",
"\n",
"# Hoping for the best this time...\n",
"X_train3, X_test3, y_train3, y_test3 = train_test_split(\n",
" bitbits, roots, test_size=0.80, shuffle=False\n",
")\n",
"\n",
"clf3.fit(X_train3, y_train3)\n",
"predicted3 = clf3.predict(X_test3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f581d224",
"metadata": {},
"outputs": [],
"source": [
"# Pretty please?\n",
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, prediction in zip(axes, X_test3, predicted3):\n",
" ax.set_axis_off()\n",
" image = image.reshape(8, 8)\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(f\"Prediction: {prediction}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f54b62f3",
"metadata": {},
"outputs": [],
"source": [
"# Not sure yet...\n",
"print(\n",
" f\"Classification report for classifier {clf3}:\\n\"\n",
" f\"{metrics.classification_report(y_test3, predicted3)}\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84e11bf9",
"metadata": {},
"outputs": [],
"source": [
"# Whyyyyyyyyyy this is not good at alllllllllllllllll\n",
"disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test3, predicted3)\n",
"disp.figure_.suptitle(\"Confusion Matrix\")\n",
"print(f\"Confusion matrix:\\n{disp.confusion_matrix}\")\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ab9795d",
"metadata": {},
"outputs": [],
"source": [
"# Ugh, fine. Maybe what this needs is more data. \n",
"# \n",
"# Yeah, more data. That's a good idea. Let's do that.\n",
"# How do we get more data? FEMINIST only had that dataset.\n",
"# \n",
"# Hmm... Maybe we can generate our own data? Sure, that's\n",
"# totally normal. Yeah let's do that.\n",
"def entry(n):\n",
" while n > 9:\n",
" n = sum(map(int, str(n)))\n",
" return n\n",
"\n",
"# What's a good number of data points? A million?\n",
"import random\n",
"n_points = 1_000_000\n",
"numbers = [random.randint(0, (1 << 64) - 1) for _ in range(1_000_000)]\n",
"\n",
"b_iii_iiits = np.array([np.array(list(np.binary_repr(n, width = 64))) == '1' for n in numbers])\n",
"r_ooo_ooots = np.array([entry(n) for n in numbers])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5c1030b",
"metadata": {},
"outputs": [],
"source": [
"# Maybe this is enough data? It took more than a second to generate after all.\n",
"for i in range(4):\n",
" print(f\"Number:\\n{b_iii_iiits[i]}\\nDigital root:\\n\\t{r_ooo_ooots[i]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31970791",
"metadata": {},
"outputs": [],
"source": [
"# Here we go......\n",
"clf4 = ensemble.RandomForestClassifier(n_estimators=100)\n",
"X_train4, X_test4, y_train4, y_test4 = train_test_split(\n",
" b_iii_iiits, r_ooo_ooots, test_size=0.80, shuffle=False\n",
")\n",
"clf4.fit(X_train4, y_train4)\n",
"predicted4 = clf4.predict(X_test4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab3d458b",
"metadata": {},
"outputs": [],
"source": [
"# That took a long time to train... Hoping for the best...\n",
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10, 3))\n",
"for ax, image, prediction in zip(axes, X_test4, predicted4):\n",
" ax.set_axis_off()\n",
" image = image.reshape(8, 8)\n",
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
" ax.set_title(f\"Prediction: {prediction}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6233a6c0",
"metadata": {},
"outputs": [],
"source": [
"# Please, don't make me cry...\n",
"print(\n",
" f\"Classification report for classifier {clf4}:\\n\"\n",
" f\"{metrics.classification_report(y_test4, predicted4)}\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "362b7c12",
"metadata": {},
"outputs": [],
"source": [
"# OMG, 0.11 ACCURACY!!!!!!!!!!!!!!!!!!!\n",
"# THAT'S BETTER THAN RANDOM GUESSING!!!\n",
"# I'm so happyyyyyyy\n",
"#\n",
"# But, uh, what is it actually doing\n",
"disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test4, predicted4)\n",
"disp.figure_.suptitle(\"Confusion Matrix\")\n",
"print(f\"Confusion matrix:\\n{disp.confusion_matrix}\")\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c232822d",
"metadata": {},
"outputs": [],
"source": [
"# I'm celebrating so much! This data definitely doesn't \n",
"# demonstrate a massive flaw in the training data. It means\n",
"# that my neural network learned! Yippeeeee!!!\n",
"print(\"yous tryly, sans undertale\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|
what the fuck
jan junsa
post a comment