[go: up one dir, main page]

0% found this document useful (0 votes)
40 views2 pages

Python Linear Regression Comparison

The document outlines a practical assignment to implement various types of linear regression using Python. It includes code for simple, multiple, ridge, lasso, and elastic net regression, along with a comparison of their performance based on Mean Squared Error (MSE) and R2 Score. A table is presented summarizing the evaluation metrics for each regression model.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Python Linear Regression Comparison

The document outlines a practical assignment to implement various types of linear regression using Python. It includes code for simple, multiple, ridge, lasso, and elastic net regression, along with a comparison of their performance based on Mean Squared Error (MSE) and R2 Score. A table is presented summarizing the evaluation metrics for each regression model.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

y4glso3yn

January 30, 2025

[2]: #Practical Assignment-2

#Q. IMPLEMENT ALL THE TYPES OF LINEAR REGRESSION USING PYTHON AND SHOW THE␣
↪COMPARISON IN THE FORM OF TABLE

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 LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error, r2_score

np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,␣
↪random_state=42)

lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
y_pred_lin = lin_reg.predict(X_test)
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
X_train_poly, X_test_poly, y_train, y_test = train_test_split(X_poly, y,␣
↪test_size=0.2, random_state=42)

multi_reg = LinearRegression()
multi_reg.fit(X_train_poly, y_train)
y_pred_multi = multi_reg.predict(X_test_poly)
ridge_reg = Ridge(alpha=1)
ridge_reg.fit(X_train_poly, y_train)
y_pred_ridge = ridge_reg.predict(X_test_poly)
lasso_reg = Lasso(alpha=0.1)
lasso_reg.fit(X_train_poly, y_train)
y_pred_lasso = lasso_reg.predict(X_test_poly)
elastic_net = ElasticNet(alpha=0.1, l1_ratio=0.5)
elastic_net.fit(X_train_poly, y_train)
y_pred_elastic = elastic_net.predict(X_test_poly)

1
def evaluate_model(y_true, y_pred, model_name):
return {
'Model': model_name,
'MSE': mean_squared_error(y_true, y_pred),
'R2 Score': r2_score(y_true, y_pred)
}
models_comparison = pd.DataFrame([
evaluate_model(y_test, y_pred_lin, 'Simple Linear Regression'),
evaluate_model(y_test, y_pred_multi, 'Multiple Linear Regression'),
evaluate_model(y_test, y_pred_ridge, 'Ridge Regression'),
evaluate_model(y_test, y_pred_lasso, 'Lasso Regression'),
evaluate_model(y_test, y_pred_elastic, 'ElasticNet Regression')
])
print(models_comparison)

Model MSE R2 Score


0 Simple Linear Regression 0.653700 0.807206
1 Multiple Linear Regression 0.635841 0.812473
2 Ridge Regression 0.639436 0.811413
3 Lasso Regression 0.689646 0.796604
4 ElasticNet Regression 0.660914 0.805078

You might also like