Java: linear regression with gradient descent
Linear regression fits y = w·x + b to data. The mean squared error measures how far the predictions are from the targets. Gradient descent reduces it by computing the gradient, the direction in which the error grows fastest, and moving the parameters a small step the other way.
Java
import java.util.Locale;
class Main {
public static void main(String[] args) {
double[] x = {1, 2, 3, 4};
double[] y = {3, 5, 7, 9};
double w = 0, b = 0, rate = 0.05;
for (int epoch = 0; epoch < 2000; epoch++) {
double gw = 0, gb = 0;
for (int i = 0; i < x.length; i++) {
double error = w * x[i] + b - y[i];
gw += 2 * error * x[i] / x.length;
gb += 2 * error / x.length;
}
w -= rate * gw;
b -= rate * gb;
}
System.out.printf(Locale.ROOT, "w=%.2f b=%.2f%n", w, b);
System.out.printf(Locale.ROOT, "prediction for x=5: %.2f%n", w * 5 + b);
}
}