Coder Social home page Coder Social logo

ldv1 / kstar-nn Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 2.0 1.71 MB

Implementation of the k*-Nearest Neighbors method in Python

Python 100.00%
python-3 machine-learning nearest-neighbors regression-models classification-algorithm kd-tree regression-algorithms nadaraya-watson random-forest

kstar-nn's Introduction

k*-Nearest Neighbors

Simple implementation of the k*-NN proposed by Anava and Levy in k*-Nearest Neighbors: From Global to Local.

Motivation

Recently, Coscrato et al. in NLS: an accurate and yet easy-to-interpret regression method trained a neural network to spit out the coefficients of a local linear regression. They sought "a method that that is complex enough to give good predictions, and yet gives solutions that are easy to be interpreted without the need of using a separate interpreter".

I was a bit disappointed that they did not include the k*-NN method in their benchmark. I guess, the reason therefore is that there is no implementation (that I know of).

k*-NN is a simple approach to locally weighted regression/classification, but we only consider regression. The optimal weights, and the optimal number of neighbors, are efficiently found for each data point whose value we wish to estimate. In this respect, k*-NN is data-adaptive.

k*-NN was shown to beat the standard k-NN and the Nadarays-Watson estimator on a collection of datasets. But it is not widespreadly used. Why? I believe the issue is that its performance degrades drastically in presence of irrelevant covariates. So, a preprocessing step is needed to select relevant features. In this respect, Random Forest works very well out-of-the-box and is robust to inclusion of irrelevant features.

Dependencies

You will need python 3 with numpy and sklearn. If you intend to run the tests, then matplotlib must be installed.

Code

It is light-weight: A single class kstarnn defined in kstarnn.py. It is sklearn-compatible with fit and predict methods. The fit method just builds a KDTree.

The number of neighbors needed for prediction at a given point is data-driven. Ideally, we should add one neighbor at a time. However, no KDTree offers this functionality: We can only query a fix number of nearest neighbors. Stated otherwise, there is no method query_next. So, we just query a bunch of neighbors (30 by default) with the hope that it is sufficient. If not, a warning is issued. This is the main weakness of the present implementation.

Code example

Suppose train_X (a numpy array) and train_Y (a numpy vector) are the training data sets: Inputs and labels (continuous for regression), respectively. Then the following will fit a k*-NN model:

knn = kstarnn(alpha=10.)
knn.fit(Xtrain, ytrain)
preds, ngbs = knn.predict(Xtest, return_ngbs = True)

With the option return_ngbs = True (False by default), the indices of the neighbors per test data point are returned in a list.

The parameter alpha is usually set by cross-validation. See test.py for an example.

Parameters

  • alpha [default=1]
    • This is L/C where L designates the Lipschitz constant of the unknown function to regress and C is a constant coming from the Hoeffding's inequality, see paper.
  • max_num_neighbors [default=30]
    • Maximum number of neighbors to request in a query.
  • copy_X_train [default=True]
    • If True, the training dataset is copied.

Tests

Run test.py to produce the following:

Demo

Zero-mean Gaussian noise with stand deviation of 0.1 was added to the targets.

Now we consider the same underlying sine function as a function of x_1, and add an irrelevant feature x_2. We plot the projection onto the covariate x_1:

Demo

Random Forest does a very good job whereas k*-NN performs poorly.

Authors

Laurent de Vito

License

All third-party libraries are subject to their own license.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

kstar-nn's People

Contributors

ldv1 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.