X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP3%2Fexo1%2Ftp3_exo1.py;h=57312f2cce03a9af40692e3df7210057dc9f5186;hb=df0a762f5ec979cb0953ac13c2c92ebbea66f6d9;hp=73dd307de18e12ea18c25324c03142001c04c12d;hpb=ce56d6abaff4ef1fd9598f542d521764278ca5bb;p=TP_AA.git diff --git a/TP3/exo1/tp3_exo1.py b/TP3/exo1/tp3_exo1.py index 73dd307..57312f2 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 @@ -52,10 +52,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 @@ -68,10 +69,15 @@ X = complete(X) w = perceptron_nobias(X, Y) # 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 -# FIXME: the hyperplan equation is not correct -pl.plot([0, -1 / w[1]], [w[0] / w[1] - 1 / w[1], -w[0] / w[1] - 1 / w[1]]) +# 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()