Last Updated: January 25, 2021
·
25
· kalinin84

Logistic regression

#include <stdio.h>
#include <math.h>

//
// Sigmoid
//
double sigmoid(double x)
{
    return 1 / (1 + exp(-x));
}

//
// Classifier
//
double predict(int size, double intercept, double coef[], double x[])
{
    double result = intercept;
    for (int i = 0; i < size; i++)
    {
        result += coef[i] * x[i];
    }
    return sigmoid(result);
}

//
// Example
//
int main() 
{
    double coef[6] = {0.014, 0.24, 0.19, 0.28, 0.0031, 0.00177};
    double vector[6] = {0.4, 0.2, 0.7, 0.1, 0.02, 0.009};

    printf("Result = %f\n", predict(6, 0.32, coef, vector));

    return 0;
}