Skip to content

Commit

Permalink
fix some problem. I hope this is fixing all bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
khumam committed Oct 22, 2019
1 parent bd5ac9e commit 6c632d9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion dataset/file.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"weight", "size", "label"
24,43,"Apple"
24,22,"Mango"
21,54,"Apple"
Expand Down Expand Up @@ -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"
7 changes: 0 additions & 7 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
10 changes: 4 additions & 6 deletions src/KnnCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -95,4 +93,4 @@ public function getPrediction()

return $this->prediction;
}
}
}
11 changes: 11 additions & 0 deletions testcsc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require('src/Knn.php');

$csvFileName = 'file.csv'; //name of csv file, must containt .csv {required}
$predict = [3, 4]; //predict {required}
$key = 3; //key {optional: default is 3}
$inputToCsv = false; //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;

0 comments on commit 6c632d9

Please sign in to comment.