{"id":27389441,"url":"https://github.com/holygrease/algorithm-k-nn","last_synced_at":"2025-08-22T14:12:00.605Z","repository":{"id":171717381,"uuid":"369836843","full_name":"HolyGrease/Algorithm-k-NN","owner":"HolyGrease","description":"k-NN methods assign the classification of new example most similar to the classification of k nearby training examples.","archived":false,"fork":false,"pushed_at":"2021-05-31T19:13:51.000Z","size":130,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T19:18:20.362Z","etag":null,"topics":["classification","classification-algorithm"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HolyGrease.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2021-05-22T15:03:32.000Z","updated_at":"2021-05-31T19:13:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"c4950d20-81c7-4da1-b162-8502dc6e9cc3","html_url":"https://github.com/HolyGrease/Algorithm-k-NN","commit_stats":null,"previous_names":["holygrease/algorithm-k-nn"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HolyGrease/Algorithm-k-NN","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HolyGrease%2FAlgorithm-k-NN","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HolyGrease%2FAlgorithm-k-NN/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HolyGrease%2FAlgorithm-k-NN/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HolyGrease%2FAlgorithm-k-NN/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HolyGrease","download_url":"https://codeload.github.com/HolyGrease/Algorithm-k-NN/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HolyGrease%2FAlgorithm-k-NN/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271650860,"owners_count":24796725,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["classification","classification-algorithm"],"created_at":"2025-04-13T19:14:57.018Z","updated_at":"2025-08-22T14:12:00.590Z","avatar_url":"https://github.com/HolyGrease.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Example of creating Dataset object\r\nFirstly, import Dataset:\r\n\r\n\tfrom dataset import Dataset\r\n\r\nSecondly, get some data:\r\n\r\n\tdata = [\r\n\t\t[5.1, 3.5, 1.4, 0.2, \"Iris-setosa\"],\r\n\t\t[5.0, 3.2, 1.2, 0.2, \"Iris-setosa\"],\r\n\t\t[6.4, 3.2, 4.5, 1.5, \"Iris-versicolor\"],\r\n\t\t[6.7, 3.1, 4.4, 1.4, \"Iris-versicolor\"],\r\n\t\t[6.7, 3.0, 5.2, 2.3, \"Iris-virginica\"]]\r\n\r\nThirdly, set columns (attributes) names:\r\n\r\n\tcolumn_names = [\r\n\t\t\"Sepal length\", \"Sepal width\",\r\n\t\t\"Petal length\", \"Petal width\",\r\n\t\t\"Class\"]\r\n\r\nNow we can create Dataset object. Arguments:\r\n- data - just list of list\r\n- target index - index of target attribute, attribute that contains classes values\r\n- column or attributes names - list of attributes names\r\n- name - Dataset name\r\n\u003ca/\u003e\r\n\r\n\tiris = Dataset(data, 4, column_names, \"Iris\")\r\n\r\nAlso you can just get iris dataset by calling method\r\n\r\n\tget_iris().\r\n\r\nYou can specify path to dataset file by passing this path as argument, for example:\r\n\r\n\tget_iris(\"data\\\\iris.data\")\r\n\r\nDefault value of path \r\n\u003e resources\\\\data\\\\iris\\\\iris.data.\r\n\r\n\tiris = get_iris()\r\n\r\n# Preprocessing dataset\r\nShuffle dataset:\r\n\r\n\tiris = iris.shuffle()\r\n\r\nSplit dataset on \"train\" and \"test\", as argument passing ratio. Train dataset gets 80% of original dataset, test - other:\r\n\r\n\ttrain, test = iris.split_by_ration(0.8)\r\n\r\n# Classification\r\nDon't forget to import, instead of euclidean you can import any other implemented metric:\r\n\r\n\tfrom k_NN import k_NN\r\n\tfrom k_NN import euclidean\r\n\r\n### Basic classification\r\nFor basic classification use k_NN() function with only 4 arguments:\r\n- dataset - train dataset which used for classification\r\n- k - integer, number of neighbours that will be used to predict class\r\n- row - instance to classify, must contains all attributes except target\r\n- metric - metric used to calculate distance. k_NN.py file contains some metric, use one of them or implement yours.\r\n\u003ca/\u003e\r\nk_NN method return class according to train dataset.\r\n\r\nRemember you need to delete target attribute from instance that you classify.\r\n\r\n\tassert_class = test.data[0].pop(test.target)\r\n\tinstance_to_classify = [4.8, 3.1, 1.6, 0.2]\r\n\tpredicted_class = k_NN(train, instance_to_classify, 3, euclidean)\r\n\tprint(f\"{assert_class} ?= {predicted_class}\")\r\n\r\nIn terminal you can see something like this\r\n\u003e Iris-setosa ?= Iris-setosa\r\n### Attribute weight classification\r\nAttribute weights are used with calculating distances. This weights determine how specific attribute influece on general distance.\r\nIn this case you need to define weights. For example like this:\r\n\r\n\tattributes_weights = [0.1, 0.5, 0.6, 0.3]\r\n\r\nNumber of weights must be equal number of attributes - 1 (except target attribute)\r\nAlso you can use build in Dataset method called [inforamtion gain](https://machinelearningmastery.com/information-gain-and-mutual-information/#:~:text=Information%20gain%20is%20the%20reduction,before%20and%20after%20a%20transformation.) to get data based weights. Example:\r\n\r\n\tattributes_weights = [\r\n\t\tDataset.gain(\r\n\t\t\ttrain.get_column(i),\r\n\t\t\ttrain.get_target_column())\r\n\t\tfor i in range(4)]\r\n\r\nTo use this weights in classification you need pass one more argument:\r\n\r\n\tpredicted_class = k_NN(train, row, 3, euclidean, attributes_weights=attributes_weights)\r\n\r\n### Distance weight classification\r\nIn this case you need to define weights. For example like this:\r\n\r\n\tdistances_weights = [0.1, 0.5, 0.6, 0.3]\r\n\r\nTo use this weights in classification you need pass one more argument:\r\n\r\n\tpredicted_class = k_NN(train, row, 3, euclidean, distances_weights)\r\n### Combine weight classification\r\nAlso you can combine both, attribute and distance weights, in this algorithm.\r\n\r\n\tpredicted_class = k_NN(train, row, 3, euclidean, distances_weights, attributes_weights)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fholygrease%2Falgorithm-k-nn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fholygrease%2Falgorithm-k-nn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fholygrease%2Falgorithm-k-nn/lists"}