GithubHelp home page GithubHelp logo

khumam / knn-algorithm Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 2.0 12 KB

This is the simple KNN Algorithm using PHP and you can use csv file as dataset

License: MIT License

PHP 100.00%
machine-learning machine-learning-algorithms ml php knn knn-algorithm knn-php

knn-algorithm's Introduction

KNN Algorithm

This is a simple KNN or K - Nearest Neighbor algorithm. Maybe the code 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.

Usage

require the Knn.php inside src folder

require 'src/Knn.php';

Here an example

$datasample = [[2,3],[4,5],[5,1],[8,9],[3,7],[1,7]]; //Sample Data
$datalabel = ['a', 'a', 'b', 'b', 'c', 'c']; //Label Data. [2,3] labeled by a, [5,1] labeled by b, etc
$key = 3;     //optional. Default is 3. Smallest key is more accurate
$predict = [1, 7]; //Input

$data = new Knn($datasample, $datalabel, $predict, $key);
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"

example

21,43,14,"Apple"

See file.csv inside dataset folder for example. Save it to dataset folder.

The Code

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

Train data

$fileCsvName = 'file.csv';          //csv file name
$countData = 20;                    //Number of data will be inserted
$offset = 1;                        //Create offset random number
$limit = 200;                       //Create max random number

new KnnTrain($fileCsvName, $countData, $offset, $limit);

knn-algorithm's People

Contributors

khumam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.