Fixes and code cleanup.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Nov 2018 18:14:29 +0000 (19:14 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Nov 2018 18:14:29 +0000 (19:14 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP3/exo2/tp3_exo2.py

index c59b671479c474d3b2800ae39e4bbc0c3aa559da..78392d9a193fcc9fbab2d0048a22bfd3d0daf6d2 100755 (executable)
@@ -41,7 +41,7 @@ def generateData2(n):
 
 def generateData3(n):
     """
-    Generates a 2D linearly separable dataset with 2n samples.
+    Generates a 2D linearly separable dataset with about 2n samples.
     The third element of the sample is the label
     """
     # (xb, yb) est dans le carré centré à l’origine de côté 1
@@ -62,7 +62,7 @@ def generateData3(n):
 
 
 training_set_size = 150
-training_set = generateData2(training_set_size)
+training_set = generateData3(training_set_size)
 data = np.array(training_set)
 X = data[:, 0:2]
 Y = data[:, -1]
@@ -86,14 +86,14 @@ def complete(sample):
     return np.array(new_sample)
 
 
-def plongement(sample_element):
+def plongement_phi(sample_element):
     return [1, sample_element[0], sample_element[1], sample_element[0] * sample_element[0], sample_element[0] * sample_element[1], sample_element[1] * sample_element[1]]
 
 
-def apply_plongement(sample):
+def apply_plongement(sample, p):
     output = []
     for i in range(sample.shape[0]):
-        current = plongement(sample[i])
+        current = p(sample[i])
         output.append(current)
     return np.array(output)
 
@@ -106,7 +106,11 @@ def f_from_k(coeffs, support_set, k, x):
 
 
 def k1(X1, X2):
-    return 1 + X1[0] * X2[0] + X1[1] * X2[1] + X1[0] * X1[0] * X2[0] * X2[0] + X1[0] * X1[1] * X2[0] * X1[1] + X1[1] * X2[1] * X2[1]
+    return 1 + X1[0] * X2[0] + X1[1] * X2[1] + X1[0] * X1[0] * X2[0] * X2[0] + X1[0] * X1[1] * X2[0] * X2[1] + X1[1] * X1[1] * X2[1] * X2[1]
+
+
+def kg(x, y, sigma=10):
+    return np.exp(-((x[0] - y[0])**2 + (x[1] - y[1])**2) / sigma**2)
 
 
 def perceptron_k(X, Y, k):
@@ -127,8 +131,9 @@ def perceptron_k(X, Y, k):
 
 
 print(perceptron_k(X, Y, k1))
+# print(perceptron_k(X, Y, kg))
 
-X = apply_plongement(X)
+X = apply_plongement(X, plongement_phi)
 w = perceptron_nobias(X, Y)
 print(w)