From 4bab9ffbab19664454d31b11a99a5f3d7c14d492 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 13 Nov 2018 18:34:52 +0100 Subject: [PATCH] Add some more implementation of TP3 exo2. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP3/exo2/tp3_exo2.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/TP3/exo2/tp3_exo2.py b/TP3/exo2/tp3_exo2.py index c92d590..c59b671 100755 --- a/TP3/exo2/tp3_exo2.py +++ b/TP3/exo2/tp3_exo2.py @@ -98,8 +98,40 @@ def apply_plongement(sample): return np.array(output) +def f_from_k(coeffs, support_set, k, x): + output = 0 + for c, s in zip(coeffs, support_set): + output += c * s[0] * k(s[1], x) + return output + + +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] + + +def perceptron_k(X, Y, k): + coeffs = [] + support_set = [] + # Go in the loop at least one time + classification_error = 1 + while not classification_error == 0: + classification_error = 0 + for i in range(X.shape[0]): + if Y[i] * f_from_k(coeffs, support_set, k, X[i]) <= 0: + classification_error += 1 + support_set.append([Y[i], X[i]]) + coeffs.append(1) + else: + coeffs[len(coeffs) - 1] = coeffs[len(coeffs) - 1] + 1 + return coeffs, support_set + + +print(perceptron_k(X, Y, k1)) + X = apply_plongement(X) w = perceptron_nobias(X, Y) +print(w) + pl.scatter(X[:, 0], X[:, 1], c=Y, s=training_set_size) pl.title(u"Perceptron - hyperplan") pl.show() -- 2.34.1