Skip to content

Commit 0b242ea

Browse files
committed
chore: new article
1 parent 63ab44f commit 0b242ea

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

_posts/2025-06-05-Least_Angle_Regression.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,47 @@ Least Angle Regression occupies an elegant middle ground between computational e
119119
Going forward, LARS continues to inspire variations and improvements, including hybrid methods that incorporate Bayesian priors or non-linear transformations. Additionally, integrating LARS into deep learning architectures or extending it to generalized linear models are active areas of research.
120120

121121
As machine learning and statistics continue to evolve in tandem, algorithms like LARS remind us that simplicity and insight often go hand-in-hand.
122+
123+
# Appendix: Python Example of Least Angle Regression (LARS)
124+
125+
```python
126+
import numpy as np
127+
from sklearn import datasets
128+
from sklearn.linear_model import Lars
129+
from sklearn.model_selection import train_test_split
130+
from sklearn.metrics import mean_squared_error, r2_score
131+
import matplotlib.pyplot as plt
132+
133+
# Load a high-dimensional dataset (e.g., diabetes or synthetic)
134+
X, y = datasets.make_regression(n_samples=100, n_features=50, n_informative=10, noise=0.1, random_state=42)
135+
136+
# Split the dataset into training and testing sets
137+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
138+
139+
# Initialize and fit the LARS model
140+
lars = Lars(n_nonzero_coefs=10) # Limit to 10 predictors for sparsity
141+
lars.fit(X_train, y_train)
142+
143+
# Predict on test data
144+
y_pred = lars.predict(X_test)
145+
146+
# Evaluate model performance
147+
mse = mean_squared_error(y_test, y_pred)
148+
r2 = r2_score(y_test, y_pred)
149+
150+
print("Mean Squared Error:", mse)
151+
print("R^2 Score:", r2)
152+
print("Selected Coefficients:", lars.coef_)
153+
154+
# Plot coefficient progression (coefficient paths)
155+
_, _, coefs = lars.path(X_train, y_train)
156+
157+
plt.figure(figsize=(10, 6))
158+
for coef_path in coefs.T:
159+
plt.plot(coef_path)
160+
plt.title("LARS Coefficient Paths")
161+
plt.xlabel("Step")
162+
plt.ylabel("Coefficient Value")
163+
plt.grid(True)
164+
plt.show()
165+
```

0 commit comments

Comments
 (0)