Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Fitting a Line with Gradient Descent | Mathematical Analysis
Mathematics for Data Science

bookChallenge: Fitting a Line with Gradient Descent

A student is exploring how to use gradient descent to fit a straight line to a small dataset. The dataset shows years of experience versus salary (in thousands), and the goal is to find the best-fitting line using an iterative update rule.

Your task is to adjust the slope (mm**) and intercept (bb) so that the line closely follows the data points.

The expression you are trying to minimize is:

1ni=1n(yi(mxi+b))2\frac{1}{n}\sum^n_{i=1}(y_i - (mx_i + b))^2

The gradient descent update rules for minimizing this function are:

mmαJmbbαJbm \larr m - \alpha \frac{\partial J}{\partial m} \\[6 pt] b \larr b - \alpha \frac{\partial J}{\partial b}

Where:

  • α\alpha is the learning rate (step size);
  • Jm\frac{\partial J}{\partial m} is the partial derivative of the loss function with respect to mm;
  • Jb\frac{\partial J}{\partial b} is the partial derivative of the loss function with respect to bb.

This loss measures how far off your predicted points are from the actual data. (P.S. Smaller values mean the line fits the data better.)

In order to find values mm and bb, use gradient descent.

Tehtävä

Swipe to start coding

  1. Complete the Python code below to implement the gradient descent steps.
  2. Fill in the missing expressions using basic Python operations.
  3. Track how the values of m and b change as the algorithm runs.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 11
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

Awesome!

Completion rate improved to 1.89

bookChallenge: Fitting a Line with Gradient Descent

Pyyhkäise näyttääksesi valikon

A student is exploring how to use gradient descent to fit a straight line to a small dataset. The dataset shows years of experience versus salary (in thousands), and the goal is to find the best-fitting line using an iterative update rule.

Your task is to adjust the slope (mm**) and intercept (bb) so that the line closely follows the data points.

The expression you are trying to minimize is:

1ni=1n(yi(mxi+b))2\frac{1}{n}\sum^n_{i=1}(y_i - (mx_i + b))^2

The gradient descent update rules for minimizing this function are:

mmαJmbbαJbm \larr m - \alpha \frac{\partial J}{\partial m} \\[6 pt] b \larr b - \alpha \frac{\partial J}{\partial b}

Where:

  • α\alpha is the learning rate (step size);
  • Jm\frac{\partial J}{\partial m} is the partial derivative of the loss function with respect to mm;
  • Jb\frac{\partial J}{\partial b} is the partial derivative of the loss function with respect to bb.

This loss measures how far off your predicted points are from the actual data. (P.S. Smaller values mean the line fits the data better.)

In order to find values mm and bb, use gradient descent.

Tehtävä

Swipe to start coding

  1. Complete the Python code below to implement the gradient descent steps.
  2. Fill in the missing expressions using basic Python operations.
  3. Track how the values of m and b change as the algorithm runs.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

close

Awesome!

Completion rate improved to 1.89
Osio 3. Luku 11
single

single

some-alt