Let’s see easy methods to implement Lasso Regression utilizing Scikit-Be taught step-by-step.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import Lasso
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, r2_score
For this instance, let’s use the Boston Housing dataset (if utilizing sklearn.datasets
, use one other dataset as boston
is deprecated).
from sklearn.datasets import fetch_california_housing
knowledge = fetch_california_housing()
X = knowledge.knowledge
y = knowledge.goal# Convert to DataFrame for higher visualization
df = pd.DataFrame(X, columns=knowledge.feature_names)
df["Target"] = yprint(df.head())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize options
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.remodel(X_test)
# Outline the Lasso mannequin with alpha (λ) = 0.1
lasso = Lasso(alpha=0.1)
lasso.match(X_train, y_train)
# Predict on take a look at knowledge
y_pred = lasso.predict(X_test)
# Calculate Imply Squared Error and R-squared
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)print("Imply Squared Error:", mse)
print("R-squared Rating:", r2)
# Extract and visualize coefficients
coefficients = pd.Sequence(lasso.coef_, index=knowledge.feature_names)
coefficients = coefficients[coefficients != 0] # Hold solely non-zero coefficients
# Plot characteristic significance
coefficients.sort_values().plot(form='barh', figsize=(8,6))
plt.title("Characteristic Significance (Lasso Regression)")
plt.present()