From 160c57456dcb74e6ac2384d344eb5a775138ec64 Mon Sep 17 00:00:00 2001 From: Khoerul Umam Date: Wed, 20 Feb 2019 20:25:05 +0700 Subject: [PATCH] add csv function so we can use csv file as sample --- .gitignore | 2 +- README.md | 30 ++++++++++++++++++++++ dataset/file.csv | 12 ++++++--- example.php | 2 +- src/KnnCsv.php | 67 +++++++++++++++++++++++++++++++++++++++++++++--- tests/csv.php | 5 ++-- tests/file.csv | 2 -- 7 files changed, 107 insertions(+), 13 deletions(-) delete mode 100644 tests/file.csv diff --git a/.gitignore b/.gitignore index 20fd6f3..6f8e393 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ # Test -test/ \ No newline at end of file +tests/ \ No newline at end of file diff --git a/README.md b/README.md index 711ebc1..6613165 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,33 @@ echo $data->result; //output is c ``` more examples are in example.php file + +### Using csv file as dataset +You can use csv file as dataset. Here the tutorial how to use it. + +First create the dataset in csv format. Use this format below. + +``` +"parameter1", "parameter2", "parameter3", "etc", "label" +``` + +See file.csv inside dataset folder for example. +Save it to dataset folder. + +The Code + +```php +require('src/Knn.php'); + +$csvFileName = 'files.csv'; //name of csv file, must containt .csv {required} +$predict = [3,4,1]; //predict {required} +$key = 3; //key {optional: default is 3} +$inputToCsv = true; //true, so the result will be inputed to csv file as the new sample. {optional: default is false} + +$data = new KnnCsv($csvFileName, $predict, $key, $inputToCsv); +echo $data->result; + +``` + +You can change the dataset folder in `src/KnnCsv.php` + diff --git a/dataset/file.csv b/dataset/file.csv index ff56585..560c61a 100644 --- a/dataset/file.csv +++ b/dataset/file.csv @@ -1,3 +1,9 @@ -24,43,"Apple" -24,22,"Mango" -21,54,"Apple" +"weight", "size", "label" +24,43,"Apple" +24,22,"Mango" +21,54,"Apple" +1,7,"Apple" +23,14,Mango +24,134,Mango +53,14,Mango +13,52, Apple diff --git a/example.php b/example.php index 2a5ff63..167ad7f 100644 --- a/example.php +++ b/example.php @@ -5,7 +5,7 @@ $datasample = [[7, 5, 6], [3, 4, 3], [5, 7, 7], [10, 6, 1], [12, 9, 8], [11, 1, 2]]; //Sample Data $datalabel = ['a', 'a', 'a', 'b', 'b', 'c']; //Label Data. [7,5,6] labeled by a, [10,6,1] labeled by b, etc $key = 3; //optional. Default is 3 -$predict = [1, 7]; //Input +$predict = [13, 41]; //Input $data = new Knn($datasample, $predict, $datalabel, $key); echo $data->result; diff --git a/src/KnnCsv.php b/src/KnnCsv.php index a547470..0f72616 100644 --- a/src/KnnCsv.php +++ b/src/KnnCsv.php @@ -1,5 +1,11 @@ $filePath = $filePath; - - $file = fopen('../dataset/' . $filePath, 'r'); + $this->filePath = '../dataset/' . $filePath; + $this->prediction = $predictData; + $file = fopen($this->filePath, 'r'); while (($line = fgetcsv($file)) !== false) { $resultCsv[] = $line; } fclose($file); + array_shift($resultCsv); + for ($i = 0; $i < count($resultCsv); $i++) { $label[] = end($resultCsv[$i]); @@ -30,5 +40,54 @@ public function __construct($filePath, $predictData, $k = 3) $result = new Knn($this->sample, $predictData, $this->label, $k); $this->result = $result->result; + + if ($insertNewToCsv == true) { + + $this->addToCsv($predictData); + + } + + } + + private function addToCsv($dataToInsert) + { + + $newData = $dataToInsert; + array_push($newData, $this->result); + + $stringToInput = ''; + $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($newData)); + foreach ($it as $v) { + $stringToInput .= $v . ","; + } + + $newString = rtrim($stringToInput, ', '); + $dataArrayToInsert[] = $newString; + + $fileCsv = fopen($this->filePath, "a"); + + foreach ($dataArrayToInsert as $line) { + fputcsv($fileCsv, explode(',', $line)); + } + + fclose($fileCsv); + } + + public function getLabel() + { + + return $this->label; + } + + public function getSample() + { + + return $this->sample; + } + + public function getPrediction() + { + + return $this->prediction; } } \ No newline at end of file diff --git a/tests/csv.php b/tests/csv.php index f8863d4..19ed3a6 100644 --- a/tests/csv.php +++ b/tests/csv.php @@ -8,7 +8,7 @@ // $test = [[47, 5, 6], [3, 64, 3], [5, 7, 7], [10, 6, 1], [12, 9, 8], [11, 1, 2]]; //Sample Data // $label = ['a', 'a', 'a', 'b', 'b', 'c']; //Label Data. [7,5,6] labeled by a, [10,6,1] labeled by b, etc // $key = 3; //optional. Default is 3 -$predict = [1, 7]; //Input +$predict = [32, 13]; //Input // $csv = new KnnCsv('file.csv', $predict, 3); @@ -16,4 +16,5 @@ // print_r($test); $data = new KnnCsv('file.csv', $predict, 3); -echo $data->result; +echo $data->result . "\n"; +print_r($data->result); diff --git a/tests/file.csv b/tests/file.csv deleted file mode 100644 index 621e1d5..0000000 --- a/tests/file.csv +++ /dev/null @@ -1,2 +0,0 @@ -24,43,53,52,52,42,"Apple" -24,22,24,64,13,42,"Mango"