Git Product home page Git Product logo

knnfeat's Introduction

knnFeat

Feature Extraction with KNN

Description

Python implementation of feature extraction with KNN.

And @momijiame updated my implementation. I recommend to use this:
https://github.com/momijiame/gokinjo

pip install gokinjo

The following is R implementation:
http://davpinto.com/fastknn/articles/knn-extraction.html#understanding-the-knn-features

Requirements

  • Python 3.x
    • numpy
    • scikit-learn
    • scipy

Install

git clone [email protected]:upura/knnFeat.git
cd knnFeat
pip install -r requirements.txt

Demo

Notebook version can be seen here.

Packages for visualization

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt

Data generation

x0 = np.random.rand(500) - 0.5
x1 = np.random.rand(500) - 0.5
X = np.array(list(zip(x0, x1)))
y = np.array([1 if i0 * i1 > 0 else 0 for (i0, i1)  in list(zip(x0, x1))])

Visualization

Feature extraction with KNN

from knnFeat import knnExtract
newX = knnExtract(X, y, k = 1, folds = 5)

Visualization

Algorithm

Quote from here.

It generates k * c new features, where c is the number of class labels. The new features are computed from the distances between the observations and their k nearest neighbors inside each class, as follows:

  1. The first test feature contains the distances between each test instance and its nearest neighbor inside the first class.
  2. The second test feature contains the sums of distances between each test instance and its 2 nearest neighbors inside the first class.
  3. The third test feature contains the sums of distances between each test instance and its 3 nearest neighbors inside the first class.
  4. And so on.

This procedure repeats for each class label, generating k * c new features. Then, the new training features are generated using a n-fold CV approach, in order to avoid overfitting.

Development

flake8 .
pytest
pytest -v -m 'success' --cov=.

License

MIT

Author

upura

knnfeat's People

Contributors

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