Location>code7788 >text

python ridge trace plotting function

Popularity:385 ℃/2024-11-17 11:33:05
# Mapping the Ridge Trail def Ridge_Plot(x_train, y_train): import numpy as np import as plt from sklearn.linear_model import Ridge # Ensure that y_train is a one-dimensional array y_train = (y_train) # Automatically convert (n, 1) to (n,) # Define the range of regularization parameters alphas = (-4, 4, 100) # Storage factor coefs = [] # Traverse each regularization parameter to train the ridge regression model for alpha in alphas: ridge = Ridge(alpha=alpha) (x_train, y_train) (ridge.coef_) # Converting coefficients to arrays coefs = (coefs) # Mapping the Ridge Trail (figsize=(10, 6)) for i in range(x_train.shape[1]): (alphas, coefs[:, i], label=f'Feature {i + 1}') ('log') ('Regularization Parameter (alpha)') ('Coefficients') ('Ridge Trace Plot') (0, color='black', linestyle='--', linewidth=0.7) (loc='upper right', bbox_to_anchor=(1.2, 1), ncol=1) plt.tight_layout() () Ridge_Plot(x_train=X_train,y_train=y_train)