{"id":22895891,"url":"https://github.com/daniel-lima-lopez/ktlnn-python","last_synced_at":"2025-03-31T23:39:00.576Z","repository":{"id":251148739,"uuid":"836394048","full_name":"daniel-lima-lopez/kTLNN-Python","owner":"daniel-lima-lopez","description":"A python implementation of the classifier k Two Layers Nearest Neighbors","archived":false,"fork":false,"pushed_at":"2024-08-13T19:36:46.000Z","size":482,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T01:45:41.367Z","etag":null,"topics":["knn","knn-algorithm","knn-classification","ktlnn","machine-learning"],"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/daniel-lima-lopez.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}},"created_at":"2024-07-31T18:50:01.000Z","updated_at":"2024-08-13T19:36:49.000Z","dependencies_parsed_at":"2024-08-13T22:35:40.307Z","dependency_job_id":"42b90be8-0b01-4da9-bdfa-6a99eac2ef85","html_url":"https://github.com/daniel-lima-lopez/kTLNN-Python","commit_stats":null,"previous_names":["daniel-lima-lopez/ktlnn-python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel-lima-lopez%2FkTLNN-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel-lima-lopez%2FkTLNN-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel-lima-lopez%2FkTLNN-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daniel-lima-lopez%2FkTLNN-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daniel-lima-lopez","download_url":"https://codeload.github.com/daniel-lima-lopez/kTLNN-Python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246558117,"owners_count":20796696,"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-classification","ktlnn","machine-learning"],"created_at":"2024-12-13T23:32:37.132Z","updated_at":"2025-03-31T23:39:00.558Z","avatar_url":"https://github.com/daniel-lima-lopez.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kTLNN-Python\nThis is a python implementation of the Two-Layer Nearest Neighbor classifier proposed by [Wang et al.](https://www.sciencedirect.com/science/article/abs/pii/S0950705121008662)\n\n## Classifier description\nThe operation of the classifier is divided into three stages, given a query point $x$:\n1. The first layer is built by the $k$ nearest neighbors of $x$. Then the second layer is formed by the $k$ nearest neighbors of each point in the first layer.\n2. Considering the distribution of the query with the second layer points, the extended neighborhood is built with the most salient points in the first and second layer.\n3. The final neighborhood is formed by the points in the extended neighborhood which contain $x$ among their $k_b$ nearest neighbors (backward nearest neighbor relation). The final vote to decide the $x$ class is performed on this neighborhood.\n\n## Installation\nClone this repository\n```bash\ngit clone git@github.com:daniel-lima-lopez/kTLNN-Python.git\n```\nmove to installation directory:\n```bash\ncd kTLNN-Python\n```\n## Basic usage\nImport the class and instantiate the classifier\n```python\nimport TLNN as tl\nclassifier = tl.kTLNN(k=5, kb_factor=1.4)\n```\nwhere:\n- `k` is the number of neighboors considerd in the construction of layers.\n- `kb_factor` is the $c$ factor to calculate the $k_b$ parameter, calculated as $k_b=c\\cdot k$. By default this parameter has value $1.4$, as suggested by the authors.\n\nOnce instantiated the classifier, we can perform a simple test:\n```python\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\n# data preparation\ndataset = pd.read_csv('Datasets/iris.csv')\nX = dataset.drop('class', axis=1).values\ny = dataset['class'].values\n\n# data split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n\n# fit the classifier\nclassifier.fit(X_train, y_train)\n\n# predictions\npreds = classifier.predict(X_test)\nprint(f'accuracy: {accuracy_score(y_true=y_test, y_pred=preds)}')\n\n```\n\n## Experiments\nExperiments were performed with the datasets: balance-scale, bands and wine. On each experiment, a 10-fold cross validation was applied considering the `k` values 1, 3, 5, 7, 9 and 11. The accuracy on each experiment is presented in the following figures:\n\n\u003cimg src=\"imgs/ks_balance-scale.png\" alt=\"drawing\" width=\"500\"/\u003e\n\n\u003cimg src=\"imgs/ks_bands.png\" alt=\"drawing\" width=\"500\"/\u003e\n\n\u003cimg src=\"imgs/ks_wine.png\" alt=\"drawing\" width=\"500\"/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniel-lima-lopez%2Fktlnn-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaniel-lima-lopez%2Fktlnn-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniel-lima-lopez%2Fktlnn-python/lists"}