1. Home
  2. Explore
  3. Computer Science
  4. Linear Regression Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Linear Regression Introduction

Speaker 1Speaker 2

Linear Regression Introduction

4:35
0:00-4:35
  1. Michael

    Welcome back! Today we're diving into our first learning algorithm: linear regression. It's one of the simplest but most widely used methods, and it sets the stage for much of what we'll see this quarter.

  2. Corey

    And we're going to start with a concrete example—predicting house prices from size. That's a supervised learning problem where the output is a continuous value, so it's regression, not classification.

  3. Michael

    Exactly. We have a dataset of house sizes in square feet and their prices. The learning algorithm takes this training set and outputs a hypothesis—a function that estimates price from size. For linear regression, that hypothesis is a straight line: h(x) = θ₀ + θ₁x.

  4. Corey

    But real problems often have more than one feature. For example, if we also know the number of bedrooms, our hypothesis becomes h(x) = θ₀ + θ₁x₁ + θ₂x₂. We can simplify notation by defining x₀ = 1, so h(x) = Σⱼ θⱼxⱼ.

  5. Michael

    Right. And the θ's are called parameters. The learning algorithm's job is to choose θ so that h(x) is close to the actual y for the training examples. To measure closeness, we define a cost function J(θ) = ½ Σᵢ (h(xⁱ) - yⁱ)². The ½ is just for mathematical convenience later.

  6. Corey

    So why squared error instead of absolute error? That's a great question. It turns out that squared error corresponds to assuming Gaussian noise, which we'll justify when we talk about generalized linear models.

  7. Michael

    Now, how do we minimize J? One algorithm is gradient descent. Imagine you're standing on a hill—the surface of J—and you want to go downhill as fast as possible. You look around, find the steepest direction, and take a step. That's exactly what gradient descent does: repeatedly update θⱼ ← θⱼ - α * ∂J/∂θⱼ, where α is the learning rate.

  8. Corey

    And that partial derivative, for the linear regression cost, works out to be ∂J/∂θⱼ = Σᵢ (h(xⁱ) - yⁱ) * xⱼⁱ. So each step uses all training examples—that's called batch gradient descent.

  9. Michael

    Batch gradient descent takes a step only after summing over the entire dataset. That's fine for small datasets, but if you have millions of examples, every step is painfully slow because you have to scan through all the data.

  10. Corey

    That's where stochastic gradient descent comes in. Instead of summing over all examples, you update the parameters using just one training example at a time: for i from 1 to m, update θⱼ ← θⱼ - α * (h(xⁱ) - yⁱ) * xⱼⁱ. This makes each step much faster, but the path is noisy—it never quite converges, but it gets close.

  11. Michael

    Right. And in practice, for large datasets, people often use stochastic gradient descent or mini-batch gradient descent (using a small batch of examples). They also sometimes decrease the learning rate over time to reduce oscillations.

  12. Corey

    But for linear regression, we actually have a closed-form solution that doesn't require iteration: the normal equations. By setting the derivative of J to zero, we can solve for θ directly.

  13. Michael

    To derive that, we define the design matrix X, where each row is a training example's features (including x₀=1), and let y be the vector of target values. Then the cost function can be written as J(θ) = ½ (Xθ - y)ᵀ(Xθ - y).

  14. Corey

    Taking the derivative and setting it to zero gives XᵀXθ = Xᵀy, which leads to θ = (XᵀX)⁻¹ Xᵀy. That's the normal equation. You can compute θ directly in one step using matrix operations.

  15. Michael

    But this only works for linear regression. And if XᵀX is non-invertible—usually because of redundant features—you can use the pseudo-inverse, but it's better to identify and remove those redundancies.

  16. Corey

    So to sum up: linear regression is a simple but powerful algorithm. We have two ways to fit it: gradient descent (iterative, scales well) and the normal equation (direct, but only for linear regression). The concepts of hypothesis, cost function, and parameters are the foundation for all the algorithms we'll see later.

  17. Michael

    Exactly. And those design choices—how to represent the hypothesis, how to measure error, and how to optimize—are the same questions we'll ask for every machine learning model. So mastering linear regression is a big step forward.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent