From: Jérôme Benoit Date: Sun, 11 Nov 2018 16:10:18 +0000 (+0100) Subject: Add code sample for TP3 exercices. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;ds=inline;h=9631fffca8a6a92cfe5c602ecbe0e305d8dd6d0e;p=TP_AA.git Add code sample for TP3 exercices. Signed-off-by: Jérôme Benoit --- diff --git a/TP3/tp3_v0.py b/TP3/tp3_v0.py new file mode 100755 index 0000000..81f2324 --- /dev/null +++ b/TP3/tp3_v0.py @@ -0,0 +1,31 @@ +#!/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 + """ + xb = (rand(n) * 2 - 1) / 2 - 0.6 + yb = (rand(n) * 2 - 1) / 2 + 0.6 + xr = (rand(n) * 2 - 1) / 2 + 0.6 + yr = (rand(n) * 2 - 1) / 2 - 0.6 + inputs = [] + for i in range(n): + inputs.append([xb[i], yb[i], -1]) + inputs.append([xr[i], yr[i], 1]) + return inputs + + +training_set = generateData(100) +data = np.array(training_set) +X = data[:, 0:2] +Y = data[:, -1] + +pl.scatter(X[:, 0], X[:, 1], c=Y, s=100) +pl.show()