0% found this document useful (0 votes)
41 views3 pages

Ridge Regression

Uploaded by

22eg105n47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Ridge Regression

Uploaded by

22eg105n47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

RIDGE REGRESSION

# Import necessary libraries


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.datasets import fetch_california_housing

# Load the California Housing Dataset


california = fetch_california_housing()
X = california.data # Features
y = california.target # Target (Median house value)

# Convert to DataFrame for better readability (optional)


df = pd.DataFrame(X, columns=california.feature_names)
df['Target'] = y
print("Sample Data:")
print(df.head())

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Initialize Ridge Regression model


ridge_reg = Ridge(alpha=1.0) # Regularization strength

# Train the model


ridge_reg.fit(X_train, y_train)

# Predict on the test set


y_pred = ridge_reg.predict(X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

# Display performance metrics


print(f"Ridge Regression - MSE: {mse:.2f}, R-squared: {r2:.2f}")

# Visualize actual vs predicted values


plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, color='blue', alpha=0.6)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()],
color='red', linestyle='--')
plt.title("Ridge Regression: Actual vs Predicted (California Housing
Dataset)")
plt.xlabel("Actual Median House Value")
plt.ylabel("Predicted Median House Value")
plt.show()

*********************LASSO REGRESSION**********************************

# Import necessary libraries


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso # Change from Ridge to Lasso
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.datasets import fetch_california_housing

# Load the California Housing Dataset


california = fetch_california_housing()
X = california.data # Features
y = california.target # Target (Median house value)

# Convert to DataFrame for better readability (optional)


df = pd.DataFrame(X, columns=california.feature_names)
df['Target'] = y
print("Sample Data:")
print(df.head())

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Initialize Lasso Regression model (Change here from Ridge to Lasso)


lasso_reg = Lasso(alpha=1.0) # Regularization strength, you can tune
alpha

# Train the model


lasso_reg.fit(X_train, y_train)

# Predict on the test set


y_pred = lasso_reg.predict(X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

# Display performance metrics


print(f"Lasso Regression - MSE: {mse:.2f}, R-squared: {r2:.2f}")

# Visualize actual vs predicted values


plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, color='blue', alpha=0.6)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()],
color='red', linestyle='--')
plt.title("Lasso Regression: Actual vs Predicted (California Housing
Dataset)")
plt.xlabel("Actual Median House Value")
plt.ylabel("Predicted Median House Value")
plt.show()

You might also like