-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add csv function so we can use csv file as sample
- Loading branch information
Showing
7 changed files
with
107 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Test | ||
test/ | ||
tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.