C: linear regression with gradient descent

Linear regression fits the line y = w·x + b that minimizes the mean squared error between predictions and targets. Gradient descent starts from an initial guess and repeatedly moves each parameter a small step in the direction that lowers the error.

C
#include <stdio.h>

int main(void) {
  double x[] = {0, 1, 2, 3, 4};
  double y[] = {-2, 1, 4, 7, 10};
  int n = 5;
  double w = 0.0, b = 0.0, rate = 0.05;

  for (int epoch = 0; epoch < 5000; epoch++) {
    double gw = 0.0, gb = 0.0;
    for (int i = 0; i < n; i++) {
      double error = w * x[i] + b - y[i];
      gw += 2.0 * error * x[i] / n;
      gb += 2.0 * error / n;
    }
    w -= rate * gw;
    b -= rate * gb;
  }
  printf("w=%.2f b=%.2f\n", w, b);
  printf("prediction for x=10: %.2f\n", w * 10 + b);
  return 0;
}
Read the full lesson