GithubHelp home page GithubHelp logo

johnny-knighten / k-nearest-neighbors Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 6.0 675 KB

KNN using brute force and ball trees implemented in Python/Cython

Python 100.00%
nearest-neighbors nearest-neighbor-search knn knn-classifier ball-tree balltree distance-metric distance-metrics cython python3

k-nearest-neighbors's Introduction

This is a Python/Cython implementation of KNN algorithms. Two algorithms are provided: a brute force algorithm implemented with numpy and a ball tree implemented using Cython. Also provided is a set of distance metrics that are implemented in Cython.

An overview of KNN and ball tress can be found here.

Distance Metrics Provided

Ball tress have less metrics available because the metrics that are used by the ball tree must follow the triangle inequality.

Brute Force

  • Manhattan
  • Euclidean
  • Hamming
  • Pearson
  • Cosine
  • Chi-Squared

Ball Tree

  • Manhattan
  • Euclidean
  • Hamming

How To Install

To install this package first ensure that pip is installed and updated. Then execute the following code while in the package's main directory(the directory containing setup.py):

 pip install .

This will install knn into your current python environment. You then will import knn.models to access classes for classification and regression. knn.models also contains a mixin that provides nearest neighbor search methods you can use in your own classes.

Note - Cython and Numpy are used in this project. Numpy is required but Cython is not. If Cython is not installed the packaged .c files will be complied using your systems default c compiler. Numpy will be installed when installing knn.

Note - This code was designed and tested using Python 3.5.2.

Notes About Use

Currently all implemented algorithms expect numpy arrays as input. All numpy arrays are expected to be of type np.float.

This may be changed in the future.

Example Use

Brute force classification:

import numpy as np
from knn.models import KNNClassification

train_data = np.array([[1., 2., 3.],
                       [5., 6., 7.],
                       [8., 9., 10.]])
train_labels = np.array([0, 1, 1])

test_data = np.array([[5., 10., 15.],
                      [10., 20., 30.]])

# Create and Train Classifier
knn = KNNClassification(metric="euclidean")
knn.train(train_labels, train_data)

# Get Predictions
predictions = knn.predict(test_data, k=1)
print("k=1 Predictions:\n" + str(predictions))

Ball tree classification:

import numpy as np
from knn.models import KNNClassification

train_data = np.array([[1., 2., 3.],
                       [5., 6., 7.],
                       [8., 9., 10.]])
train_labels = np.array([0, 1, 1])

test_data = np.array([[5., 10., 15.],
                      [10., 20., 30.]])

# Create and Train Classifier
knn = KNNClassification(use_tree=True, metric="euclidean")
knn.train(train_labels, train_data)

# Get Predictions
predictions = knn.predict(test_data, k=1)
print("k=1 Predictions:\n" + str(predictions))

For regression just use the KNNRegression class in knn.models.

More examples can be found in example_usage.py.

Possible Updates

  1. Allow the use of numpy arrays of types other than np.float
  2. Implemented Brute Force KNN in Cython

k-nearest-neighbors's People

Contributors

johnny-knighten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.