Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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.

Aufgabe

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.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 11
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

Awesome!

Completion rate improved to 1.89

bookChallenge: Fitting a Line with Gradient Descent

Swipe um das Menü anzuzeigen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 1.89
Abschnitt 3. Kapitel 11
single

single

some-alt