Execute & Evaluate Regression Model

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

Learn.

In this section we will learn how to run the linear regression model and calculate metrics

# Initialize and train the Linear Regression model
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)

 

# prediction on training and testing
y_pred_test = linear_model.predict(X_test)
y_pred_train = linear_model.predict(X_train)
 
# Calculate metrics
rmse_train = np.sqrt(mean_squared_error(y_train, y_pred_train))
r2_train = r2_score(y_train, y_pred_train)
mae_train = mean_absolute_error(y_train, y_pred_train)
rmse_test = np.sqrt(mean_squared_error(y_test, y_pred_test))
r2_test = r2_score(y_test, y_pred_test)
mae_test = mean_absolute_error(y_test, y_pred_test)
 

# Print RMSE scores for both training and testing
# datasets to evaluate model performance.
print(“Linear Regression Results:”)
print(“RMSE training:”, rmse_train)
print(“RMSE test:”, rmse_test)

 

# Print R-squared scores for both training and testing
# datasets to evaluate model performance.
print(“R2 score training:”, r2_train)
print(“R2 score test:”, r2_test)
 
# Print mae scores for both training and testing
# datasets to evaluate model performance.
print(“MAE training:”, mae_train)
print(“MAE test:”, mae_test)
 

Model Evaluator (e.g. RMSE, R2, MAE etc.)

  1. RMSE 1135 tells us that on an average our prediction will be off by 1135
  2. R2 score or coefficient of determination, it is a value ranging from 0-1, it tells you how much variance in the target variable is explained by the features. In our case we have r2 score of 0.91 which is extremely good.
  3. MAE tells us the deviation between the actual and predicted value, in our case we have a MAE of 737 which says that the deviation between actual and predicted value is 737
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