Add code sample for TP3 exercices.
[TP_AA.git] / TP3 / tp3_v0.py
1 #!/usr/bin/env python3
2
3 # -*- coding: utf-8 -*-
4 import numpy as np
5 from numpy.random import rand
6 import pylab as pl
7
8
9 def generateData(n):
10 """
11 Generates a 2D linearly separable dataset with 2n samples.
12 The third element of the sample is the label
13 """
14 xb = (rand(n) * 2 - 1) / 2 - 0.6
15 yb = (rand(n) * 2 - 1) / 2 + 0.6
16 xr = (rand(n) * 2 - 1) / 2 + 0.6
17 yr = (rand(n) * 2 - 1) / 2 - 0.6
18 inputs = []
19 for i in range(n):
20 inputs.append([xb[i], yb[i], -1])
21 inputs.append([xr[i], yr[i], 1])
22 return inputs
23
24
25 training_set = generateData(100)
26 data = np.array(training_set)
27 X = data[:, 0:2]
28 Y = data[:, -1]
29
30 pl.scatter(X[:, 0], X[:, 1], c=Y, s=100)
31 pl.show()