-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn-training-testing.php
52 lines (39 loc) · 1.35 KB
/
knn-training-testing.php
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
<?php
declare(strict_types=1);
namespace IFVABOTKNN;
require './vendor/autoload.php';
// ini_set('memory_limit', '1024M'); // or you could use 1G
ini_set('memory_limit', '-1');
use Rubix\ML\ModelManager;
use Rubix\ML\Classifiers\KNearestNeighbors;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Persisters\Filesystem;
include './fungsi.php';
$connect = connectDB('ifva');
$data_latih = file_get_contents("json/datalatih.json");
$json_latih = json_decode($data_latih, TRUE);
$labellatih = $json_latih['label'];
$tfidflatih = $json_latih['tfidf'];
//TABLE TF UJI
$data_uji = file_get_contents("json/datauji.json");
$json_uji = json_decode($data_uji, TRUE);
foreach ($json_uji as $key => $value) {
$labeluji[$key] = $value['label'];
$tfuji[$key] = $value['tf'];
}
$datalatih_tfidf = new Labeled($tfidflatih, $labellatih);
$datauji_tf = new Labeled($tfuji, $labeluji);
//TRAINNIG
print_r("<hr><h1>TRAINING KNN K-5</h1><br>");
$classifier = new KNearestNeighbors(5);
$classifier->train($datalatih_tfidf); // Trainning Hasil TFIDF
//TESTING
print_r("<hr><h1>TESTING KNN K-5</h1><br>");
$predictions = $classifier->predict($datauji_tf);
$results = $report->generate($predictions, $labeluji);
echo $results;
//SIMPAN MODEL
//menyimpan model agar tidak dilatih ulang setiap saat.
$modelManager = new ModelManager();
$modelManager->saveToFile($classifier, __DIR__.'/knn.model');
?>