-
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.
- Loading branch information
Showing
7 changed files
with
66 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Test | ||
test/ |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
24,43,"Apple" | ||
24,22,"Mango" | ||
21,54,"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,13 +1,13 @@ | ||
<?php | ||
|
||
/* | ||
require_once('KnnCsv.php'); | ||
|
||
This is a simple Machine Learning program using the KNN or K - Nearest Neighbor algorithm. Maybe the code below can't be read properly, so I try to make this program so that it can be read better and for anyone who wants to use it is welcome. | ||
/* This is a simple Machine Learning program using the KNN or K - Nearest Neighbor algorithm. | ||
Maybe the code below can't be read properly, so I try to make this program so that it can | ||
be read better and for anyone who wants to use it is welcome. | ||
Author: Khoerul Umam | ||
Email: [email protected] | ||
*/ | ||
Email: [email protected] */ | ||
|
||
class Knn | ||
{ | ||
|
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
class KnnCsv | ||
{ | ||
|
||
protected $filePath; | ||
public $sample; | ||
public $label; | ||
public $result; | ||
|
||
public function __construct($filePath, $predictData, $k = 3) | ||
{ | ||
$this->$filePath = $filePath; | ||
|
||
$file = fopen('../dataset/' . $filePath, 'r'); | ||
while (($line = fgetcsv($file)) !== false) { | ||
|
||
$resultCsv[] = $line; | ||
} | ||
fclose($file); | ||
|
||
for ($i = 0; $i < count($resultCsv); $i++) { | ||
|
||
$label[] = end($resultCsv[$i]); | ||
unset($resultCsv[$i][count($resultCsv[$i]) - 1]); | ||
} | ||
|
||
$this->sample = $resultCsv; | ||
$this->label = $label; | ||
|
||
$result = new Knn($this->sample, $predictData, $this->label, $k); | ||
$this->result = $result->result; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
require '../src/Knn.php'; | ||
// $predict = [3, 2]; | ||
// $test = [[24, 43], [24, 22]]; | ||
// $label = ['A', 'M']; | ||
|
||
// $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 | ||
|
||
// $csv = new KnnCsv('file.csv', $predict, 3); | ||
|
||
// print_r($csv->sample); | ||
// print_r($test); | ||
|
||
$data = new KnnCsv('file.csv', $predict, 3); | ||
echo $data->result; |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
24,43,53,52,52,42,"Apple" | ||
24,22,24,64,13,42,"Mango" |