Skip to content

Commit

Permalink
add csv file supported
Browse files Browse the repository at this point in the history
  • Loading branch information
khumam committed Feb 20, 2019
1 parent decae05 commit e781af2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test
test/
3 changes: 3 additions & 0 deletions dataset/file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
24,43,"Apple"
24,22,"Mango"
21,54,"Apple"
4 changes: 1 addition & 3 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@
echo "\n";
print_r($data->getPrediction());
echo "\n";
print_r($data->getSquare());


print_r($data->getSquare());
10 changes: 5 additions & 5 deletions src/Knn.php
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
{
Expand Down
34 changes: 34 additions & 0 deletions src/KnnCsv.php
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;
}
}
19 changes: 19 additions & 0 deletions tests/csv.php
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;
2 changes: 2 additions & 0 deletions tests/file.csv
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"

0 comments on commit e781af2

Please sign in to comment.