From 9fa4e985cd669b39149628215e437b72a66e7b49 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 15 Nov 2018 14:59:23 +0100 Subject: [PATCH] Add TP3 exo3 and TP4 exo1. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP3/exo3/learn.data | 91 +++++++++++++++++++ TP3/exo3/tp3_exo3.py | 180 ++++++++++++++++++++++++++++++++++++++ TP4/exo1/dataRegLin2D.txt | 100 +++++++++++++++++++++ TP4/exo1/tp4_exo1.py | 60 +++++++++++++ 4 files changed, 431 insertions(+) create mode 100644 TP3/exo3/learn.data create mode 100755 TP3/exo3/tp3_exo3.py create mode 100644 TP4/exo1/dataRegLin2D.txt create mode 100755 TP4/exo1/tp4_exo1.py diff --git a/TP3/exo3/learn.data b/TP3/exo3/learn.data new file mode 100644 index 0000000..7e79164 --- /dev/null +++ b/TP3/exo3/learn.data @@ -0,0 +1,91 @@ +((16.695999571453935, -0.92787346122700254), 1) +((15.850388825759982, 0.47211252902514111), -1) +((10.622623441731935, -0.31499449528815537), -1) +((7.0784268427453174, -0.17699681107630116), 1) +((19.069159584397536, -0.027484910013680031), 1) +((15.991549733559815, 0.23823004013080151), -1) +((4.8615293559045814, 0.057468964112846743), -1) +((14.934748753321383, 0.076892110240574185), 1) +((4.888686399955084, 0.1430260760559372), -1) +((0.85702943648359398, 0.15123320778307159), 1) +((10.124866276079473, 0.85197942671311888), -1) +((4.8958991656835487, 0.69585576409453753), -1) +((4.8391930923249742, -0.28690936737134098), -1) +((5.3995102888136941, 0.72744167839658624), -1) +((9.40391434298669, 0.35725004144808126), -1) +((12.909034200628621, -0.4232915510035673), 1) +((0.87017159126183463, 0.75163187899954842), 1) +((6.9562049949800713, -0.90973561718081219), 1) +((7.1717649919440785, -0.28842490811701826), 1) +((17.615529564829849, -0.18592723684801538), -1) +((7.132240565453019, 0.13114464704213868), 1) +((6.9830817325630479, -0.38151006148972355), 1) +((8.4294369469384236, 0.13814115702916974), 1) +((0.75557290592005, 0.28604087736928352), 1) +((0.83922292474758242, 0.58015703686809039), 1) +((0.70135832302924195, 0.50818652333324454), 1) +((13.573423185282881, 0.96611173973180287), -1) +((7.672704708982014, -0.74103813009467223), 1) +((18.945112446592461, -0.89165104626659741), 1) +((10.455526984693195, 0.96700036169426595), -1) +((9.7212729427924529, -0.35321827008440287), 1) +((10.545228385763641, 0.027840325333361893), -1) +((11.948858391362833, 0.050800412258009686), -1) +((4.9657521885585005, 0.18300050771400911), -1) +((16.26401493653054, 0.30667600691224917), -1) +((17.9245310070866, 0.94639881861859765), -1) +((5.9247269215840763, 0.31364953306796961), -1) +((10.345035469877903, -0.47195264308674556), -1) +((6.8556689494737011, -0.9997647068865394), 1) +((8.3550190383955414, 0.99922894229815107), -1) +((3.2101466401133827, -0.64659653185031241), 1) +((15.344817996874134, 0.51704626505728801), -1) +((11.644537982887906, 0.49396793371846193), -1) +((13.752898511406602, 0.76803163383677786), 1) +((19.243862164394372, -0.92809967348090061), 1) +((17.138430318422571, -0.39239639049625774), -1) +((17.823828075185403, -0.11334270685581394), -1) +((16.160613938611476, 0.54441903255144064), -1) +((14.243439703916451, 0.72171673081152043), 1) +((0.49394728724601711, -0.47594397141280265), 1) +((13.254018719315731, 0.95756943670582961), -1) +((17.684416102436089, 0.64668519761196608), -1) +((8.8322321649974498, -0.09109729319185611), 1) +((18.649532059529726, 0.24733323013667219), -1) +((1.0436632280681279, -0.2521501001074713), 1) +((11.664389239843725, 0.86732297881487663), -1) +((16.428288200233997, -0.15358839214361608), -1) +((2.1382085467537792, -0.50680711352825325), 1) +((10.818139201342889, -0.12736814980256428), -1) +((11.255868945207521, 0.66711418146243395), -1) +((8.2998543493818016, -0.13730142331252471), 1) +((16.347602957223614, -0.61805196879434399), 1) +((18.320815963491508, -0.64466928264091661), 1) +((13.164792547880657, -0.13380109607084556), 1) +((0.4572122629454789, -0.38345621696307575), 1) +((15.620961409110869, 0.45970561036366453), -1) +((18.163225598542038, -0.7529684013103275), 1) +((6.0100221084338257, 0.28991615155035189), -1) +((16.815402002750851, -0.19380648631387531), -1) +((6.97100180671743, -0.37910735915631522), 1) +((8.8541549430877957, 0.11569878910064912), 1) +((10.915144936112371, -0.51182807783035789), -1) +((13.244618552298217, 0.6038180093761738), 1) +((1.582858749571483, -0.0039123101698492757), 1) +((1.2705975471954711, -0.49375247128526722), 1) +((2.7510701789850112, -0.76463023088040183), 1) +((8.3629887122557598, 0.53435800228334052), 1) +((0.9192421784134841, -0.31942277867475077), 1) +((4.0463017601103912, 0.11392149645586547), -1) +((6.3720540497103739, -0.55465541087586034), 1) +((15.290088491708588, 0.67986120735910172), -1) +((1.5745431952965294, -0.73582714870824661), 1) +((17.959851292975642, 0.099503634520412776), -1) +((7.7307687650278183, 0.60567816327110124), 1) +((17.878056546463807, 0.10820081597007536), -1) +((12.246109090184902, -0.77100807235001345), 1) +((2.5213015873727818, 0.084873128476091519), 1) +((6.847971329921716, 0.82329735128559234), -1) +((8.1535084884322178, 0.43240560983320431), 1) +((19.237326622160804, 0.95388209408948965), -1) +((11.041491819881665, -0.12069818862895776), -1) diff --git a/TP3/exo3/tp3_exo3.py b/TP3/exo3/tp3_exo3.py new file mode 100755 index 0000000..3476f6f --- /dev/null +++ b/TP3/exo3/tp3_exo3.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python3 + +# -*- coding: utf-8 -*- +import numpy as np +from numpy.random import rand +import pylab as pl + + +def generateData(n): + """ + Generates a 2D linearly separable dataset with 2n samples. + The third element of the sample is the label + """ + linear_offset = 0.6 + xb = (rand(n) * 2 - 1) / 2 - linear_offset + yb = (rand(n) * 2 - 1) / 2 + linear_offset + xr = (rand(n) * 2 - 1) / 2 + linear_offset + yr = (rand(n) * 2 - 1) / 2 - linear_offset + inputs = [] + for i in range(n): + inputs.append([xb[i], yb[i], -1]) + inputs.append([xr[i], yr[i], 1]) + return inputs + + +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 + yb = (rand(n) * 2 - 1) / 2 + xr = (rand(n) * 2 - 1) / 2 + 1.5 + yr = (rand(n) * 2 - 1) / 2 - 0.5 + inputs = [] + for i in range(n): + inputs.append([xb[i], yb[i], -1]) + inputs.append([xr[i], yr[i], 1]) + return inputs + + +def generateData3(n): + """ + 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 + xb = (rand(n) * 2 - 1) / 2 + yb = (rand(n) * 2 - 1) / 2 + # (xr, yr) est dans le carré centré à l’origine de côté 3 + xr = 3 * (rand(4 * n) * 2 - 1) / 2 + yr = 3 * (rand(4 * n) * 2 - 1) / 2 + inputs = [] + for i in range(n): + inputs.append([xb[i], yb[i], -1]) + for i in range(4 * n): + # on ne conserve que les points extérieurs au carré centré à l’origine + # de côté 2 + if abs(xr[i]) >= 1 or abs(yr[i]) >= 1: + inputs.append([xr[i], yr[i], 1]) + return inputs + + +def readData(file): + f = open(file, "r") + training_set = [] + x = f.readline() + while x: + x_eval = eval(x) + training_set.append([x_eval[0][0], x_eval[0][1], x_eval[1]]) + x = f.readline() + f.close() + return training_set + + +training_set_size = 150 +# training_set = generateData3(training_set_size) +training_set = readData("learn.data") +data = np.array(training_set) +X = data[:, 0:2] +Y = data[:, -1] + + +def perceptron_nobias(X, Y): + w = np.zeros([len(X[0])]) + # Go in the loop at least one time + classification_error = 1 + while not classification_error == 0: + classification_error = 0 + for x, y in zip(X, Y): + if y * np.dot(w, x) <= 0: + classification_error += 1 + w = w + y * x + print(classification_error) + return w + + +def complete(sample): + new_sample = np.insert(sample, len(sample[0]), [1], axis=1) + return np.array(new_sample) + + +def plongement_phi(sample_element): + return [1, sample_element[0], sample_element[1], sample_element[0]**2, + sample_element[0] * sample_element[1], sample_element[1]**2] + + +def apply_plongement(sample, p): + output = [] + for i in range(sample.shape[0]): + current = p(sample[i]) + output.append(current) + 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[1] * k(s[0], x) + return output + + +def k1(X1, X2): + return 1 + X1[0] * X2[0] + X1[1] * X2[1] + X1[0]**2 * X2[0]**2 \ + + X1[0] * X1[1] * X2[0] * X2[1] + X1[1]**2 * X2[1]**2 + + +def kg(x, y): + # sigma = 20 # do not converge + # sigma = 10 # do not converge + sigma = 1 # overfitting + # sigma = 0.5 # overfitting + # sigma = 0.2 # overfitting + return np.exp(-((x[0] - y[0])**2 + (x[1] - y[1])**2) / sigma**2) + + +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 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 + print(classification_error) + return np.array(coeffs), np.array(support_set) + + +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) +pl.title(u"Perceptron - prolontaged hyperplan") + +# k = k1 +# coeffs, support_set = perceptron_k(X, Y, k) +k = kg +coeffs, support_set = perceptron_k(X, Y, k) +res = training_set_size +for x in range(res): + for y in range(res): + if abs(f_from_k(coeffs, support_set, k, [-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') + +pl.show() diff --git a/TP4/exo1/dataRegLin2D.txt b/TP4/exo1/dataRegLin2D.txt new file mode 100644 index 0000000..021ed56 --- /dev/null +++ b/TP4/exo1/dataRegLin2D.txt @@ -0,0 +1,100 @@ + 0.974633 -0.792363 -1.55824 + 0.322351 1.8034 1.8891 + 1.35589 0.389593 0.114214 + 0.0859725 -1.0693 -2.02948 + -0.6436 -0.400372 -1.43223 + -0.88582 0.602299 -0.0590777 + -0.05727 -0.68016 -1.62127 + 1.3165 -0.570268 -1.07097 + -1.2269 0.0372067 -0.670011 + -0.307733 -0.217767 -1.0776 + 2.01961 -0.143457 -0.310584 + -0.346529 -0.474998 -1.62418 + 0.624072 -0.437087 -1.24966 + 1.50335 1.33349 1.51722 + 1.00832 0.019365 -0.44385 + 2.61706 1.87667 2.59865 + -2.26652 -0.0321358 -1.29284 + 1.30894 2.11607 2.47544 + 1.09092 1.04312 1.04046 + 1.20046 0.469415 0.352557 + 0.780383 0.0111708 -0.463614 + -0.10054 -1.40242 -2.61138 + 1.21726 1.06692 1.17218 + 2.33033 0.0444168 -0.0603761 + 0.4038 1.44355 1.4526 + -1.35011 -0.320975 -1.5722 + -1.52264 -0.050186 -1.12542 + -0.61236 -0.782373 -1.79026 + 0.0934571 0.36143 -0.192081 + 0.602785 -0.128348 -0.853118 + 1.4923 0.705827 0.486798 + -0.973672 -0.578001 -1.71765 + -0.0543634 -0.192952 -0.913405 + 0.0864153 -1.98039 -3.42191 + -0.0877569 0.829806 0.395068 + -0.16916 -0.650094 -1.53461 + 0.190051 0.590431 0.112188 + 2.24335 -0.540746 -0.834482 + 2.29005 1.75977 2.41157 + 1.19283 -0.217893 -0.618301 + -0.0130601 -0.775068 -1.71921 + 1.50782 0.26217 -0.0075728 + -0.495035 1.38466 1.14518 + -2.57561 0.0101092 -1.40083 + -0.550798 0.793548 0.314286 + 0.33286 -0.790275 -1.74584 + 0.600883 0.363704 -0.104026 + -0.967231 -0.124756 -1.06134 + 0.86357 0.915796 0.880184 + -2.00479 -1.20705 -2.73107 + -1.5744 -1.06133 -2.49398 + -1.24246 -0.0532174 -1.03638 + -0.142512 0.437945 0.0117141 + -0.190588 -1.13174 -2.02514 + 0.0717578 0.657072 0.233795 + -0.847836 1.68174 1.39365 + -2.61233 -0.160418 -1.47194 + -0.150264 -0.0907573 -0.756651 + 1.01469 -1.21428 -2.01372 + -0.123068 0.730712 0.265598 + 1.25154 -0.526393 -0.918618 + 0.572973 -0.0325019 -0.449671 + 0.744767 0.567598 0.0952907 + 0.767336 1.28563 1.1117 + 0.27401 0.0259356 -0.583271 + 0.0387451 0.863472 0.307276 + 1.37652 0.883576 0.864879 + -0.24544 -1.50491 -2.83699 + -1.01056 0.463139 -0.223184 + -0.266867 -0.7196 -1.69465 + -0.698632 0.320259 -0.451467 + -0.487443 -0.578519 -1.78539 + 0.111006 -2.07078 -3.45287 + -0.688077 -2.78516 -4.82784 + -1.5226 1.12646 0.564719 + 0.717233 0.685768 0.58831 + -0.380492 0.59173 -0.028383 + 1.00786 -0.027513 -0.314784 + 0.376207 0.463145 -0.153662 + -1.77862 0.336624 -0.573957 + -0.669653 0.093832 -0.678172 + -0.478987 1.15075 0.666276 + -0.275699 -0.811538 -1.81975 + -0.185535 0.161834 -0.566101 + 2.39935 -0.560211 -0.890581 + 0.478475 -0.380631 -1.1044 + 0.0151748 0.413237 -0.211421 + 1.8743 -0.17471 -0.471368 + -0.034751 0.165149 -0.764119 + 0.440518 0.61982 0.255088 + 1.41156 -0.15232 -0.397338 + 2.04356 0.799842 0.940545 + 1.96347 0.652255 0.764774 + 0.63714 0.24976 -0.142234 + 0.189887 -0.417674 -1.21615 + -1.3693 0.262116 -0.663902 + -1.66794 1.34258 1.03625 + 0.0747798 0.622185 0.0616578 + -0.292759 -0.473951 -1.39402 + 0.230967 -0.255208 -1.00798 \ No newline at end of file diff --git a/TP4/exo1/tp4_exo1.py b/TP4/exo1/tp4_exo1.py new file mode 100755 index 0000000..06bc95b --- /dev/null +++ b/TP4/exo1/tp4_exo1.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# -*- coding: utf-8 -*- +import numpy as np +import pylab as pl +from mpl_toolkits.mplot3d import Axes3D + + +data = np.loadtxt("dataRegLin2D.txt") +X = data[:, 0:2] +Y = data[:, -1] + + +def complete(sample): + if sample.ndim > 1: + ones = np.ones((sample.shape[0], 1)) + new_sample = np.append(sample, ones, axis=-1) + else: + new_sample = [] + for s in sample: + s = [s, 1] + new_sample.append(s) + return np.array(new_sample) + + +def train_regression(X, Y): + X = complete(X) + return np.dot(np.dot(np.linalg.inv(np.dot(np.transpose(X), X)), np.transpose(X)), Y) + + +def predict(x, w): + return np.dot(w[:len(w) - 1], x) + w[-1] + + +def error(X, Y, w, idx): + err = 0.0 + for i in range(len(X)): + y = predict(X[i, :idx], w) + err += (y - Y[i])**2 + err /= len(X) + return err + + +fig = pl.figure() +ax = fig.add_subplot(131, projection='3d') +ax.scatter(X[:, 0], X[:, 1], Y) +w1 = train_regression(X, Y) +print(error(X, Y, w1, 2)) + +ax = fig.add_subplot(132) +ax.scatter(X[:, 0], Y[:]) +w2 = train_regression(X[:, 0], Y) +print(error(X[:, 0].reshape((len(X), 1)), Y, w2, 1)) + +ax = fig.add_subplot(133) +ax.scatter(X[:, 1], Y[:]) +w3 = train_regression(X[:, 1], Y) +print(error(X[:, 1].reshape((len(X), 1)), Y, w3, 1)) + +pl.show() -- 2.34.1