Extension de la régression linéaire à deux variables d’entrée.
Génération de données, entraînement, visualisation 3D interactive et
évaluation.
1. Génération des données
# Génération de 100 points, 2 variablesimport numpy as np
from sklearn.datasets import make_regression
x, y = make_regression(n_samples=100, n_features=2, noise=10)
y = y.reshape(y.shape[0], 1)
# Génération de la matrice X
X = np.hstack((x, np.ones((x.shape[0], 1))))
2. Modèle, coût, descente de gradient
def modele(X, theta):
return X.dot(theta)
def cost_function(X, y, theta):
m = len(y)
return 1/(2*m)*np.sum((modele(X, theta)-y)**2)
def grad(X, y, theta):
m = len(y)
return 1/m*X.T.dot(modele(X, theta)-y)
def gradient_descent(X, y, theta, learning_rate, n_iterations):
cost_history = np.zeros(n_iterations)
for i inrange(n_iterations):
theta = theta - learning_rate * grad(X, y, theta)
cost_history[i] = cost_function(X, y, theta)
return theta, cost_history
theta = np.random.randn(3,1)
theta_final, cost_history = gradient_descent(X, y, theta, 0.01, 1000)
3. Visualisation 3D interactive
4. Évaluation du modèle
def coef_determination(y, pred):
u = ((y - pred)**2).sum()
v = ((y - y.mean())**2).sum()
return 1-u/v
print(coef_determination(y, predictions))
Le coefficient de détermination (R²) mesure la qualité
de l’ajustement du modèle.