-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing-design-knn-04fold.php
273 lines (254 loc) · 10.7 KB
/
testing-design-knn-04fold.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
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
<?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\GridSearch;
use Rubix\ML\ModelManager;
use Rubix\ML\Backends\Tasks\TrainAndValidate;
use Rubix\ML\Classifiers\KNearestNeighbors;
use Rubix\ML\Kernels\Distance\Manhattan;
use Rubix\ML\Classifiers\SVC;
use Rubix\ML\CrossValidation\KFold;
use Rubix\ML\CrossValidation\Metrics\Accuracy;
use Rubix\ML\CrossValidation\Metrics\FBeta;
use Rubix\ML\CrossValidation\Reports\MulticlassBreakdown;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Persisters\Filesystem;
use Rubix\ML\Kernels\SVM\Linear;
use Rubix\ML\Other\Helpers\Stats;
use Rubix\ML\Tfidf;
use svmmodel;
use svm;
include './fungsi.php';
include './fungsi_svd.php';
$connect = connectDB('ifva');
//AMBIL DATA SET
$i=0;
$data_samples=mysqli_query($connect, "SELECT * FROM `dataset`");
$samples= array();
while ($d=mysqli_fetch_array($data_samples)) {
$s['pertanyaan'] = $d['pertanyaan'];
$s['hasil_praproses'] = $d['hasil_praproses'];
$s['jawaban'] = $d['jawaban'];
$samples[$i] = $s;
$lab[$i] = $d['label'];
$i++;
}
$kfold=4;
$dataset = new Labeled($samples, $lab);
// $folds = $dataset->randomize()->stratifiedFold($kfold);
$folds = $dataset->stratifiedFold($kfold);
// print_r("<pre>");
// print_r($folds);
// mysqli_query($connect, "TRUNCATE TABLE kfold_datauji_svm");
for ($i = 0; $i < $kfold; $i++) {
$data[$i] = $folds[$i]->getSamples();
$label[$i] = $folds[$i]->getTargets();
foreach ($data[$i] as $key => $value) {
$data[$i][$key][3] = $label[$i][$key];
$pertanyaan[$i][$key] = $value[0];
$hasil_praproses[$i][$key] = $value[1];
$jawaban[$i][$key] = $value[2];
}
$x=0;
// foreach ($data[$i] as $row) {
// mysqli_query($connect, "INSERT INTO kfold_datauji_svm ( id, kfold, i, pertanyaan, hasil_praproses, jawaban, label) VALUES ('', $i, '$x', '$row[0]','$row[1]','$row[2]','$row[3]')");
// $x++;
// }
}
for ($i = 0; $i < $kfold; $i++) {
$coba[$i][$i] = $i;
$pertanyaandatalatih[$i] = array_diff_key($pertanyaan, $coba[$i]);
$hasil_praprosesdatalatih[$i] = array_diff_key($hasil_praproses, $coba[$i]);
$jawabandatalatih[$i] = array_diff_key($jawaban, $coba[$i]);
$labeldatalatih[$i] = array_diff_key($label, $coba[$i]);
}
for ($i = 0; $i < $kfold; $i++) {
$n=0;
foreach ($pertanyaandatalatih[$i] as $key => $value) {
foreach ($value as $kunci => $val) {
$pertanyaandatalatihbaru[$i][$n] = $pertanyaandatalatih[$i][$key][$kunci];
$kalimatstemm[$i][$n] = $hasil_praprosesdatalatih[$i][$key][$kunci];
$labeldatalatihbaru[$i][$n] = $labeldatalatih[$i][$key][$kunci];
$jawabandatalatihbaru[$i][$n] = $jawabandatalatih[$i][$key][$kunci];
$kalimatarraystemm[$i][$n] = explode(" ", $kalimatstemm[$i][$n]);
$n++;
}
}
$m=0;
foreach($kalimatarraystemm[$i] as $key => $value ){
foreach ($value as $kunci => $val){
$termstemm[$i][$m] = $val;
$m++;
}
}
// print_r("</pre><hr><pre><h1>TFIDF DATALATIH MASING-MASING KFOLD KE- ".$i."</h1><br>");
$con=new Tfidf();
$con->proses($kalimatstemm[$i],$termstemm[$i]);
$datalatih_table1[$i]=$con->table1;
foreach ($datalatih_table1[$i] as $key => $value) {
$datalatih_term[$i][$key] = array('term'=> $value['term']);
}
// TF DATA LATIH
foreach ($kalimatarraystemm[$i] as $key => $value) {
$tf_latih[$i][$key] = query($kalimatarraystemm[$i][$key], $datalatih_term[$i]);
}
//TF-IDF NORMALISASI L2
$datalatih_table2[$i]=normalisasi($con->table2);
$datalatih[$i] = new Labeled($datalatih_table2[$i], $labeldatalatihbaru[$i]);
$array_latih[$i] = array('tf'=> $tf_latih[$i],'tfidf' => $datalatih_table2[$i], 'label' => $labeldatalatihbaru[$i]);
//TF DATA UJI
$x=0;
foreach ($hasil_praproses[$i] as $key => $value) {
$kalimatujiarraystemm[$i][$x] = explode(" ", $value);
$tf_uji[$i][$x] = query($kalimatujiarraystemm[$i][$x], $datalatih_term[$i]);
$array_uji[$i][$x] = array('tf' => $tf_uji[$i][$x] , 'label' => $label[$i][$x]);
$x++;
}
$datauji[$i] = new Labeled($tf_uji[$i], $label[$i]);
}
// print_r("<pre>TF LATIH");
// print_r($array_latih);
// print_r("<pre>");
// print_r($datalatih_table1);
// print_r("<pre><h3>DATA LATIH</h3><br>");
// print_r($pertanyaandatalatihbaru);
// print_r("<pre><h3>DATA UJI<br></h3>");
// print_r($array_uji);
// print_r("<br>");
// print_r($label);
// encode array to json array latih
$json_datalatih = json_encode($array_latih);
//write json to file
if (file_put_contents("json/datalatih-knn-4-fold.json", $json_datalatih))
echo "<br>JSON DATA LATIH file created successfully...<br>";
else
echo "Oops! Error creating json file...";
// // encode array to json array uji
$json_datauji = json_encode($array_uji);
//write json to file
if (file_put_contents("json/datauji-knn-4-fold.json", $json_datauji))
echo "<br>JSON DATA UJI file created successfully...<br>";
else
echo "Oops! Error creating json file...";
foreach ($pertanyaandatalatihbaru as $key => $value) {
$i=0;
foreach ($value as $kunci => $val) {
$gabung_datalatih[$key][$i]['pertanyaan'] = $val;
$gabung_datalatih[$key][$i]['hasil_praproses'] = $kalimatstemm[$key][$i];
$gabung_datalatih[$key][$i]['jawaban'] = $jawabandatalatihbaru[$key][$i];
$gabung_datalatih[$key][$i]['label'] = $labeldatalatihbaru[$key][$i];
$i++;
}
}
// mysqli_query($connect, "TRUNCATE TABLE kfold_datalatih_svm");
// for ($i = 0; $i < $kfold; $i++) {
// $x=0;
// foreach ($gabung_datalatih[$i] as $row) {
// mysqli_query($connect, "INSERT INTO kfold_datalatih_svm ( id, kfold, i, pertanyaan, hasil_praproses, jawaban, label) VALUES ('', $i, '$x', '$row[pertanyaan]','$row[hasil_praproses]','$row[jawaban]','$row[label]')");
// $x++;
// }
// }
//==============================================================================================================================================//
$report = new MulticlassBreakdown();
print_r('<pre><h3>PENGUJIAN K-FOLD KNearestNeighbors K = 5</h3>');
$estimator = new KNearestNeighbors(5);
for ($i = 0; $i < $kfold; $i++) {
$estimator->train($datalatih[$i]);
$predictions[$i] = $estimator->predict($datauji[$i]);
$results[$i] = $report->generate($predictions[$i], $label[$i]);
$akurasi[$i] = $results[$i]['overall']['accuracy'];
$fmeasure[$i] = $results[$i]['overall']['f1_score'];
$presisi[$i] = $results[$i]['overall']['precision'];
$recall[$i] = $results[$i]['overall']['recall'];
print_r("<br>AKURASI KE- " . $i . " = " . $results[$i]['overall']['accuracy']);
print_r("<br>F-MEASURE KE- " . $i . " = " . $results[$i]['overall']['f1_score']);
print_r("<br>PRECISION KE- " . $i . " = " . $results[$i]['overall']['precision']);
print_r("<br>RECALL KE- " . $i . " = " . $results[$i]['overall']['recall']);
print_r("<br>");
}
print_r("<br>RATA-RATA");
print_r("<br>AKURASI = " . Stats::mean($akurasi));
print_r("<br>F-MEASURE = " . Stats::mean($fmeasure));
print_r("<br>PRESISI = " . Stats::mean($presisi));
print_r("<br>RECALL = " . Stats::mean($recall));
print_r("<br><br>");
print_r($results);
//==============================================================================================================================================//
// foreach ($predictions as $key => $value) {
// foreach($value as $kunci => $val){
// $data_kandidat[$key][$kunci] = mysqli_query($connect, "SELECT * FROM `kfold_datalatih` WHERE label = '$val' ");
// while ($d=mysqli_fetch_array($data_kandidat[$key][$kunci])) {
// $ans[$key][$kunci][$d['i']] = $d['jawaban'];
// $ask[$key][$kunci][$d['i']] = $d['pertanyaan'];
// foreach ($ask[$key][$kunci] as $i => $val) {
// $tfidf_kandidat[$key][$i] = array($datalatih_table2[$key][$i]);
// $nilaicossim[$key][$kunci][$i] = cosinesimilarity(array($tf_uji[$key][$kunci]), matrixTranspose($tfidf_kandidat[$key][$i]));
// $k[$key][$kunci][$i] = $nilaicossim[$key][$kunci][$i][0][0];
// arsort($k[$key][$kunci], SORT_NUMERIC);
// }
// }
// $x=0;
// foreach ($k[$key][$kunci] as $y => $value) {
// $kandidat[$key][$kunci][$x]['nilaicossim'] = $value;
// $kandidat[$key][$kunci][$x]['pertanyaan'] = $ask[$key][$kunci][$y];
// $kandidat[$key][$kunci][$x]['jawaban'] = $ans[$key][$kunci][$y];
// $x++;
// }
// }
// }
// // print_r($predictions);
// // print_r($ans);
// foreach ($kandidat as $key => $value) {
// foreach ($value as $k => $val) {
// $hasilakhir[$key][$k] = $val[0]['jawaban'];
// }
// }
// // print_r($hasilakhir);
// for ($i = 0; $i < $kfold; $i++) {
// $resultsretrieval[$i] = $report->generate($hasilakhir[$i], $jawaban[$i]);
// $akurasiretrieval[$i] = $resultsretrieval[$i]['overall']['accuracy'];
// $fmeasureretrieval[$i] = $resultsretrieval[$i]['overall']['f1_score'];
// $presisiretrieval[$i] = $resultsretrieval[$i]['overall']['precision'];
// $recallretrieval[$i] = $resultsretrieval[$i]['overall']['recall'];
// print_r("<br>AKURASI KE- " . $i . " = " . $resultsretrieval[$i]['overall']['accuracy']);
// print_r("<br>F-MEASURE KE- " . $i . " = " . $resultsretrieval[$i]['overall']['f1_score']);
// print_r("<br>PRECISION KE- " . $i . " = " . $resultsretrieval[$i]['overall']['precision']);
// print_r("<br>RECALL KE- " . $i . " = " . $resultsretrieval[$i]['overall']['recall']);
// print_r("<br>");
// }
// // print_r($resultsretrieval);
// print_r("<br>RATA-RATA");
// print_r("<br>AKURASI = " . Stats::mean($akurasiretrieval));
// print_r("<br>F-MEASURE = " . Stats::mean($fmeasureretrieval));
// print_r("<br>PRESISI = " . Stats::mean($presisiretrieval));
// print_r("<br>RECALL = " . Stats::mean($recallretrieval));
// print_r("<br><br>");
// //===========================================================================================================================================//
// print_r($predictions[0][0]);
// arsort($akurasi, SORT_NUMERIC);
// $id_akurasiurut=array_keys($akurasi);
// print_r($id_akurasiurut);
// if($akurasi[$id_akurasiurut[0]] == $akurasi[$id_akurasiurut[1]]){
// print_r("<br>");
// if ($fmeasure[$id_akurasiurut[0]] >= $fmeasure[$id_akurasiurut[1]]) {
// print_r("id paling bagus = " . $id_akurasiurut[0] . "<br><br>");
// $datalatihbagus = $datalatihpengujian[$id_akurasiurut[0]];
// $dataujibagus = $folds[$id_akurasiurut[0]];
// } else {
// print_r("id paling bagus = " . $id_akurasiurut[1] . "<br><br>");
// $datalatihbagus = $datalatihpengujian[$id_akurasiurut[1]];
// $dataujibagus = $folds[$id_akurasiurut[1]];
// }
// } else {
// print_r("<br>");
// print_r("id paling bagus = " . $id_akurasiurut[0] . "<br><br>");
// $datalatihbagus = $datalatihpengujian[$id_akurasiurut[0]];
// $dataujibagus = $folds[$id_akurasiurut[0]];
// $semuadata = $semuadatalatih[$id_akurasiurut[0]];
// }
//==============================================================================================================================================//
?>