mirror of
https://github.com/Ladebeze66/ft_linear_regression.git
synced 2026-11-09 13:47:18 +01:00
Docker
This commit is contained in:
23
src/evaluate_model.py
Normal file
23
src/evaluate_model.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import numpy as np
|
||||
|
||||
def evaluate_model(df, theta_0, theta_1):
|
||||
"""Calcule la précision du modèle en utilisant l'erreur quadratique moyenne (MSE) et R²."""
|
||||
X = df["km_norm"].values
|
||||
y = df["price_norm"].values
|
||||
|
||||
# Prédictions du modèle
|
||||
y_pred = theta_0 + theta_1 * X
|
||||
|
||||
# Erreur quadratique moyenne (MSE)
|
||||
mse = np.mean((y - y_pred) ** 2)
|
||||
|
||||
# Coefficient de détermination (R²)
|
||||
ss_total = np.sum((y - np.mean(y)) ** 2)
|
||||
ss_residual = np.sum((y - y_pred) ** 2)
|
||||
r2_score = 1 - (ss_residual / ss_total)
|
||||
|
||||
print(f"\nPrécision du modèle :")
|
||||
print(f"🔹 Erreur quadratique moyenne (MSE) : {mse:.4f}")
|
||||
print(f"🔹 Coefficient de détermination (R²) : {r2_score:.4f} (plus proche de 1 = meilleur)")
|
||||
|
||||
return mse, r2_score
|
||||
@@ -3,6 +3,7 @@ import pandas as pd
|
||||
from data_process import load_and_process_data
|
||||
from gradient_descent import gradient_descent
|
||||
from visualization import plot_cost_history, plot_regression_line
|
||||
from evaluate_model import evaluate_model
|
||||
|
||||
def train_model():
|
||||
"""
|
||||
@@ -16,7 +17,7 @@ def train_model():
|
||||
theta_0_init = 0
|
||||
theta_1_init = 1
|
||||
learning_rate = 0.1 # Taux d'apprentissage
|
||||
iterations = 1000
|
||||
iterations = 10000
|
||||
|
||||
# Exécuter la descente de gradient
|
||||
theta_0_opt, theta_1_opt, cost_history = gradient_descent(X, y, theta_0_init, theta_1_init, learning_rate, iterations)
|
||||
@@ -31,6 +32,7 @@ def train_model():
|
||||
plot_cost_history(cost_history) # Affichage de l'historique du coût
|
||||
# Affichage de la régression linéaire
|
||||
plot_regression_line(df, theta_0_opt, theta_1_opt)
|
||||
evaluate_model(df, theta_0_opt, theta_1_opt)
|
||||
|
||||
return theta_0_opt, theta_1_opt
|
||||
|
||||
|
||||
Reference in New Issue
Block a user