Logistic regression
Case 1. Logistic regression for customer churn
Implementation in pure PHP
Customer churn is one of the most typical binary classification tasks. A user either stays in the product or leaves. What we really care about is not just the final "yes or no", but the probability of churn: how high the risk is and whether we should react. This is exactly where logistic regression fits especially well. We will build a simple model in pure PHP that estimates the churn probability from user behavior and see how this probability is formed from features.
Example of use
<?php
include __DIR__ . '/code.php';
// Describe a new user with the same feature structure as rows in $X.
// For example: three behavioral / profile metrics.
$newUser = [5.0, 7.0, 120.0];
// Compute the churn probability for this user:
// 1) linear score z = w · x + b
// 2) pass through sigmoid to get a probability in (0, 1).
$probability = sigmoid(dot($weights, $newUser) + $bias);
// Print the final probability (rounded) to the console / page.
echo 'Churn probability: ' . round($probability, 3) . PHP_EOL;
Result:
Memory: 0.005 Mb
Time running: 0.002 sec.
Churn probability: 0.994