Minor changes, calcule precision instead of error rate

This commit is contained in:
flyingscorpio@clevo 2023-01-25 21:13:11 +01:00
parent d21b879428
commit 8860354c81

View file

@ -17,7 +17,7 @@ def main():
data, expected = generate_dataset(nb_data_points)
nb_training_points = int(nb_data_points * 0.8)
# Affichage des points
# Affichage des points d'entraînement
plot_dots(data, expected)
# Apprentissage
@ -33,7 +33,11 @@ def main():
testing_data = data[nb_training_points:]
testing_expected = expected[nb_training_points:]
predicted = [perceptron.predict(x) for x in testing_data]
print(error_rate(predicted, testing_expected))
# Calculation de la précision du modèle
print(precision(predicted, testing_expected))
# Affichage des points de test, classés par le modèle
plot_dots(testing_data, testing_expected)
plt.show()
@ -57,8 +61,8 @@ def generate_dataset(nb_data_points: int):
func1 = lambda x: 2 * x + 1
func0 = lambda x: 0.5 * x - 3
# Colliding dataset
#func1 = lambda x: 2 * x + 1
#func0 = lambda x: 1.5 * x - 2
func1 = lambda x: 2 * x + 1
func0 = lambda x: 1.5 * x - 2
# Création groupe 1
x1 = np.linspace(0, 8, nb_data_points)
@ -82,16 +86,16 @@ def generate_dataset(nb_data_points: int):
return data, expected
def error_rate(predicted, expected):
"""Calculate the error rate of a prediction set"""
def precision(predicted, expected):
"""Calculate the precision of the model"""
assert len(predicted) == len(expected)
errors = 0
good = 0
for i, item in enumerate(predicted):
if item != expected[i]:
errors += 1
return errors / len(predicted)
if item == expected[i]:
good += 1
return good / len(predicted)
def plot_dots(data, data_class):