Browse Source

Comparison of the 3 methods

Louis Farina 2 years ago
parent
commit
b882fd9d0a
3 changed files with 160 additions and 0 deletions
  1. 6
    0
      .ipynb_checkpoints/Untitled-checkpoint.ipynb
  2. 122
    0
      Untitled.ipynb
  3. 32
    0
      eval.py

+ 6
- 0
.ipynb_checkpoints/Untitled-checkpoint.ipynb View File

@@ -0,0 +1,6 @@
1
+{
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+}

+ 122
- 0
Untitled.ipynb View File

@@ -0,0 +1,122 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 1,
6
+   "id": "40152b50",
7
+   "metadata": {},
8
+   "outputs": [],
9
+   "source": []
10
+  },
11
+  {
12
+   "cell_type": "code",
13
+   "execution_count": 18,
14
+   "id": "be8d8613",
15
+   "metadata": {},
16
+   "outputs": [
17
+    {
18
+     "name": "stdout",
19
+     "output_type": "stream",
20
+     "text": [
21
+      "training + evaluating time : 1.9439990520477295, score = 0.9504444444444444\n",
22
+      "[[470   0   1   0   0   0   1   0   1   0]\n",
23
+      " [  0 502   2   1   0   0   0   0   0   0]\n",
24
+      " [  4   9 388   2   0   0   2  13   2   0]\n",
25
+      " [  2   0   2 454   1  12   0   2   3   3]\n",
26
+      " [  0   8   0   0 388   0   2   2   0  20]\n",
27
+      " [  1   3   0  11   0 368   6   0   0   2]\n",
28
+      " [  6   2   0   0   1   2 454   0   0   0]\n",
29
+      " [  1   5   2   0   1   0   0 440   0  14]\n",
30
+      " [  2  12   4  12   2   7   2   2 398   4]\n",
31
+      " [  1   2   0   8   8   0   0   5   0 415]]\n",
32
+      "training + evaluating time : 7.785594463348389, score = 0.9304444444444444\n",
33
+      "[[465   0   3   0   0   0   3   1   0   1]\n",
34
+      " [  0 494   4   1   3   0   2   0   0   1]\n",
35
+      " [  3  13 378   8   4   0   4   6   3   1]\n",
36
+      " [  3   1   9 446   0   2   1   3   8   6]\n",
37
+      " [  1   3   1   0 392   2   5   4   3   9]\n",
38
+      " [  5   2   0   9   4 353   8   1   7   2]\n",
39
+      " [  5   2   4   0   7   1 444   0   1   1]\n",
40
+      " [  2   3   3   3   4   1   0 431   0  16]\n",
41
+      " [  1  15  10   8   1   6   4   3 394   3]\n",
42
+      " [  4   2   1  10  11   2   0  11   8 390]]\n",
43
+      "training + evaluating time : 27.00832462310791, score = 0.9651111111111111\n",
44
+      "[[467   0   1   0   0   1   3   0   1   0]\n",
45
+      " [  1 495   4   2   1   1   0   0   0   1]\n",
46
+      " [  1   1 401   3   4   2   2   4   2   0]\n",
47
+      " [  1   0   3 460   0   8   0   2   3   2]\n",
48
+      " [  0   1   0   0 407   0   3   1   0   8]\n",
49
+      " [  0   0   0   7   0 378   4   0   1   1]\n",
50
+      " [  3   0   0   0   2   4 455   0   1   0]\n",
51
+      " [  1   3   2   1   2   1   0 443   0  10]\n",
52
+      " [  1   6   3   5   2   5   1   0 421   1]\n",
53
+      " [  1   2   1   8   4   1   0   4   2 416]]\n"
54
+     ]
55
+    }
56
+   ],
57
+   "source": [
58
+    "import matplotlib.pyplot as plt\n",
59
+    "from sklearn.neural_network import MLPClassifier\n",
60
+    "from sklearn.neighbors import KNeighborsClassifier\n",
61
+    "from sklearn.svm import SVC\n",
62
+    "from sklearn.model_selection import train_test_split\n",
63
+    "from sklearn.model_selection import KFold\n",
64
+    "from sklearn.metrics import precision_score\n",
65
+    "from sklearn.metrics import confusion_matrix\n",
66
+    "import random\n",
67
+    "import time\n",
68
+    "from sklearn.datasets import fetch_openml \n",
69
+    "\n",
70
+    "#mnist = fetch_openml('mnist_784') \n",
71
+    "\n",
72
+    "indices = [i for i in range(len(mnist.data))]\n",
73
+    "random.shuffle(indices)\n",
74
+    "indices = indices[:15000]\n",
75
+    "\n",
76
+    "data = [mnist.data.values[i] for i in indices]\n",
77
+    "target = [mnist.target[i] for i in indices]\n",
78
+    "\n",
79
+    "classifiers = [KNeighborsClassifier(3), MLPClassifier(hidden_layer_sizes = (100,)), SVC()]\n",
80
+    "xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=0.7)\n",
81
+    "\n",
82
+    "\n",
83
+    "for clf in classifiers:\n",
84
+    "    start = time.time()\n",
85
+    "    clf.fit(xtrain, ytrain)\n",
86
+    "    score = clf.score(xtest, ytest)\n",
87
+    "    end = time.time()\n",
88
+    "    print(f\"training + evaluating time : {end - start}, score = {score}\")\n",
89
+    "    print(confusion_matrix(ytest, clf.predict(xtest)))\n"
90
+   ]
91
+  },
92
+  {
93
+   "cell_type": "code",
94
+   "execution_count": null,
95
+   "id": "1aff265d",
96
+   "metadata": {},
97
+   "outputs": [],
98
+   "source": []
99
+  }
100
+ ],
101
+ "metadata": {
102
+  "kernelspec": {
103
+   "display_name": "Python 3 (ipykernel)",
104
+   "language": "python",
105
+   "name": "python3"
106
+  },
107
+  "language_info": {
108
+   "codemirror_mode": {
109
+    "name": "ipython",
110
+    "version": 3
111
+   },
112
+   "file_extension": ".py",
113
+   "mimetype": "text/x-python",
114
+   "name": "python",
115
+   "nbconvert_exporter": "python",
116
+   "pygments_lexer": "ipython3",
117
+   "version": "3.9.9"
118
+  }
119
+ },
120
+ "nbformat": 4,
121
+ "nbformat_minor": 5
122
+}

+ 32
- 0
eval.py View File

@@ -0,0 +1,32 @@
1
+import matplotlib.pyplot as plt
2
+from sklearn.neural_network import MLPClassifier
3
+from sklearn.neighbors import KNeighborsClassifier
4
+from sklearn.svm import SVC
5
+from sklearn.model_selection import train_test_split
6
+from sklearn.model_selection import KFold
7
+from sklearn.metrics import precision_score
8
+from sklearn.metrics import confusion_matrix
9
+import random
10
+import time
11
+from sklearn.datasets import fetch_openml 
12
+
13
+#mnist = fetch_openml('mnist_784') 
14
+
15
+indices = [i for i in range(len(mnist.data))]
16
+random.shuffle(indices)
17
+indices = indices[:15000]
18
+
19
+data = [mnist.data.values[i] for i in indices]
20
+target = [mnist.target[i] for i in indices]
21
+
22
+classifiers = [KNeighborsClassifier(3), MLPClassifier(hidden_layer_sizes = (100,)), SVC()]
23
+xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=0.7)
24
+
25
+
26
+for clf in classifiers:
27
+    start = time.time()
28
+    clf.fit(xtrain, ytrain)
29
+    score = clf.score(xtest, ytest)
30
+    end = time.time()
31
+    print(f"training + evaluating time : {end - start}, score = {score}")
32
+    print(confusion_matrix(ytest, clf.predict(xtest)))

Loading…
Cancel
Save