https://github.com/daniel-lima-lopez/ktlnn-python
A python implementation of the classifier k Two Layers Nearest Neighbors
https://github.com/daniel-lima-lopez/ktlnn-python
knn knn-algorithm knn-classification ktlnn machine-learning
Last synced: 3 months ago
JSON representation
A python implementation of the classifier k Two Layers Nearest Neighbors
- Host: GitHub
- URL: https://github.com/daniel-lima-lopez/ktlnn-python
- Owner: daniel-lima-lopez
- Created: 2024-07-31T18:50:01.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-08-13T19:36:46.000Z (10 months ago)
- Last Synced: 2025-02-07T01:45:41.367Z (5 months ago)
- Topics: knn, knn-algorithm, knn-classification, ktlnn, machine-learning
- Language: Python
- Homepage:
- Size: 471 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# kTLNN-Python
This 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)## Classifier description
The operation of the classifier is divided into three stages, given a query point $x$:
1. 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.
2. 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.
3. 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.## Installation
Clone this repository
```bash
git clone [email protected]:daniel-lima-lopez/kTLNN-Python.git
```
move to installation directory:
```bash
cd kTLNN-Python
```
## Basic usage
Import the class and instantiate the classifier
```python
import TLNN as tl
classifier = tl.kTLNN(k=5, kb_factor=1.4)
```
where:
- `k` is the number of neighboors considerd in the construction of layers.
- `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.Once instantiated the classifier, we can perform a simple test:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score# data preparation
dataset = pd.read_csv('Datasets/iris.csv')
X = dataset.drop('class', axis=1).values
y = dataset['class'].values# data split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)# fit the classifier
classifier.fit(X_train, y_train)# predictions
preds = classifier.predict(X_test)
print(f'accuracy: {accuracy_score(y_true=y_test, y_pred=preds)}')```
## Experiments
Experiments 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:
![]()