{"id":19363828,"url":"https://github.com/khumam/knn-algorithm","last_synced_at":"2025-08-20T22:07:35.773Z","repository":{"id":117123503,"uuid":"171500543","full_name":"khumam/Knn-Algorithm","owner":"khumam","description":"This is the simple KNN Algorithm using PHP and you can use csv file as dataset","archived":false,"fork":false,"pushed_at":"2019-10-22T23:20:52.000Z","size":12,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-27T02:38:54.278Z","etag":null,"topics":["knn","knn-algorithm","knn-php","machine-learning","machine-learning-algorithms","ml","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khumam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-19T15:38:45.000Z","updated_at":"2025-03-01T21:02:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"fa7a521d-b9d1-4bef-932f-14d48d6ab9fa","html_url":"https://github.com/khumam/Knn-Algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khumam%2FKnn-Algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khumam%2FKnn-Algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khumam%2FKnn-Algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khumam%2FKnn-Algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khumam","download_url":"https://codeload.github.com/khumam/Knn-Algorithm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248704351,"owners_count":21148350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["knn","knn-algorithm","knn-php","machine-learning","machine-learning-algorithms","ml","php"],"created_at":"2024-11-10T07:35:34.827Z","updated_at":"2025-04-13T11:24:59.204Z","avatar_url":"https://github.com/khumam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KNN Algorithm\nThis 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.\n\n# Usage\nrequire the Knn.php inside src folder\n\n```php\nrequire 'src/Knn.php';\n```\nHere an example\n\n```php\n$datasample = [[2,3],[4,5],[5,1],[8,9],[3,7],[1,7]]; //Sample Data\n$datalabel = ['a', 'a', 'b', 'b', 'c', 'c']; //Label Data. [2,3] labeled by a, [5,1] labeled by b, etc\n$key = 3;     //optional. Default is 3. Smallest key is more accurate\n$predict = [1, 7]; //Input\n\n$data = new Knn($datasample, $datalabel, $predict, $key);\necho $data-\u003eresult;     //output is c\n```\n\nmore examples are in example.php file\n\n### Using csv file as dataset\nYou can use csv file as dataset. Here the tutorial how to use it.\n\nFirst create the dataset in csv format. Use this format below.\n\n```\n\"parameter1\", \"parameter2\", \"parameter3\", \"etc\", \"label\"\n```\n\nexample\n\n```\n21,43,14,\"Apple\"\n```\n\nSee file.csv inside dataset folder for example.\nSave it to dataset folder.\n\nThe Code\n\n```php\nrequire('src/Knn.php');\n\n$csvFileName = 'files.csv';     //name of csv file, must containt .csv {required}\n$predict = [3,4,1];             //predict {required}\n$key = 3;                       //key {optional: default is 3}\n$inputToCsv = true;             //true, so the result will be inputed to csv file as the new sample. {optional: default is false}\n\n$data = new KnnCsv($csvFileName, $predict, $key, $inputToCsv);\necho $data-\u003eresult;\n\n```\n\nYou can change the dataset folder in `src/KnnCsv.php`\n\n### Train data\n\n```php\n$fileCsvName = 'file.csv';          //csv file name\n$countData = 20;                    //Number of data will be inserted\n$offset = 1;                        //Create offset random number\n$limit = 200;                       //Create max random number\n\nnew KnnTrain($fileCsvName, $countData, $offset, $limit);\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhumam%2Fknn-algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhumam%2Fknn-algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhumam%2Fknn-algorithm/lists"}