X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP3%2Fexo1%2Ftp3_exo1.py;h=02416ba46171c462dde7934537888c37661bdd3f;hb=f08c4a957aa85ba34302bfa77dfe92fdd5e9668c;hp=dfd04d831840fa1024b5a45c35073325826d4ffa;hpb=6f4ffbd78d75ea191f5d7708c1fe3a6f5ae734b5;p=TP_AA.git diff --git a/TP3/exo1/tp3_exo1.py b/TP3/exo1/tp3_exo1.py index dfd04d8..02416ba 100755 --- a/TP3/exo1/tp3_exo1.py +++ b/TP3/exo1/tp3_exo1.py @@ -28,7 +28,7 @@ def generateData2(n): Generates a 2D linearly separable dataset with 2n samples. The third element of the sample is the label """ - xb = (rand(n) * 2 - 1) / 2 - 0.5 + xb = (rand(n) * 2 - 1) / 2 + 0.5 yb = (rand(n) * 2 - 1) / 2 xr = (rand(n) * 2 - 1) / 2 + 1.5 yr = (rand(n) * 2 - 1) / 2 - 0.5 @@ -39,8 +39,8 @@ def generateData2(n): return inputs -training_set_size = 100 -training_set = generateData(training_set_size) +training_set_size = 150 +training_set = generateData2(training_set_size) data = np.array(training_set) X = data[:, 0:2] Y = data[:, -1] @@ -54,17 +54,29 @@ def perceptron_nobias(X, Y): classification_error = 0 for i in range(X.shape[0]): if Y[i] * np.dot(w, X[i]) <= 0: - classification_error = classification_error + 1 + classification_error += 1 w = w + Y[i] * X[i] return w def complete(sample): - sample = np.expand_dims(sample, axis=0) - return sample + new_sample = np.insert(sample, len(sample[0]), [1], axis=1) + return np.array(new_sample) +X = complete(X) w = perceptron_nobias(X, Y) -pl.plot([-1, 1], [w[0] / w[1], -w[0] / w[1]]) +# w is orthogonal to the hyperplan +# with generateData +# plot arguments format is pl.plot([x1,x2],[y1,y2]) +# w[0]x + w[1]y = 0, so y = -w[0]x / w[1] +# pl.plot([-1, 1], [w[0] / w[1], -w[0] / w[1]]) +# with generateData2 and complete +# w[0]x + w[1]y + w[2] = 0, so y = -(w[0]x + w[2]) / w[1] +x_start1 = -0.5 +x_start2 = 2.5 +pl.plot([x_start1, x_start2], [-(w[0] * x_start1 + w[2]) / + w[1], -(w[0] * x_start2 + w[2]) / w[1]]) pl.scatter(X[:, 0], X[:, 1], c=Y, s=training_set_size) +pl.title(u"Perceptron - hyperplan") pl.show()