Understanding Logistic Regression in C#

Logistic regression is a statistical method used for binary classification problems. Unlike linear regression which predicts continuous values, logistic regression predicts the probability that an instance belongs to a particular category. It is widely used in medical science to predict disease outcomes and in business to forecast customer behavior.

The key advantage of logistic regression is that it produces probabilities between 0 and 1, making it ideal for classification tasks. It also provides interpretable results through odds ratios and supports statistical hypothesis testing.

Mathematical Foundation

Logistic regression uses a linear model combined with the logistic function (sigmoid function) to map any real number to a value between 0 and 1. The linear model has the following form −

z = c?x? + c?x? + ... + c?x? + i

Where −

  • c is the coefficient vector

  • i is the intercept value

  • x is the observation vector

The logistic function transforms the linear output into a probability −

P(y=1|x) = 1 / (1 + e^(-z))

Implementing Logistic Regression in C#

Basic Logistic Regression Model

using System;

public class LogisticRegression {
    private double[] coefficients;
    private double intercept;
    
    public LogisticRegression(double[] coefficients, double intercept) {
        this.coefficients = coefficients;
        this.intercept = intercept;
    }
    
    private double Sigmoid(double z) {
        return 1.0 / (1.0 + Math.Exp(-z));
    }
    
    public double Predict(double[] features) {
        double z = intercept;
        for (int i = 0; i < features.Length; i++) {
            z += coefficients[i] * features[i];
        }
        return Sigmoid(z);
    }
    
    public int ClassifyBinary(double[] features) {
        return Predict(features) >= 0.5 ? 1 : 0;
    }
}

class Program {
    public static void Main() {
        // Example: Predicting customer purchase (coefficients from trained model)
        double[] coefficients = { 0.8, 1.2 }; // age, income coefficients
        double intercept = -2.5;
        
        LogisticRegression model = new LogisticRegression(coefficients, intercept);
        
        // Test with customer data: age=35, income=50000 (normalized)
        double[] customer1 = { 0.35, 0.50 };
        double probability = model.Predict(customer1);
        int classification = model.ClassifyBinary(customer1);
        
        Console.WriteLine($"Purchase probability: {probability:F3}");
        Console.WriteLine($"Will purchase: {(classification == 1 ? "Yes" : "No")}");
        
        // Test with another customer: age=25, income=30000 (normalized)
        double[] customer2 = { 0.25, 0.30 };
        probability = model.Predict(customer2);
        classification = model.ClassifyBinary(customer2);
        
        Console.WriteLine($"Purchase probability: {probability:F3}");
        Console.WriteLine($"Will purchase: {(classification == 1 ? "Yes" : "No")}");
    }
}

The output of the above code is −

Purchase probability: 0.269
Will purchase: No
Purchase probability: 0.119
Will purchase: No

Medical Diagnosis Example

Here's a practical example showing how logistic regression can be used for medical diagnosis prediction −

using System;

public class MedicalDiagnosis {
    private LogisticRegression model;
    
    public MedicalDiagnosis() {
        // Coefficients for: blood_pressure, cholesterol, age
        double[] coefficients = { 1.5, 1.8, 0.6 };
        double intercept = -3.2;
        model = new LogisticRegression(coefficients, intercept);
    }
    
    public void DiagnosePatient(double bloodPressure, double cholesterol, double age, string patientName) {
        // Normalize features (simplified normalization)
        double[] features = { 
            bloodPressure / 200.0,  // normalize BP
            cholesterol / 300.0,    // normalize cholesterol
            age / 100.0             // normalize age
        };
        
        double riskProbability = model.Predict(features);
        string riskLevel = GetRiskLevel(riskProbability);
        
        Console.WriteLine($"Patient: {patientName}");
        Console.WriteLine($"Heart Disease Risk: {riskProbability:F3} ({riskLevel})");
        Console.WriteLine("---");
    }
    
    private string GetRiskLevel(double probability) {
        if (probability >= 0.7) return "High Risk";
        if (probability >= 0.4) return "Medium Risk";
        return "Low Risk";
    }
}

class Program {
    public static void Main() {
        MedicalDiagnosis diagnosis = new MedicalDiagnosis();
        
        diagnosis.DiagnosePatient(140, 220, 55, "John Doe");
        diagnosis.DiagnosePatient(160, 280, 65, "Jane Smith");
        diagnosis.DiagnosePatient(120, 180, 35, "Bob Johnson");
    }
}

The output of the above code is −

Patient: John Doe
Heart Disease Risk: 0.689 (Medium Risk)
---
Patient: Jane Smith
Heart Disease Risk: 0.881 (High Risk)
---
Patient: Bob Johnson
Heart Disease Risk: 0.269 (Low Risk)
---

Key Advantages

  • Probabilistic output: Returns probabilities rather than just classifications

  • No assumptions about data distribution: Works with various data types

  • Interpretable coefficients: Each coefficient represents the change in log-odds

  • Computationally efficient: Fast training and prediction

Conclusion

Logistic regression in C# provides a powerful tool for binary classification problems, offering probabilistic predictions and interpretable results. It's particularly valuable in medical diagnosis, customer behavior prediction, and any scenario requiring probability-based decision making with clear statistical interpretation.

Updated on: 2026-03-17T07:04:35+05:30

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements