Execute and Evaluate Regression Models

** Introduction to Machine Learning & AI with Python
Lesson Content
0% Complete

Learn.

In this section we will learn how to run the model and compare the evaluation (e.g. Linear, Lasso, Ridge, Elastic Net etc.)

# Initialize the regression models
# Linear Regression: No regularization applied.
lr = LinearRegression()

# Ridge Regression (L2 regularization): ‘alpha’ controls the strength of the L2 penalty.
# A larger alpha means stronger regularization, forcing coefficients closer to zero.
ridge = Ridge(alpha=0.1)

# Lasso Regression (L1 regularization): ‘alpha’ controls the strength of the L1 penalty.
# A larger alpha means stronger regularization, potentially driving some coefficients exactly to zero (feature selection).
lasso = Lasso(alpha=0.1)

# Elastic Net Regression (L1 + L2 regularization): ‘alpha’ is the total regularization strength.
# ‘l1_ratio’ (default 0.5) controls the mix between L1 and L2 penalties.
elastic_net = ElasticNet(alpha=0.1)

# Lists to store Root Mean Squared Error (RMSE) values for test sets and model names for easy comparison.
rmse = []
model_names = [“Linear Regression”, “Ridge”, “Lasso”, “Elastic Net”]
models = [lr, ridge, lasso, elastic_net]

# Loop through each defined model to train, predict, and evaluate its performance.
for model, name in zip(models, model_names):
  # Train the current model using the training data (features X_train, target y_train).
  model.fit(X_train, y_train)

  # Make predictions on the training set to check for potential overfitting.
  y_pred_train = model.predict(X_train)

  # Make predictions on the unseen test set to evaluate generalization performance.
  y_pred_test = model.predict(X_test)

  # Calculate the Root Mean Squared Error (RMSE) for the test set.
  # RMSE measures the average magnitude of the errors. Lower values indicate better fit.
  rmse_test = np.sqrt(mean_squared_error(y_test, y_pred_test))

  # Store the test RMSE for later visualization or comparison.
  rmse.append(rmse_test)

  # Print the name of the current model being evaluated.
  print(name)

  # Print the RMSE for the training set.
  print(“RMSE training”, np.sqrt(mean_squared_error(y_train, y_pred_train)))

  # Print the RMSE for the test set.
  print(“RMSE testing”, np.sqrt(mean_squared_error(y_test, y_pred_test)))
Jupyter Notebook
Loading Python...

Loading Python environment...

First load takes 15–30 seconds

Lesson Materials & Datasets

No materials available for this lesson yet.

Course Outline