Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 23

    Introduction to Linear Regression

    Linear regression models a continuous target as a weighted combination of input features plus an intercept.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Linear regression models a continuous target as a weighted combination of input features plus an intercept. It is often the first supervised model teams try because it is fast, interpretable, and sets a baseline for harder algorithms.

    Understanding the topic

    When to use it Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.

    • When to use it — Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
    • Fit and predict — Call fit on training features and targets, then predict on new rows.
    TermDescription
    CoefficientWeight applied to each feature.
    InterceptBaseline prediction when features are zero.
    ResidualDifference between actual and predicted value.

    Step-by-step explanation

    1. When to use it — Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.
    2. Fit and predict — Call fit on training features and targets, then predict on new rows.

    Syntax reference

    Notation / API sketch:

    text
    y ≈ w1*x1 + w2*x2 + ... + b

    Informative example

    Python starter:

    python
    from sklearn.linear_model import LinearRegression
    import numpy as np
    X = np.array([[1], [2], [3], [4], [5]])
    y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
    model = LinearRegression().fit(X, y)
    print(round(model.predict([[6]])[0], 2))

    Output

    12.04

    Execution workflow

    1Introduction to Linear Regression — workflow
    1 / 2

    When to use it

    Choose linear regression when the relationship looks roughly linear and you need coefficients you can explain to stakeholders.

    Worked examples

    Fit and predict

    Call fit on training features and targets, then predict on new rows.

    python
    pred = model.predict(X_test)
    print(pred[:3])

    Output

    [...]

    Best practices

    • Hold out a test set before hyperparameter tuning.
    • Scale numeric columns for distance-based models.
    • Track multiple metrics — not accuracy alone on skewed labels.

    Common mistakes

    • Leaking test statistics into preprocessing fit on full data.
    • Training on the same rows you report as test performance.
    • Chasing complex models before a simple baseline.

    Hands-on exercise

    Practice:

    • Plot residuals vs predictions
    • Compare R² against a dummy mean predictor

    Summary

    Introduction to Linear Regression — Weighted sum of features for continuous targets.

    Ready to mark this lesson complete?Track your journey across the entire course.