Module 4 of 26 · Scikit-Learn Machine Learning · Beginner

Linear Regression

Duration: 5 min

This module delves into Linear Regression, a fundamental machine learning algorithm used for predicting continuous outcomes. Understanding Linear Regression is crucial as it forms the basis for more complex models and provides insights into the relationship between variables.

Understanding Linear Regression

Linear Regression models the relationship between a dependent variable and one or more independent variables by fitting a linear equation to observed data. The goal is to find the line of best fit that minimizes the sum of squared residuals (differences between observed and predicted values).

import numpy as np
from sklearn.linear_model import LinearRegression

# Sample data
x = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 3, 2, 3, 5])

# Create and fit the model
model = LinearRegression()
model.fit(x, y)

# Make a prediction
x_new = np.array([[6]])
prediction = model.predict(x_new)

print(f'Prediction for x=6: {prediction[0]}')

Try it in Google Colab: Open in Colab

Prediction for x=6: 5.199999999999999

Evaluating Linear Regression Models

Evaluating the performance of a Linear Regression model is essential to ensure its effectiveness. Common metrics include the coefficient of determination (R-squared), Mean Squared Error (MSE), and Mean Absolute Error (MAE). These metrics help in understanding how well the model predicts the dependent variable.

from sklearn.metrics import mean_squared_error, r2_score

# True values
y_true = np.array([1, 3, 2, 3, 5])
# Predicted values from the model
y_pred = model.predict(x)

# Calculate metrics
mse = mean_squared_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)

print(f'Mean Squared Error: {mse}')
print(f'R-squared: {r2}')

💡 Tip: Always check the assumptions of Linear Regression, such as linearity, independence, homoscedasticity, and normality of residuals, to ensure the model's validity.

❓ What is the primary goal of Linear Regression?

❓ Which metric is used to evaluate how well the Linear Regression model predicts the dependent variable?

Key Concepts

Concept Description
Slope & Intercept Core principle in this module
Least Squares Core principle in this module
R² Score Core principle in this module
Residuals Core principle in this module

Check Your Understanding

❓ How does Linear handle edge cases?

❓ What is the computational complexity of Linear?

❓ Which hyperparameter is most critical for Linear?

← Previous Continue interactively → Next →

Related Courses