X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP3%2Fexo2%2Ftp3_exo2.py;h=b97c6ea4df6424608316272322dc3840da29428f;hb=df0a762f5ec979cb0953ac13c2c92ebbea66f6d9;hp=e1c03dde8b616335b18aad4f442c64af8f5e1e68;hpb=f08c4a957aa85ba34302bfa77dfe92fdd5e9668c;p=TP_AA.git diff --git a/TP3/exo2/tp3_exo2.py b/TP3/exo2/tp3_exo2.py index e1c03dd..b97c6ea 100755 --- a/TP3/exo2/tp3_exo2.py +++ b/TP3/exo2/tp3_exo2.py @@ -74,10 +74,11 @@ def perceptron_nobias(X, Y): classification_error = 1 while not classification_error == 0: classification_error = 0 - for i in range(X.shape[0]): - if Y[i] * np.dot(w, X[i]) <= 0: + for x, y in zip(X, Y): + if y * np.dot(w, x) <= 0: classification_error += 1 - w = w + Y[i] * X[i] + w = w + y * x + print(classification_error) return w @@ -102,7 +103,7 @@ def apply_plongement(sample, p): 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) + output += c * s[1] * k(s[0], x) return output @@ -122,29 +123,38 @@ def perceptron_k(X, Y, k): 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: + for x, y in zip(X, Y): + if y * f_from_k(coeffs, support_set, k, x) <= 0: + if x not in support_set: + support_set.append((x, y)) + coeffs.append(1) + else: + coeffs[support_set.index((x, y))] += 1 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(classification_error) + return np.array(coeffs), np.array(support_set) -def f(x, y, w): - return +def f(w, x, y): + return w[0] + w[1] * x + w[2] * y + w[3] * x**2 + w[4] * x * y + w[5] * y**2 +pl.scatter(X[:, 0], X[:, 1], c=Y, s=training_set_size) +pl.title(u"Perceptron - hyperplan") + coeffs, support_set = perceptron_k(X, Y, k1) # coeffs, support_set = perceptron_k(X, Y, kg) -print(coeffs) -print(support_set) +res = training_set_size +for x in range(res): + for y in range(res): + if abs(f_from_k(coeffs, support_set, k1, [-3 / 2 + 3 * x / res, -3 / 2 + 3 * y / res])) < 0.01: + pl.plot(-3 / 2 + 3 * x / res, -3 / 2 + 3 * y / res, 'xr') + +# X = apply_plongement(X, plongement_phi) +# w = perceptron_nobias(X, Y) +# for x in range(res): +# for y in range(res): +# if abs(f(w, -3 / 2 + 3 * x / res, -3 / 2 + 3 * y / res)) < 0.01: +# pl.plot(-3 / 2 + 3 * x / res, -3 / 2 + 3 * y / res, 'xb') -X = apply_plongement(X, plongement_phi) -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()