C++: linear regression with gradient descent

Linear regression finds the line that best fits the data by minimizing the mean squared error. Gradient descent starts from an initial guess and repeatedly nudges each parameter in the direction that reduces the error. The size of each nudge is the learning rate times the gradient.

C++
#include <iomanip>
#include <iostream>
#include <vector>

int main() {
  std::vector<double> x = {0, 1, 2, 3, 4};
  std::vector<double> y = {-2, 1, 4, 7, 10};
  double w = 0.0, b = 0.0, rate = 0.05;
  double n = static_cast<double>(x.size());

  for (int epoch = 0; epoch < 5000; ++epoch) {
    double gw = 0.0, gb = 0.0;
    for (std::size_t i = 0; i < x.size(); ++i) {
      double error = w * x[i] + b - y[i];
      gw += 2 * error * x[i] / n;
      gb += 2 * error / n;
    }
    w -= rate * gw;
    b -= rate * gb;
  }
  std::cout << std::fixed << std::setprecision(2);
  std::cout << "w=" << w << " b=" << b << "\n";
  std::cout << "prediction for x=10: " << w * 10 + b << "\n";
}
Read the full lesson