From 6c632d91fc488d3de72685fe07aad4e893ec2299 Mon Sep 17 00:00:00 2001 From: Khoerul Umam Date: Wed, 23 Oct 2019 06:20:35 +0700 Subject: [PATCH] fix some problem. I hope this is fixing all bugs --- README.md | 8 +++++++- dataset/file.csv | 2 +- example.php | 7 ------- src/KnnCsv.php | 10 ++++------ testcsc.php | 11 +++++++++++ 5 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 testcsc.php diff --git a/README.md b/README.md index 15dd462..fd7ce5a 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ $datalabel = ['a', 'a', 'b', 'b', 'c', 'c']; //Label Data. [2,3] labeled by a, [ $key = 3; //optional. Default is 3. Smallest key is more accurate $predict = [1, 7]; //Input -$data = new Knn($datasample, $predict, $datalabel, $key); +$data = new Knn($datasample, $datalabel, $predict, $key); echo $data->result; //output is c ``` @@ -30,6 +30,12 @@ First create the dataset in csv format. Use this format below. "parameter1", "parameter2", "parameter3", "etc", "label" ``` +example + +``` +21,43,14,"Apple" +``` + See file.csv inside dataset folder for example. Save it to dataset folder. diff --git a/dataset/file.csv b/dataset/file.csv index 95338d5..b002d6e 100644 --- a/dataset/file.csv +++ b/dataset/file.csv @@ -1,4 +1,3 @@ -"weight", "size", "label" 24,43,"Apple" 24,22,"Mango" 21,54,"Apple" @@ -44,3 +43,4 @@ 70,117,Apple 123,119,Apple 17,129,Apple +175,40,"The count of Label data is not match with Sample Data" diff --git a/example.php b/example.php index 990ac39..5a3f6d7 100644 --- a/example.php +++ b/example.php @@ -10,10 +10,3 @@ $data = new Knn($datasample, $datalabel, $predict, $key); echo $data->result; echo "\n"; -// print_r($data->getSample()); -// echo "\n"; -// print_r($data->getLabel()); -// echo "\n"; -// print_r($data->getPrediction()); -// echo "\n"; -// print_r($data->getSquare()); diff --git a/src/KnnCsv.php b/src/KnnCsv.php index 328c834..d5d383c 100644 --- a/src/KnnCsv.php +++ b/src/KnnCsv.php @@ -20,11 +20,11 @@ class KnnCsv public function __construct($filePath, $predictData = [], $k = 3, $insertNewToCsv = false) { - $this->filePath = '../dataset/' . $filePath; + $this->filePath = 'dataset/' . $filePath; $this->prediction = $predictData; $file = fopen($this->filePath, 'r'); - while (($line = fgetcsv($file)) !== false) { + while (($line = fgetcsv($file))) { $resultCsv[] = $line; } @@ -42,15 +42,13 @@ public function __construct($filePath, $predictData = [], $k = 3, $insertNewToCs $this->label = $label; $this->sampleLength = count($this->sample); - $result = new Knn($this->sample, $predictData, $this->label, $k); + $result = new Knn($this->sample, $this->label, $predictData, $k); $this->result = $result->result; if ($insertNewToCsv == true) { $this->addToCsv($predictData, $result->result); - } - } private function addToCsv($dataToInsert, $result) @@ -95,4 +93,4 @@ public function getPrediction() return $this->prediction; } -} \ No newline at end of file +} diff --git a/testcsc.php b/testcsc.php new file mode 100644 index 0000000..2eab926 --- /dev/null +++ b/testcsc.php @@ -0,0 +1,11 @@ +result;