-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomprehensive_demo.php
More file actions
402 lines (335 loc) · 15.4 KB
/
Copy pathcomprehensive_demo.php
File metadata and controls
402 lines (335 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/**
* Comprehensive Machine Learning Library Demonstration
*
* This script demonstrates all concepts from the 12-article series:
* Articles 1-12: Complete implementation from introduction to practical examples
*
* Run this script to see the full ML pipeline in action!
*/
require_once __DIR__ . '/src/LinearAlgebra/Vector.php';
require_once __DIR__ . '/src/LinearAlgebra/Matrix.php';
require_once __DIR__ . '/src/Algorithms/Sorting.php';
require_once __DIR__ . '/src/Algorithms/Searching.php';
require_once __DIR__ . '/src/DataProcessing/DataCleaner.php';
require_once __DIR__ . '/src/NeuralNetwork/Perceptron.php';
require_once __DIR__ . '/src/NeuralNetwork/NeuralNetwork.php';
require_once __DIR__ . '/src/NeuralNetwork/LossFunctions.php';
require_once __DIR__ . '/src/Training/GradientDescent.php';
require_once __DIR__ . '/src/Training/HyperparameterTuner.php';
require_once __DIR__ . '/src/Evaluation/Metrics.php';
require_once __DIR__ . '/src/Evaluation/DataSplitter.php';
require_once __DIR__ . '/src/Examples/CompleteExample.php';
use MLPyHP\LinearAlgebra\Vector;
use MLPyHP\LinearAlgebra\Matrix;
use MLPyHP\Algorithms\Sorting;
use MLPyHP\Algorithms\Searching;
use MLPyHP\DataProcessing\DataCleaner;
use MLPyHP\NeuralNetwork\Perceptron;
use MLPyHP\NeuralNetwork\NeuralNetwork;
use MLPyHP\NeuralNetwork\LossFunctions;
use MLPyHP\Training\GradientDescent;
use MLPyHP\Training\HyperparameterTuner;
use MLPyHP\Evaluation\Metrics;
use MLPyHP\Evaluation\DataSplitter;
function printHeader($title, $article = null) {
echo "\n" . str_repeat("=", 70) . "\n";
if ($article) {
echo "ARTICLE {$article}: {$title}\n";
} else {
echo $title . "\n";
}
echo str_repeat("=", 70) . "\n";
}
function printSection($title) {
echo "\n" . str_repeat("-", 50) . "\n";
echo $title . "\n";
echo str_repeat("-", 50) . "\n";
}
// Start the comprehensive demonstration
printHeader("COMPLETE MACHINE LEARNING LIBRARY DEMONSTRATION");
echo "This demonstrates all concepts from the 12-article ML series\n";
echo "Each section shows working code that you can use in production!\n";
// ============================================================================
// ARTICLE 2: LINEAR ALGEBRA
// ============================================================================
printHeader("LINEAR ALGEBRA: THE MATHEMATICAL FOUNDATION", "2");
printSection("Vector Operations");
$userPrefs = new Vector([4, 2, 5, 1, 3]);
$movie1 = new Vector([3, 1, 4, 0, 2]);
$movie2 = new Vector([1, 4, 2, 3, 1]);
echo "User preferences: {$userPrefs}\n";
echo "Movie 1 features: {$movie1}\n";
echo "Movie 2 features: {$movie2}\n\n";
$similarity1 = $userPrefs->dotProduct($movie1);
$similarity2 = $userPrefs->dotProduct($movie2);
echo "Movie 1 similarity score: {$similarity1}\n";
echo "Movie 2 similarity score: {$similarity2}\n";
echo "Recommendation: " . ($similarity1 > $similarity2 ? "Movie 1" : "Movie 2") . "\n";
printSection("Matrix Operations");
$customerData = new Matrix([
[25, 50000, 2, 1], // Age, Income, Kids, Previous_purchases
[35, 75000, 1, 3],
[45, 100000, 3, 5],
[28, 60000, 0, 2]
]);
echo "Customer data matrix:\n{$customerData}\n";
$featureWeights = new Matrix([[0.1], [0.00001], [0.2], [0.3]]);
$customerScores = $customerData->multiply($featureWeights);
echo "Customer value scores:\n{$customerScores}\n";
// Demonstrate matrix operations
echo "Matrix transpose:\n{$customerData->transpose()}\n";
// ============================================================================
// ARTICLE 3: SORTING AND SEARCHING ALGORITHMS
// ============================================================================
printHeader("ALGORITHMS: SORTING AND SEARCHING", "3");
printSection("Sorting Algorithms Performance");
$data = [];
for ($i = 0; $i < 20; $i++) {
$data[] = mt_rand(1, 100);
}
echo "Random data: [" . implode(', ', $data) . "]\n\n";
$results = Sorting::compareAlgorithms($data);
foreach ($results as $algorithm => $result) {
echo "{$algorithm}: " . number_format($result['time'] * 1000, 3) . "ms\n";
}
printSection("Searching Performance");
$sortedData = Sorting::quickSort($data);
echo "Sorted data: [" . implode(', ', array_slice($sortedData, 0, 10)) . "...]\n\n";
$searchResults = Searching::compareSearchMethods($sortedData, 50);
foreach ($searchResults as $method => $result) {
echo "{$method}: Found at index {$result['index']} in " .
number_format($result['time'] * 1000000, 2) . " microseconds\n";
}
// ============================================================================
// ARTICLE 4: DATA PROCESSING
// ============================================================================
printHeader("DATA PROCESSING: CLEANING THE MESS", "4");
printSection("Data Cleaning Demonstration");
$messyData = [
[25, 50000, 85, 1],
[null, 75000, 92, 1],
[35, null, 78, 0],
[45, 100000, 95, 1],
[22, 30000, 45, 0], // Outlier score
[null, null, 88, 1],
[39, 85000, 91, 1]
];
echo "Original messy data:\n";
foreach ($messyData as $i => $row) {
echo "Row {$i}: [" . implode(', ', array_map(function($x) {
return $x === null ? 'NULL' : $x;
}, $row)) . "]\n";
}
$cleaner = new DataCleaner();
$cleanedData = $cleaner->handleMissingValues($messyData, 'mean');
echo "\nAfter cleaning:\n";
foreach ($cleanedData as $i => $row) {
echo "Row {$i}: [" . implode(', ', array_map(function($x) {
return number_format($x, 1);
}, $row)) . "]\n";
}
// Outlier detection
$scores = array_column($cleanedData, 2);
$outlierIndices = $cleaner->detectOutliers($scores, 2.0);
echo "\nOutlier analysis:\n";
$mean = array_sum($scores) / count($scores);
$variance = array_sum(array_map(function($x) use ($mean) {
return pow($x - $mean, 2);
}, $scores)) / count($scores);
$stdDev = sqrt($variance);
echo "Mean: " . number_format($mean, 2) . "\n";
echo "Std Dev: " . number_format($stdDev, 2) . "\n";
echo "Outliers found at indices: [" . implode(', ', $outlierIndices) . "]\n";
// ============================================================================
// ARTICLE 5: PERCEPTRONS
// ============================================================================
printHeader("PERCEPTRONS: THE BOUNCER AT THE CLUB", "5");
printSection("Learning Logic Gates");
echo "Training perceptron on AND gate:\n";
$andPerceptron = new Perceptron(2, 0.1, 'step');
$andData = Perceptron::createAndGateData();
$andHistory = $andPerceptron->train($andData, 20, false);
echo "Testing AND gate:\n";
foreach ($andData as $example) {
$inputs = $example[0];
$expected = $example[1];
$prediction = $andPerceptron->predict($inputs);
echo "[" . implode(', ', $inputs) . "] -> Expected: {$expected}, Got: {$prediction} " .
($prediction == $expected ? "✓" : "✗") . "\n";
}
echo "\nTraining perceptron on OR gate:\n";
$orPerceptron = new Perceptron(2, 0.1, 'step');
$orData = Perceptron::createOrGateData();
$orHistory = $orPerceptron->train($orData, 20, false);
echo "Testing OR gate:\n";
foreach ($orData as $example) {
$inputs = $example[0];
$expected = $example[1];
$prediction = $orPerceptron->predict($inputs);
echo "[" . implode(', ', $inputs) . "] -> Expected: {$expected}, Got: {$prediction} " .
($prediction == $expected ? "✓" : "✗") . "\n";
}
// ============================================================================
// ARTICLE 6: FORWARD PROPAGATION
// ============================================================================
printHeader("FORWARD PROPAGATION: THE ASSEMBLY LINE", "6");
printSection("Neural Network Architecture");
$network = new NeuralNetwork([2, 4, 3, 1], 'sigmoid');
$architecture = $network->getArchitecture();
echo "Network architecture: [" . implode(' -> ', $architecture['layers']) . "]\n";
echo "Total parameters: " . $architecture['total_parameters'] . "\n";
echo "Activation function: " . $architecture['activation_function'] . "\n\n";
printSection("Forward Propagation Demo");
$input = [0.7, 0.3];
echo "Processing input: [" . implode(', ', $input) . "]\n\n";
$output = $network->demonstrateAssemblyLine($input);
printSection("XOR Problem Solution");
NeuralNetwork::demonstrateXorSolution();
// ============================================================================
// ARTICLE 7: LOSS FUNCTIONS
// ============================================================================
printHeader("LOSS FUNCTIONS: THE TEACHER'S RED PEN", "7");
printSection("Loss Function Comparison");
LossFunctions::demonstrateLossBehavior();
printSection("Why Loss Function Choice Matters");
LossFunctions::demonstrateWhyChoiceMatters();
// ============================================================================
// ARTICLE 8: GRADIENT DESCENT
// ============================================================================
printHeader("GRADIENT DESCENT: HIKING DOWN THE MOUNTAIN", "8");
printSection("Training Data Generation");
$trainingData = [];
for ($i = 0; $i < 200; $i++) {
$x1 = (mt_rand() / mt_getrandmax()) * 2 - 1;
$x2 = (mt_rand() / mt_getrandmax()) * 2 - 1;
$target = $x1 * $x2 > 0 ? 1.0 : 0.0; // Simple quadrant-based classification
$trainingData[] = [[$x1, $x2], [$target]];
}
echo "Generated " . count($trainingData) . " training samples\n\n";
printSection("Gradient Descent Variants");
$network = new NeuralNetwork([2, 4, 1], 'sigmoid');
$optimizer = new GradientDescent(0.1, 'mini-batch', 0.0);
echo "Training with mini-batch gradient descent:\n";
$history = $optimizer->train($network, array_slice($trainingData, 0, 50), 30, 16, true);
// Test a few predictions
echo "\nTesting trained network:\n";
$testData = array_slice($trainingData, 50, 5);
foreach ($testData as $example) {
$prediction = $network->forwardPropagate($example[0]);
echo "Input: [" . implode(', ', array_map(function($x) { return number_format($x, 2); }, $example[0])) .
"] -> Prediction: " . number_format($prediction[0], 3) .
", Target: " . $example[1][0] . "\n";
}
// ============================================================================
// ARTICLE 9: EVALUATING PERFORMANCE
// ============================================================================
printHeader("EVALUATING PERFORMANCE: THE REPORT CARD", "9");
printSection("Data Splitting Demonstration");
$demoInputs = array_map(function($x) { return $x[0]; }, $trainingData);
$demoTargets = array_map(function($x) { return $x[1][0]; }, $trainingData);
$split = DataSplitter::trainTestSplit($demoInputs, $demoTargets, 0.3, true, 42);
echo "Original dataset: " . count($demoInputs) . " samples\n";
echo "Training set: " . count($split['train']['inputs']) . " samples\n";
echo "Test set: " . count($split['test']['inputs']) . " samples\n\n";
printSection("Model Evaluation");
// Generate predictions for evaluation
$predictions = [];
$targets = [];
foreach ($split['test']['inputs'] as $i => $input) {
$pred = $network->forwardPropagate($input);
$predictions[] = $pred[0];
$targets[] = $split['test']['targets'][$i];
}
$report = Metrics::evaluationReport($predictions, $targets);
Metrics::printReport($report);
printSection("Cross-Validation Demo");
$folds = DataSplitter::kFoldSplit($demoInputs, $demoTargets, 5, true, 42);
echo "Created " . count($folds) . " folds for cross-validation\n";
foreach ($folds as $fold) {
echo "Fold {$fold['fold_number']}: Train=" . count($fold['train']['inputs']) .
", Test=" . count($fold['test']['inputs']) . "\n";
}
// ============================================================================
// ARTICLE 10: HYPERPARAMETER TUNING
// ============================================================================
printHeader("HYPERPARAMETER TUNING: FINDING THE PERFECT SETTINGS", "10");
printSection("Search Space Definition");
$searchSpace = HyperparameterTuner::createDefaultSearchSpace('classification');
echo "Default search space for classification:\n";
foreach ($searchSpace as $param => $values) {
if (is_array($values) && isset($values[0])) {
echo "- {$param}: [" . implode(', ', array_slice($values, 0, 3)) .
(count($values) > 3 ? ", ..." : "") . "]\n";
}
}
printSection("Hyperparameter Tuning Demo");
$smallSearchSpace = [
'learning_rate' => [0.01, 0.1],
'batch_size' => [16, 32],
'epochs' => [10, 20],
'architecture' => [[2, 4, 1], [2, 8, 1]]
];
$tuner = new HyperparameterTuner($smallSearchSpace, 'f1_score', true);
$tuningResults = $tuner->randomSearch(
array_slice($trainingData, 0, 80),
array_slice($trainingData, 80, 20),
6,
true
);
echo "Best parameters found:\n";
foreach ($tuningResults['best_parameters'] as $param => $value) {
if (is_array($value)) {
echo "- {$param}: [" . implode(', ', $value) . "]\n";
} else {
echo "- {$param}: {$value}\n";
}
}
// ============================================================================
// ARTICLE 11: BACKPROPAGATION
// ============================================================================
printHeader("BACKPROPAGATION: LEARNING FROM MISTAKES", "11");
printSection("Backpropagation Demonstration");
$demoNetwork = new NeuralNetwork([2, 3, 1], 'sigmoid');
$sampleInput = [0.5, 0.8];
$sampleTarget = [1.0];
echo "Demonstrating backpropagation step by step:\n";
$backpropDemo = $demoNetwork->demonstrateBackpropagation($sampleInput, $sampleTarget);
echo "This shows how errors propagate backwards through the network\n";
echo "to update weights and improve future predictions.\n";
// ============================================================================
// ARTICLE 12: PRACTICAL EXAMPLES
// ============================================================================
printHeader("PRACTICAL EXAMPLES: PUTTING IT ALL TOGETHER", "12");
printSection("Complete ML Pipeline");
echo "Running the complete machine learning pipeline...\n";
echo "This demonstrates every concept from all 12 articles!\n\n";
$pipelineResults = CompleteExample::completeMachineLearningPipeline();
// ============================================================================
// FINAL SUMMARY
// ============================================================================
printHeader("DEMONSTRATION COMPLETE!");
echo "🎉 Congratulations! You've seen the complete ML library in action!\n\n";
echo "What you've learned:\n";
echo "✓ Article 1: Introduction to machine learning concepts\n";
echo "✓ Article 2: Linear algebra operations with vectors and matrices\n";
echo "✓ Article 3: Sorting and searching algorithms for efficient computation\n";
echo "✓ Article 4: Data preprocessing and cleaning techniques\n";
echo "✓ Article 5: Perceptron learning for basic classification\n";
echo "✓ Article 6: Forward propagation through neural networks\n";
echo "✓ Article 7: Loss functions for measuring prediction quality\n";
echo "✓ Article 8: Gradient descent for network training\n";
echo "✓ Article 9: Performance evaluation and data splitting\n";
echo "✓ Article 10: Hyperparameter tuning for optimal performance\n";
echo "✓ Article 11: Backpropagation for learning from mistakes\n";
echo "✓ Article 12: Complete practical examples and production workflows\n\n";
echo "Next steps:\n";
echo "• Experiment with different network architectures\n";
echo "• Try different datasets and problem types\n";
echo "• Implement additional optimization algorithms\n";
echo "• Add regularization techniques\n";
echo "• Build real-world applications\n\n";
echo "This library provides a solid foundation for understanding\n";
echo "and implementing machine learning algorithms from scratch!\n\n";
echo "Happy machine learning! 🤖\n";
echo str_repeat("=", 70) . "\n";