From 59423546eaaf7246b3fb2b160aa5243d19cd3da0 Mon Sep 17 00:00:00 2001 From: hashlag <90853356+hashlag@users.noreply.github.com> Date: Sun, 4 Feb 2024 21:20:00 +0300 Subject: [PATCH] upd README.md, refactor --- README.md | 16 +++++++++++++--- demo/{demo.py => classifier_demo.py} | 4 ---- demo/regressor_demo.py | 2 +- {neighbours => src/neighbours}/__init__.py | 0 {neighbours => src/neighbours}/classifier.py | 2 +- {neighbours => src/neighbours}/distance.py | 0 {neighbours => src/neighbours}/exceptions.py | 0 {neighbours => src/neighbours}/kernel.py | 0 {neighbours => src/neighbours}/regressor.py | 2 +- {neighbours => src/neighbours}/rp_neighbours.py | 8 ++++---- 10 files changed, 20 insertions(+), 14 deletions(-) rename demo/{demo.py => classifier_demo.py} (94%) rename {neighbours => src/neighbours}/__init__.py (100%) rename {neighbours => src/neighbours}/classifier.py (97%) rename {neighbours => src/neighbours}/distance.py (100%) rename {neighbours => src/neighbours}/exceptions.py (100%) rename {neighbours => src/neighbours}/kernel.py (100%) rename {neighbours => src/neighbours}/regressor.py (97%) rename {neighbours => src/neighbours}/rp_neighbours.py (97%) diff --git a/README.md b/README.md index 64800cc..a175d65 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Neighbours -Weighted kNN classifier implementation based on random projection forest. +Weighted kNN classifier and Nadaraya-Watson kernel regressor implementation based on random projection forest. ### Classifier description @@ -19,13 +19,22 @@ Built-in distance metrics: Custom distance metrics and smoothing kernels are supported. +Demo: `demo/regressor_demo.py` + +### Regressor description + +Nadaraya-Watson kernel regressor is implemented. + +Demo: `demo/classifier_demo.py` + ### Neighbors search algorithm Random projection forest is used to search for neighbours. RP trees are built by recursive binary splits of space by selected hyperplanes. -On each step algorithm chooses two random objects from train set and calculates a hyperplane symmetrically separating these two objects in feature space. +On each step algorithm chooses two random objects from the train set +and calculates a hyperplane symmetrically separating these two objects in feature space. Such hyperplanes become splitting nodes of an RP tree. Each resulting subset is split again if it contains more than `m` (hyperparameter) objects. @@ -52,6 +61,7 @@ Since nodes are organized into a tree, we can perform search by evaluating the e Import the package, for example, as ```python + import neighbours as ns ``` @@ -84,4 +94,4 @@ The only third-party dependency is `numpy`. ### License -This project is licensed under [the MIT License](https://raw.githubusercontent.com/hashlag/neighbours/main/LICENSE) \ No newline at end of file +This project is licensed under [the MIT License](https://raw.githubusercontent.com/hashlag/neighbours/main/LICENSE). \ No newline at end of file diff --git a/demo/demo.py b/demo/classifier_demo.py similarity index 94% rename from demo/demo.py rename to demo/classifier_demo.py index 2c1d950..67cd4aa 100644 --- a/demo/demo.py +++ b/demo/classifier_demo.py @@ -1,7 +1,3 @@ -import sys - -sys.path.append("../") - import neighbours as ns import numpy as np diff --git a/demo/regressor_demo.py b/demo/regressor_demo.py index 6d25286..1604135 100644 --- a/demo/regressor_demo.py +++ b/demo/regressor_demo.py @@ -1,6 +1,6 @@ -import matplotlib.pyplot as plt import neighbours as ns +import matplotlib.pyplot as plt import numpy as np import random import math diff --git a/neighbours/__init__.py b/src/neighbours/__init__.py similarity index 100% rename from neighbours/__init__.py rename to src/neighbours/__init__.py diff --git a/neighbours/classifier.py b/src/neighbours/classifier.py similarity index 97% rename from neighbours/classifier.py rename to src/neighbours/classifier.py index 4a8ed71..ab82e4c 100644 --- a/neighbours/classifier.py +++ b/src/neighbours/classifier.py @@ -28,7 +28,7 @@ class KNNClassifier: """ self.features = features - self.forest = RPTForest(features, trees_count, rpt_m) + self.forest = RPForest(features, trees_count, rpt_m) self.classes = None self.classes_count = classes_count diff --git a/neighbours/distance.py b/src/neighbours/distance.py similarity index 100% rename from neighbours/distance.py rename to src/neighbours/distance.py diff --git a/neighbours/exceptions.py b/src/neighbours/exceptions.py similarity index 100% rename from neighbours/exceptions.py rename to src/neighbours/exceptions.py diff --git a/neighbours/kernel.py b/src/neighbours/kernel.py similarity index 100% rename from neighbours/kernel.py rename to src/neighbours/kernel.py diff --git a/neighbours/regressor.py b/src/neighbours/regressor.py similarity index 97% rename from neighbours/regressor.py rename to src/neighbours/regressor.py index 119b6c8..fa49bd1 100644 --- a/neighbours/regressor.py +++ b/src/neighbours/regressor.py @@ -26,7 +26,7 @@ class KNNRegressor: """ self.features = features - self.forest = RPTForest(features, trees_count, rpt_m) + self.forest = RPForest(features, trees_count, rpt_m) self.targets = None def load(self, points, targets): diff --git a/neighbours/rp_neighbours.py b/src/neighbours/rp_neighbours.py similarity index 97% rename from neighbours/rp_neighbours.py rename to src/neighbours/rp_neighbours.py index 398384b..b79239c 100644 --- a/neighbours/rp_neighbours.py +++ b/src/neighbours/rp_neighbours.py @@ -48,7 +48,7 @@ class LeafNode: """ def __init__(self, ixs): - """Creates new LeafNode with given set of point indexes + """Creates new LeafNode with a given set of point indexes :param ixs: list of point indexes """ @@ -56,8 +56,8 @@ class LeafNode: self.indexes = set(ixs) -class RPTForest: - """Forest of random projection trees +class RPForest: + """Random projection forest Attributes: features: number of features in each sample @@ -68,7 +68,7 @@ class RPTForest: """ def __init__(self, features, trees_count, m): - """Creates new random projection tree forest + """Creates new random projection forest :param features: number of features in each sample :param trees_count: number of trees in the forest