-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.php
More file actions
68 lines (53 loc) · 2.07 KB
/
train.php
File metadata and controls
68 lines (53 loc) · 2.07 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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\Classification\NaiveBayes;
use Phpml\Regression\LeastSquares;
use Phpml\ModelManager;
echo "🌱 Starting AgroSafeAI Training...\n";
$modelManager = new ModelManager();
// ==========================================
// 1. TRAIN DISEASE CLASSIFIER (Feature 1)
// ==========================================
echo "🔹 Training Disease Diagnosis Model... ";
// Load Data manually to handle text properly
$diseaseSamples = [];
$diseaseTargets = [];
if (($handle = fopen("data/diseases.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$diseaseSamples[] = [$data[0]]; // Symptom (wrapped in array)
$diseaseTargets[] = $data[1]; // Disease Name
}
fclose($handle);
}
$classifier = new NaiveBayes();
$classifier->train($diseaseSamples, $diseaseTargets);
// Save the trained model
$modelManager->saveToFile($classifier, 'models/disease_classifier.phpml');
echo "Done! ✅\n";
// ==========================================
// 2. TRAIN RESOURCE PREDICTOR (Feature 2)
// ==========================================
echo "🔹 Training Treatment Resource Models... ";
$resourceSamples = [];
$waterTargets = [];
$fungicideTargets = [];
if (($handle = fopen("data/treatments.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Input: [Farm Size, Severity]
$resourceSamples[] = [(int)$data[1], (int)$data[2]];
// Targets: Water and Fungicide
$waterTargets[] = (int)$data[3];
$fungicideTargets[] = (int)$data[4];
}
fclose($handle);
}
// Train Water Model
$waterModel = new LeastSquares();
$waterModel->train($resourceSamples, $waterTargets);
$modelManager->saveToFile($waterModel, 'models/water_predictor.phpml');
// Train Fungicide Model
$fungicideModel = new LeastSquares();
$fungicideModel->train($resourceSamples, $fungicideTargets);
$modelManager->saveToFile($fungicideModel, 'models/fungicide_predictor.phpml');
echo "Done! ✅\n";
echo "\n🎉 All models trained and saved to /models folder.\n";