Create better dataset
This commit is contained in:
parent
25f1991a25
commit
8dc3637e2f
1 changed files with 20 additions and 8 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
"""TP1 - Perceptron"""
|
||||
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
|
@ -38,20 +39,31 @@ def main():
|
|||
plt.show()
|
||||
|
||||
|
||||
def generate_dataset(
|
||||
nb_data_points: int
|
||||
) -> tuple[np.ndarray[float, float], np.ndarray[int]]:
|
||||
def blur_point(point):
|
||||
x, y = point
|
||||
x = 0.3 * (random.randint(0, 2000) / 1000 - 1) * x + x
|
||||
y = 0.3 * (random.randint(0, 2000) / 1000 - 1) * x + y
|
||||
|
||||
return [x, y]
|
||||
|
||||
|
||||
def generate_dataset(nb_data_points: int):
|
||||
"""Create a dataset made of two seperate groups of points
|
||||
|
||||
Return (data, expected), where expected is the expected group for each group.
|
||||
"""
|
||||
|
||||
func1 = lambda x: 2 * x + 1
|
||||
func0 = lambda x: 0.5 * x - 3
|
||||
|
||||
# Création groupe 1
|
||||
data1 = np.random.rand(nb_data_points // 2, 2) + 3
|
||||
x1 = np.linspace(0, 8, nb_data_points)
|
||||
data1 = np.array([blur_point([x, func1(x)]) for x in x1])
|
||||
expected1 = np.array([1 for _ in data1])
|
||||
|
||||
# Création groupe 0
|
||||
data0 = np.random.rand(nb_data_points // 2, 2) + 1
|
||||
x0 = np.linspace(0, 8, nb_data_points)
|
||||
data0 = np.array([blur_point([x, func0(x)]) for x in x0])
|
||||
expected0 = np.array([0 for _ in data0])
|
||||
|
||||
# Concaténation des deux groupes
|
||||
|
@ -59,9 +71,9 @@ def generate_dataset(
|
|||
expected = np.concatenate((expected1, expected0))
|
||||
|
||||
# Mélange des données
|
||||
permutation = np.random.permutation(nb_data_points)
|
||||
data = data[permutation]
|
||||
expected = expected[permutation]
|
||||
p = np.random.permutation(len(data))
|
||||
data = data[p]
|
||||
expected = expected[p]
|
||||
|
||||
return data, expected
|
||||
|
||||
|
|
Loading…
Reference in a new issue