C#: logistic regression for classification
Logistic regression is a classifier. It computes a weighted sum z = w·x + b, passes it through the sigmoid 1 / (1 + e^−z) to get a probability between 0 and 1, and predicts class 1 when the probability is at least 0.5. Unlike a squared-error line fit, it is trained to maximize the likelihood of the labels.
C#
using System;
using System.Globalization;
class Program {
static double Sigmoid(double z) {
return 1.0 / (1.0 + Math.Exp(-z));
}
static string F(double v) {
return v.ToString("F2", CultureInfo.InvariantCulture);
}
static void Main() {
double[] hours = { 1, 2, 3, 4, 5, 6 };
int[] passed = { 0, 0, 1, 0, 1, 1 };
double w = 0, b = 0, rate = 0.1;
for (int epoch = 0; epoch < 5000; epoch++) {
double gw = 0, gb = 0;
for (int i = 0; i < hours.Length; i++) {
double error = Sigmoid(w * hours[i] + b) - passed[i];
gw += error * hours[i] / hours.Length;
gb += error / hours.Length;
}
w -= rate * gw;
b -= rate * gb;
}
Console.WriteLine("w=" + F(w) + " b=" + F(b));
Console.WriteLine("P(pass | 1h) = " + F(Sigmoid(w * 1 + b)));
Console.WriteLine("P(pass | 6h) = " + F(Sigmoid(w * 6 + b)));
int correct = 0;
for (int i = 0; i < hours.Length; i++) {
int predicted = Sigmoid(w * hours[i] + b) >= 0.5 ? 1 : 0;
if (predicted == passed[i]) correct++;
}
Console.WriteLine("accuracy: " + correct + "/" + hours.Length);
}
}