Skip to content

Commit

Permalink
add csv function so we can use csv file as sample
Browse files Browse the repository at this point in the history
  • Loading branch information
khumam committed Feb 20, 2019
1 parent e781af2 commit 160c574
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Test
test/
tests/
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

12 changes: 9 additions & 3 deletions dataset/file.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 63 additions & 4 deletions src/KnnCsv.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
<?php

/*KNN from CSV
This is a class for processing KNN from dataset in csv type.
Author : Khoerul Umam
Email : [email protected]*/

class KnnCsv
{

protected $filePath;
public $sample;
public $label;
public $result;
public $newData;
public $prediction;

public function __construct($filePath, $predictData, $k = 3)
public function __construct($filePath, $predictData, $k = 3, $insertNewToCsv = false)
{
$this->$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]);
Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions tests/csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
// $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);

// print_r($csv->sample);
// print_r($test);

$data = new KnnCsv('file.csv', $predict, 3);
echo $data->result;
echo $data->result . "\n";
print_r($data->result);
2 changes: 0 additions & 2 deletions tests/file.csv

This file was deleted.

0 comments on commit 160c574

Please sign in to comment.