{"id":13437833,"url":"https://github.com/mljs/knn","last_synced_at":"2025-04-06T06:08:52.042Z","repository":{"id":34911033,"uuid":"38955293","full_name":"mljs/knn","owner":"mljs","description":"A k-nearest neighboor classifier algorithm.","archived":false,"fork":false,"pushed_at":"2023-02-13T14:56:38.000Z","size":1305,"stargazers_count":140,"open_issues_count":3,"forks_count":22,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-30T04:09:46.769Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/mljs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2015-07-12T08:36:34.000Z","updated_at":"2025-03-15T16:53:44.000Z","dependencies_parsed_at":"2022-06-27T12:04:10.063Z","dependency_job_id":"b5e75266-62a1-4f5d-bdb1-e9593f2cbf9a","html_url":"https://github.com/mljs/knn","commit_stats":{"total_commits":38,"total_committers":7,"mean_commits":5.428571428571429,"dds":0.6052631578947368,"last_synced_commit":"afc1bdb8ddba029b7da54015f2d7e84989e54e2b"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fknn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fknn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fknn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2Fknn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mljs","download_url":"https://codeload.github.com/mljs/knn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441052,"owners_count":20939239,"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":[],"created_at":"2024-07-31T03:01:00.570Z","updated_at":"2025-04-06T06:08:52.022Z","avatar_url":"https://github.com/mljs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# knn\n\n[![NPM version][npm-image]][npm-url]\n[![npm download][download-image]][download-url]\n\nA General purpose k-nearest neighbor classifier algorithm based on the k-d tree Javascript library develop by Ubilabs:\n\n- [k-d trees](https://github.com/ubilabs/kd-tree-javascript)\n\n## Installation\n\n`$ npm i ml-knn`\n\n## API\n\n### new KNN(dataset, labels[, options])\n\nInstantiates the KNN algorithm.\n\n**Arguments**:\n\n- `dataset` - A matrix (2D array) of the dataset.\n- `labels` - An array of labels (one for each sample in the dataset).\n- `options` - Object with the options for the algorithm.\n\n**Options**:\n\n- `k` - number of nearest neighbors (Default: number of labels + 1).\n- `distance` - distance function for the algorithm (Default: euclidean distance).\n\n**Example**:\n\n```js\nvar train_dataset = [\n  [0, 0, 0],\n  [0, 1, 1],\n  [1, 1, 0],\n  [2, 2, 2],\n  [1, 2, 2],\n  [2, 1, 2],\n];\nvar train_labels = [0, 0, 0, 1, 1, 1];\nvar knn = new KNN(train_dataset, train_labels, { k: 2 }); // consider 2 nearest neighbors\n```\n\n### predict(newDataset)\n\nPredict the values of the dataset.\n\n**Arguments**:\n\n- `newDataset` - A matrix that contains the dataset.\n\n**Example**:\n\n```js\nvar test_dataset = [\n  [0.9, 0.9, 0.9],\n  [1.1, 1.1, 1.1],\n  [1.1, 1.1, 1.2],\n  [1.2, 1.2, 1.2],\n];\n\nvar ans = knn.predict(test_dataset);\n\nconsole.log(ans);\n// classification result:\n// ans = [ 0, 0, 1, 1 ]\n// Based on the training data, the first two points of the test dataset are classified as \"0\" (type 0, perhaps),\n// the third and fourth data points are classified as \"1\".\n```\n\n### toJSON()\n\nReturns an object representing the model. This function is automatically called if `JSON.stringify(knn)` is used.  \nBe aware that the serialized model takes about 1.3 times the size of the input dataset (it actually is the dataset in a tree structure). Stringification can fail if the resulting string is too large.\n\n### KNN.load(model[, distance])\n\nLoads a model previously exported by `knn.toJSON()`. If a custom distance function was provided, it must be passed again.\n\n## External links\n\nCheck this cool blog post for a detailed example:\nhttps://hackernoon.com/machine-learning-with-javascript-part-2-da994c17d483\n\n## License\n\n[MIT](./LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/ml-knn.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/ml-knn\n[download-image]: https://img.shields.io/npm/dm/ml-knn.svg?style=flat-square\n[download-url]: https://npmjs.org/package/ml-knn\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fknn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmljs%2Fknn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fknn/lists"}