|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/../vendor/autoload.php'; |
| 4 | + |
| 5 | +use HiFolks\Statistics\NormalDist; |
| 6 | + |
| 7 | +/** |
| 8 | + * Recipe: Naive Bayesian Classifier |
| 9 | + * |
| 10 | + * Adapted from the Python statistics module "Examples and Recipes": |
| 11 | + * https://docs.python.org/3/library/statistics.html#examples-and-recipes |
| 12 | + * |
| 13 | + * A simple Naive Bayes classifier using NormalDist. |
| 14 | + * Given training data for height, weight, and foot size of males |
| 15 | + * and females, classify a new person based on their measurements. |
| 16 | + */ |
| 17 | + |
| 18 | +echo "=== Naive Bayesian Classifier ===" . PHP_EOL . PHP_EOL; |
| 19 | + |
| 20 | +// --- Training data --- |
| 21 | +// Fit normal distributions to each feature for each class |
| 22 | + |
| 23 | +echo "--- Training Phase ---" . PHP_EOL; |
| 24 | + |
| 25 | +$heightMale = NormalDist::fromSamples([6, 5.92, 5.58, 5.92]); |
| 26 | +$heightFemale = NormalDist::fromSamples([5, 5.5, 5.42, 5.75]); |
| 27 | + |
| 28 | +$weightMale = NormalDist::fromSamples([180, 190, 170, 165]); |
| 29 | +$weightFemale = NormalDist::fromSamples([100, 150, 130, 150]); |
| 30 | + |
| 31 | +$footSizeMale = NormalDist::fromSamples([12, 11, 12, 10]); |
| 32 | +$footSizeFemale = NormalDist::fromSamples([6, 8, 7, 9]); |
| 33 | + |
| 34 | +echo "Height (male): mu=" . $heightMale->getMeanRounded(2) |
| 35 | + . ", sigma=" . $heightMale->getSigmaRounded(2) . PHP_EOL; |
| 36 | +echo "Height (female): mu=" . $heightFemale->getMeanRounded(2) |
| 37 | + . ", sigma=" . $heightFemale->getSigmaRounded(2) . PHP_EOL; |
| 38 | +echo "Weight (male): mu=" . $weightMale->getMeanRounded(2) |
| 39 | + . ", sigma=" . $weightMale->getSigmaRounded(2) . PHP_EOL; |
| 40 | +echo "Weight (female): mu=" . $weightFemale->getMeanRounded(2) |
| 41 | + . ", sigma=" . $weightFemale->getSigmaRounded(2) . PHP_EOL; |
| 42 | +echo "Foot size (male): mu=" . $footSizeMale->getMeanRounded(2) |
| 43 | + . ", sigma=" . $footSizeMale->getSigmaRounded(2) . PHP_EOL; |
| 44 | +echo "Foot size (female): mu=" . $footSizeFemale->getMeanRounded(2) |
| 45 | + . ", sigma=" . $footSizeFemale->getSigmaRounded(2) . PHP_EOL; |
| 46 | + |
| 47 | +// --- Classification --- |
| 48 | +echo PHP_EOL . "--- Classification Phase ---" . PHP_EOL . PHP_EOL; |
| 49 | + |
| 50 | +// Person to classify |
| 51 | +$ht = 6.0; // height in feet |
| 52 | +$wt = 130; // weight in pounds |
| 53 | +$fs = 8; // foot size |
| 54 | + |
| 55 | +echo "New person: height=" . $ht . "ft, weight=" . $wt |
| 56 | + . "lbs, foot size=" . $fs . PHP_EOL . PHP_EOL; |
| 57 | + |
| 58 | +// Equal prior probabilities |
| 59 | +$priorMale = 0.5; |
| 60 | +$priorFemale = 0.5; |
| 61 | + |
| 62 | +// Posterior ∝ prior × P(height|class) × P(weight|class) × P(foot_size|class) |
| 63 | +// Naive Bayes assumes features are conditionally independent. |
| 64 | +$posteriorMale = $priorMale |
| 65 | + * $heightMale->pdf($ht) |
| 66 | + * $weightMale->pdf($wt) |
| 67 | + * $footSizeMale->pdf($fs); |
| 68 | + |
| 69 | +$posteriorFemale = $priorFemale |
| 70 | + * $heightFemale->pdf($ht) |
| 71 | + * $weightFemale->pdf($wt) |
| 72 | + * $footSizeFemale->pdf($fs); |
| 73 | + |
| 74 | +echo "Posterior (male): " . sprintf("%.4e", $posteriorMale) . PHP_EOL; |
| 75 | +echo "Posterior (female): " . sprintf("%.4e", $posteriorFemale) . PHP_EOL; |
| 76 | +echo PHP_EOL; |
| 77 | + |
| 78 | +$classification = $posteriorMale > $posteriorFemale ? 'male' : 'female'; |
| 79 | +echo "Classification: " . $classification . PHP_EOL; |
| 80 | + |
| 81 | +// Show confidence as normalized probability |
| 82 | +$total = $posteriorMale + $posteriorFemale; |
| 83 | +$confidenceMale = $posteriorMale / $total; |
| 84 | +$confidenceFemale = $posteriorFemale / $total; |
| 85 | +echo "Confidence (male): " . round($confidenceMale * 100, 1) . "%" . PHP_EOL; |
| 86 | +echo "Confidence (female): " . round($confidenceFemale * 100, 1) . "%" . PHP_EOL; |
| 87 | + |
| 88 | +// --- Classify a second person --- |
| 89 | +echo PHP_EOL . "--- Classify another person ---" . PHP_EOL . PHP_EOL; |
| 90 | + |
| 91 | +$ht2 = 5.5; |
| 92 | +$wt2 = 175; |
| 93 | +$fs2 = 11; |
| 94 | + |
| 95 | +echo "New person: height=" . $ht2 . "ft, weight=" . $wt2 |
| 96 | + . "lbs, foot size=" . $fs2 . PHP_EOL . PHP_EOL; |
| 97 | + |
| 98 | +$posteriorMale2 = $priorMale |
| 99 | + * $heightMale->pdf($ht2) |
| 100 | + * $weightMale->pdf($wt2) |
| 101 | + * $footSizeMale->pdf($fs2); |
| 102 | + |
| 103 | +$posteriorFemale2 = $priorFemale |
| 104 | + * $heightFemale->pdf($ht2) |
| 105 | + * $weightFemale->pdf($wt2) |
| 106 | + * $footSizeFemale->pdf($fs2); |
| 107 | + |
| 108 | +$classification2 = $posteriorMale2 > $posteriorFemale2 ? 'male' : 'female'; |
| 109 | +$total2 = $posteriorMale2 + $posteriorFemale2; |
| 110 | + |
| 111 | +echo "Posterior (male): " . sprintf("%.4e", $posteriorMale2) . PHP_EOL; |
| 112 | +echo "Posterior (female): " . sprintf("%.4e", $posteriorFemale2) . PHP_EOL; |
| 113 | +echo "Classification: " . $classification2 . PHP_EOL; |
| 114 | +echo "Confidence: " . round(max($posteriorMale2, $posteriorFemale2) / $total2 * 100, 1) |
| 115 | + . "%" . PHP_EOL; |
0 commit comments