|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +def elastic_net_gradient_descent( |
| 5 | + X: np.ndarray, |
| 6 | + y: np.ndarray, |
| 7 | + alpha1: float = 0.1, |
| 8 | + alpha2: float = 0.1, |
| 9 | + learning_rate: float = 0.01, |
| 10 | + max_iter: int = 1000, |
| 11 | + tol: float = 1e-4, |
| 12 | +) -> tuple: |
| 13 | + """ |
| 14 | + Implement Elastic Net regression using gradient descent. |
| 15 | +
|
| 16 | + Parameters: |
| 17 | + X: Feature matrix (n_samples, n_features) |
| 18 | + y: Target values (n_samples,) |
| 19 | + alpha1: L1 regularization strength (Lasso) |
| 20 | + alpha2: L2 regularization strength (Ridge) |
| 21 | + learning_rate: Step size for gradient descent |
| 22 | + max_iter: Maximum number of iterations |
| 23 | + tol: Convergence tolerance |
| 24 | +
|
| 25 | + Returns: |
| 26 | + tuple: (weights, bias) |
| 27 | + """ |
| 28 | + n_samples, n_features = X.shape |
| 29 | + |
| 30 | + # Initialize weights and bias |
| 31 | + weights = np.zeros(n_features) |
| 32 | + bias = 0 |
| 33 | + |
| 34 | + for _ in range(max_iter): |
| 35 | + # Make predictions |
| 36 | + y_pred = np.dot(X, weights) + bias |
| 37 | + |
| 38 | + # Calculate residuals |
| 39 | + error = y_pred - y |
| 40 | + |
| 41 | + # Calculate gradients |
| 42 | + grad_w = (1 / n_samples) * np.dot(X.T, error) + alpha1 * np.sign(weights) + 2 * alpha2 * weights |
| 43 | + grad_b = (1 / n_samples) * np.sum(error) |
| 44 | + |
| 45 | + # Update weights and bias |
| 46 | + weights -= learning_rate * grad_w |
| 47 | + bias -= learning_rate * grad_b |
| 48 | + |
| 49 | + # Check for convergence |
| 50 | + if np.linalg.norm(grad_w, ord=1) < tol: |
| 51 | + break |
| 52 | + |
| 53 | + return weights, bias |
| 54 | + |
| 55 | + |
| 56 | +def test_elastic_net_gradient_descent(): |
| 57 | + """Test cases for Elastic Net implementation""" |
| 58 | + |
| 59 | + # Test case 1: Simple linear relationship |
| 60 | + X = np.array([[0, 0], [1, 1], [2, 2]]) |
| 61 | + y = np.array([0, 1, 2]) |
| 62 | + alpha1, alpha2 = 0.1, 0.1 |
| 63 | + |
| 64 | + weights, bias = elastic_net_gradient_descent( |
| 65 | + X, y, alpha1=alpha1, alpha2=alpha2, learning_rate=0.01, max_iter=1000 |
| 66 | + ) |
| 67 | + |
| 68 | + expected_weights = np.array([0.37, 0.37]) |
| 69 | + expected_bias = 0.24 |
| 70 | + |
| 71 | + assert np.allclose(weights, expected_weights, atol=0.05), "Test case 1 failed" |
| 72 | + assert np.isclose(bias, expected_bias, atol=0.05), "Test case 1 failed" |
| 73 | + |
| 74 | + # Test case 2: More complex relationship |
| 75 | + X = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]) |
| 76 | + y = np.array([1, 2, 3, 4, 5]) |
| 77 | + alpha1, alpha2 = 0.1, 0.1 |
| 78 | + |
| 79 | + weights, bias = elastic_net_gradient_descent( |
| 80 | + X, y, alpha1=alpha1, alpha2=alpha2, learning_rate=0.01, max_iter=2000 |
| 81 | + ) |
| 82 | + |
| 83 | + expected_weights = np.array([0.42, 0.48]) |
| 84 | + expected_bias = 0.68 |
| 85 | + |
| 86 | + assert np.allclose(weights, expected_weights, atol=0.05), "Test case 2 failed" |
| 87 | + assert np.isclose(bias, expected_bias, atol=0.05), "Test case 2 failed" |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + test_elastic_net_gradient_descent() |
| 92 | + print("All Elastic Net tests passed!") |
0 commit comments