Browse Source

première version des TP

Farina Louis 2 years ago
parent
commit
277dafbfb7
3 changed files with 307 additions and 0 deletions
  1. 94
    0
      tp1.py
  2. 119
    0
      tp2.py
  3. 94
    0
      tp3.py

+ 94
- 0
tp1.py View File

@@ -0,0 +1,94 @@
1
+#!/usr/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+import matplotlib.pyplot as plt
5
+from sklearn.neighbors import KNeighborsClassifier
6
+from sklearn.model_selection import train_test_split
7
+from sklearn.model_selection import KFold
8
+import random
9
+import time
10
+
11
+
12
+"""
13
+from sklearn.datasets import fetch_openml 
14
+mnist = fetch_openml('mnist_784') 
15
+"""
16
+
17
+"""
18
+---Ex 1---
19
+images = mnist.data.values.reshape((-1, 28, 28)) 
20
+plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest") 
21
+plt.show()
22
+
23
+print(mnist.target[0])
24
+"""
25
+
26
+"""
27
+---Ex 2---
28
+"""
29
+indices = [i for i in range(len(mnist.data))]
30
+random.shuffle(indices)
31
+indices = indices[:5000]
32
+
33
+data = [mnist.data.values[i] for i in indices]
34
+target = [mnist.target[i] for i in indices]
35
+
36
+bestClf = None
37
+bestScore = 0
38
+bestK = 0
39
+scores = []
40
+
41
+kvalues = [i for i in range(2,16)]
42
+train_sizes = [0.05*i for i in range(1,20)]
43
+pvalues = [i for i in range(1,11)]
44
+
45
+k = 3
46
+t = 0.9
47
+
48
+start = time.time()
49
+xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=t)
50
+clf = KNeighborsClassifier(3, n_jobs = 1)
51
+clf.fit(xtrain, ytrain)
52
+score = clf.score(xtest, ytest)
53
+end = time.time()
54
+print(f"n_jobs = 1, training + evaluating time : {end - start}")
55
+
56
+start = time.time()
57
+xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=t)
58
+clf = KNeighborsClassifier(3, n_jobs = -1)
59
+clf.fit(xtrain, ytrain)
60
+score = clf.score(xtest, ytest)
61
+end = time.time()
62
+print(f"n_jobs = -1, training + evaluating time : {end - start}")
63
+
64
+
65
+"""
66
+#for k in kvalues:    
67
+#for t in train_sizes:
68
+for p in pvalues:
69
+    print(p)
70
+    xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=t)
71
+    clf = KNeighborsClassifier(k, p = p)
72
+    clf.fit(xtrain, ytrain)
73
+    score = clf.score(xtest, ytest)
74
+    scores += [score]
75
+    if score > bestScore:
76
+        bestClf = clf
77
+        #bestK = k
78
+        #bestSize = t
79
+        bestP = p
80
+        bestScore = score
81
+
82
+
83
+print(target[4])
84
+print(bestClf.predict(data[4].reshape(1,-1)))
85
+plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
86
+plt.show()
87
+
88
+#plt.plot(kvalues, scores)
89
+#plt.plot(train_sizes, scores)
90
+plt.plot(pvalues, scores)
91
+
92
+print(bestScore)
93
+#print(bestSize)
94
+"""

+ 119
- 0
tp2.py View File

@@ -0,0 +1,119 @@
1
+#!/usr/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+import matplotlib.pyplot as plt
5
+from sklearn.neural_network import MLPClassifier
6
+from sklearn.model_selection import train_test_split
7
+from sklearn.model_selection import KFold
8
+from sklearn.metrics import precision_score
9
+import random
10
+import time
11
+
12
+"""
13
+from sklearn.datasets import fetch_openml 
14
+mnist = fetch_openml('mnist_784') 
15
+"""
16
+
17
+"""
18
+
19
+images = mnist.data.values.reshape((-1, 28, 28)) 
20
+plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest") 
21
+plt.show()
22
+
23
+print(mnist.target[0])
24
+"""
25
+
26
+
27
+indices = [i for i in range(len(mnist.data))]
28
+random.shuffle(indices)
29
+indices = indices[:15000]
30
+
31
+data = [mnist.data.values[i] for i in indices]
32
+target = [mnist.target[i] for i in indices]
33
+
34
+bestClf = None
35
+bestScore = 0
36
+scores = []
37
+
38
+train_sizes = [0.05*i for i in range(1,20)]
39
+
40
+
41
+xtrain,  xtest,  ytrain,  ytest  =  train_test_split(data, target, train_size=0.7)
42
+
43
+"""
44
+network = MLPClassifier(hidden_layer_sizes = (50))
45
+network.fit(xtrain, ytrain)
46
+ytest_pred = clf.predict(xtest)
47
+print("Average = macro:")
48
+print(precision_score(ytest, ytest_pred, average = "macro"))
49
+print("Average = None:")
50
+print(precision_score(ytest, ytest_pred, average = None))
51
+
52
+print(target[4])
53
+print(network.predict(data[4].reshape(1,-1)))
54
+plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
55
+plt.show()
56
+"""
57
+
58
+"""
59
+n_layers = [i for i in range(1,51)]
60
+
61
+
62
+for n in n_layers:
63
+    print(n)
64
+    network = MLPClassifier(hidden_layer_sizes = (50,)*n)
65
+    network.fit(xtrain, ytrain)
66
+    scores += [network.score(xtest, ytest)]
67
+
68
+plt.plot(n_layers, scores)
69
+"""
70
+
71
+"""
72
+for _ in range(5):
73
+    n_layers = random.randint(1,3)
74
+    layer_size = tuple([random.randint(10,300) for _ in range(n_layers)]) 
75
+    print(layer_size)
76
+    
77
+    network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu")
78
+    network.fit(xtrain, ytrain)
79
+    print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
80
+"""
81
+
82
+"""
83
+solvers = ["lbfgs", "sgd", "adam"]
84
+for s in solvers:
85
+    print(s + ":")
86
+    start_time = time.time() 
87
+    network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s)
88
+    network.fit(xtrain, ytrain)
89
+    print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
90
+"""
91
+
92
+"""
93
+functions = ["identity", "logistic", "tanh", "relu"]
94
+
95
+for f in functions:
96
+    print(f + ":")
97
+    start_time = time.time() 
98
+    network = MLPClassifier(hidden_layer_sizes = (50,)*5, activation = f)
99
+    network.fit(xtrain, ytrain)
100
+    print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
101
+"""
102
+
103
+times = []
104
+scores = []
105
+
106
+for i in range(-7,7):
107
+    alpha = 10**i
108
+    print(f"alpha = {alpha}:")
109
+    start_time = time.time() 
110
+    network = MLPClassifier(hidden_layer_sizes = (50,)*5, alpha = alpha)
111
+    network.fit(xtrain, ytrain)
112
+    trainingTime = time.time() - start_time
113
+    score = network.score(xtest, ytest)
114
+    times +=[trainingTime]
115
+    scores += [score]
116
+    
117
+plt.plot([i for i in range(-7,7)], times)
118
+plt.show()
119
+plt.plot([i for i in range(-7,7)], scores)

+ 94
- 0
tp3.py View File

@@ -0,0 +1,94 @@
1
+#!/usr/bin/env python3
2
+# -*- coding: utf-8 -*-
3
+
4
+#!/usr/bin/env python3
5
+# -*- coding: utf-8 -*-
6
+
7
+import matplotlib.pyplot as plt
8
+from sklearn.neural_network import MLPClassifier
9
+from sklearn.model_selection import train_test_split
10
+from sklearn.model_selection import KFold
11
+from sklearn.metrics import precision_score
12
+import random
13
+import time
14
+
15
+"""
16
+from sklearn.datasets import fetch_openml 
17
+mnist = fetch_openml('mnist_784') 
18
+"""
19
+
20
+"""
21
+---Ex 1---
22
+images = mnist.data.values.reshape((-1, 28, 28)) 
23
+plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest") 
24
+plt.show()
25
+
26
+print(mnist.target[0])
27
+"""
28
+
29
+"""
30
+---Ex 2---
31
+"""
32
+indices = [i for i in range(len(mnist.data))]
33
+random.shuffle(indices)
34
+indices = indices[:15000]
35
+
36
+data = [mnist.data.values[i] for i in indices]
37
+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
+
45
+
46
+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
+
56
+print(target[4])
57
+print(network.predict(data[4].reshape(1,-1)))
58
+plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
59
+plt.show()
60
+"""
61
+
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
+
75
+"""
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)}")
84
+"""
85
+
86
+"""
87
+solvers = ["lbfgs", "sgd", "adam"]
88
+for s in solvers:
89
+    print(s + ":")
90
+    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
+"""

Loading…
Cancel
Save