|
@@ -9,6 +9,8 @@ from sklearn.neural_network import MLPClassifier
|
9
|
9
|
from sklearn.model_selection import train_test_split
|
10
|
10
|
from sklearn.model_selection import KFold
|
11
|
11
|
from sklearn.metrics import precision_score
|
|
12
|
+from sklearn.metrics import confusion_matrix
|
|
13
|
+from sklearn.svm import SVC
|
12
|
14
|
import random
|
13
|
15
|
import time
|
14
|
16
|
|
|
@@ -18,7 +20,6 @@ mnist = fetch_openml('mnist_784')
|
18
|
20
|
"""
|
19
|
21
|
|
20
|
22
|
"""
|
21
|
|
----Ex 1---
|
22
|
23
|
images = mnist.data.values.reshape((-1, 28, 28))
|
23
|
24
|
plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
|
24
|
25
|
plt.show()
|
|
@@ -27,68 +28,53 @@ print(mnist.target[0])
|
27
|
28
|
"""
|
28
|
29
|
|
29
|
30
|
"""
|
30
|
|
----Ex 2---
|
31
|
|
-"""
|
32
|
31
|
indices = [i for i in range(len(mnist.data))]
|
33
|
32
|
random.shuffle(indices)
|
34
|
33
|
indices = indices[:15000]
|
35
|
34
|
|
36
|
35
|
data = [mnist.data.values[i] for i in indices]
|
37
|
36
|
target = [mnist.target[i] for i in indices]
|
38
|
|
-
|
39
|
|
-bestClf = None
|
40
|
|
-bestScore = 0
|
41
|
|
-scores = []
|
42
|
|
-
|
43
|
|
-train_sizes = [0.05*i for i in range(1,20)]
|
44
|
|
-
|
|
37
|
+"""
|
45
|
38
|
|
46
|
39
|
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
|
47
|
|
-"""
|
48
|
|
-network = MLPClassifier(hidden_layer_sizes = (50))
|
49
|
|
-network.fit(xtrain, ytrain)
|
50
|
|
-ytest_pred = clf.predict(xtest)
|
51
|
|
-print("Average = macro:")
|
52
|
|
-print(precision_score(ytest, ytest_pred, average = "macro"))
|
53
|
|
-print("Average = None:")
|
54
|
|
-print(precision_score(ytest, ytest_pred, average = None))
|
55
|
40
|
|
|
41
|
+clf = SVC()
|
|
42
|
+clf.fit(xtrain, ytrain)
|
|
43
|
+ypred = clf.predict(xtest)
|
|
44
|
+print(confusion_matrix(ytest, ypred))
|
|
45
|
+"""
|
56
|
46
|
print(target[4])
|
57
|
|
-print(network.predict(data[4].reshape(1,-1)))
|
|
47
|
+print(clf.predict(data[4].reshape(1,-1)))
|
58
|
48
|
plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
|
59
|
49
|
plt.show()
|
60
|
50
|
"""
|
61
|
51
|
|
62
|
|
-"""
|
63
|
|
-n_layers = [i for i in range(1,51)]
|
64
|
|
-
|
65
|
|
-
|
66
|
|
-for n in n_layers:
|
67
|
|
- print(n)
|
68
|
|
- network = MLPClassifier(hidden_layer_sizes = (50,)*n)
|
69
|
|
- network.fit(xtrain, ytrain)
|
70
|
|
- scores += [network.score(xtest, ytest)]
|
71
|
|
-
|
72
|
|
-plt.plot(n_layers, scores)
|
73
|
|
-"""
|
74
|
52
|
|
75
|
53
|
"""
|
76
|
|
-for _ in range(5):
|
77
|
|
- n_layers = random.randint(1,3)
|
78
|
|
- layer_size = tuple([random.randint(10,300) for _ in range(n_layers)])
|
79
|
|
- print(layer_size)
|
80
|
|
-
|
81
|
|
- network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu")
|
82
|
|
- network.fit(xtrain, ytrain)
|
83
|
|
- print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
|
|
54
|
+kernels = ["linear", "poly", "rbf", "sigmoid"]
|
|
55
|
+for k in kernels:
|
|
56
|
+ print(k + ":")
|
|
57
|
+ start_time = time.time()
|
|
58
|
+ clf = SVC(kernel = k)
|
|
59
|
+ clf.fit(xtrain, ytrain)
|
|
60
|
+ print(f"training time = {time.time() - start_time}, score = {clf.score(xtest, ytest)}")
|
84
|
61
|
"""
|
85
|
62
|
|
86
|
63
|
"""
|
87
|
|
-solvers = ["lbfgs", "sgd", "adam"]
|
88
|
|
-for s in solvers:
|
89
|
|
- print(s + ":")
|
|
64
|
+scores = []
|
|
65
|
+times = []
|
|
66
|
+tolerance=[i*0.1 for i in range(1,10,2)]
|
|
67
|
+for C in tolerance:
|
|
68
|
+ print(f"C = {C} :")
|
90
|
69
|
start_time = time.time()
|
91
|
|
- network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s)
|
92
|
|
- network.fit(xtrain, ytrain)
|
93
|
|
- print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
|
94
|
|
-"""
|
|
70
|
+ clf = SVC(C = C)
|
|
71
|
+ clf.fit(xtrain, ytrain)
|
|
72
|
+ training_time = time.time() - start_time
|
|
73
|
+ score = clf.score(xtest, ytest)
|
|
74
|
+ times += [training_time]
|
|
75
|
+ scores += [score]
|
|
76
|
+
|
|
77
|
+plt.plot(tolerance, scores)
|
|
78
|
+plt.show()
|
|
79
|
+plt.plot(tolerance, times)
|
|
80
|
+"""
|