Add code sample for TP3 exercices.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 11 Nov 2018 16:10:18 +0000 (17:10 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 11 Nov 2018 16:10:18 +0000 (17:10 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP3/tp3_v0.py [new file with mode: 0755]

diff --git a/TP3/tp3_v0.py b/TP3/tp3_v0.py
new file mode 100755 (executable)
index 0000000..81f2324
--- /dev/null
@@ -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()