All Projects → ankane → Neighbor

ankane / Neighbor

Licence: mit
Nearest neighbor search for Rails and Postgres

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Neighbor

pgvector
Open-source vector similarity search for Postgres
Stars: ✭ 482 (+322.81%)
Mutual labels:  nearest-neighbor-search
Milvus
An open-source vector database for embedding similarity search and AI applications.
Stars: ✭ 9,015 (+7807.89%)
Mutual labels:  nearest-neighbor-search
Awesome Cbir Papers
📝Awesome and classical image retrieval papers
Stars: ✭ 1,114 (+877.19%)
Mutual labels:  nearest-neighbor-search
Mlpack
mlpack: a scalable C++ machine learning library --
Stars: ✭ 3,859 (+3285.09%)
Mutual labels:  nearest-neighbor-search
Soundfingerprinting
Open source audio fingerprinting in .NET. An efficient algorithm for acoustic fingerprinting written purely in C#.
Stars: ✭ 554 (+385.96%)
Mutual labels:  nearest-neighbor-search
Pointcloudutilities
Utilities for point cloud processing. read ply, write ply, search nearest neighbors using octree ...
Stars: ✭ 17 (-85.09%)
Mutual labels:  nearest-neighbor-search
docarray
The data structure for unstructured data
Stars: ✭ 561 (+392.11%)
Mutual labels:  nearest-neighbor-search
Gann
gann(go-approximate-nearest-neighbor) is a library for Approximate Nearest Neighbor Search written in Go
Stars: ✭ 75 (-34.21%)
Mutual labels:  nearest-neighbor-search
Smile
Statistical Machine Intelligence & Learning Engine
Stars: ✭ 5,412 (+4647.37%)
Mutual labels:  nearest-neighbor-search
Fast Near Duplicate Image Search
Fast Near-Duplicate Image Search and Delete using pHash, t-SNE and KDTree.
Stars: ✭ 54 (-52.63%)
Mutual labels:  nearest-neighbor-search
Pynndescent
A Python nearest neighbor descent for approximate nearest neighbors
Stars: ✭ 377 (+230.7%)
Mutual labels:  nearest-neighbor-search
Lopq
Training of Locally Optimized Product Quantization (LOPQ) models for approximate nearest neighbor search of high dimensional data in Python and Spark.
Stars: ✭ 530 (+364.91%)
Mutual labels:  nearest-neighbor-search
Falconn
FAst Lookups of Cosine and Other Nearest Neighbors (based on fast locality-sensitive hashing)
Stars: ✭ 919 (+706.14%)
Mutual labels:  nearest-neighbor-search
lbvh
an implementation of parallel linear BVH (LBVH) on GPU
Stars: ✭ 67 (-41.23%)
Mutual labels:  nearest-neighbor-search
Ggnn
GGNN: State of the Art Graph-based GPU Nearest Neighbor Search
Stars: ✭ 63 (-44.74%)
Mutual labels:  nearest-neighbor-search
adventures-with-ann
All the code for a series of Medium articles on Approximate Nearest Neighbors
Stars: ✭ 40 (-64.91%)
Mutual labels:  nearest-neighbor-search
Ngt
Nearest Neighbor Search with Neighborhood Graph and Tree for High-dimensional Data
Stars: ✭ 636 (+457.89%)
Mutual labels:  nearest-neighbor-search
Rii
Fast and memory-efficient ANN with a subset-search functionality
Stars: ✭ 96 (-15.79%)
Mutual labels:  nearest-neighbor-search
Annoy
Approximate Nearest Neighbors in C++/Python optimized for memory usage and loading/saving to disk
Stars: ✭ 9,262 (+8024.56%)
Mutual labels:  nearest-neighbor-search
Deep Mihash
Code for papers "Hashing with Mutual Information" (TPAMI 2019) and "Hashing with Binary Matrix Pursuit" (ECCV 2018)
Stars: ✭ 13 (-88.6%)
Mutual labels:  nearest-neighbor-search

Neighbor

Nearest neighbor search for Rails and Postgres

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'neighbor'

And run:

bundle install
rails generate neighbor:install
rails db:migrate

This enables the cube extension in Postgres

Getting Started

Create a migration

class AddNeighborVectorToItems < ActiveRecord::Migration[6.1]
  def change
    add_column :items, :neighbor_vector, :cube
  end
end

Add to your model

class Item < ApplicationRecord
  has_neighbors dimensions: 3
end

Update the vectors

item.update(neighbor_vector: [1.0, 1.2, 0.5])

With cosine distance (the default), vectors are normalized before being stored

Get the nearest neighbors to a record

item.nearest_neighbors.first(5)

Get the nearest neighbors to a vector

Item.nearest_neighbors([0.9, 1.3, 1.1]).first(5)

Distance

Specify the distance metric

class Item < ApplicationRecord
  has_neighbors dimensions: 3, distance: "euclidean"
end

Supported values are:

  • cosine (default)
  • euclidean
  • taxicab
  • chebyshev

For inner product, see this example

Records returned from nearest_neighbors will have a neighbor_distance attribute

nearest_item = item.nearest_neighbors.first
nearest_item.neighbor_distance

Dimensions

By default, Postgres limits the cube data type to 100 dimensions. See the Postgres docs for how to increase this.

Example

You can use Neighbor for online item-based recommendations with Disco. We’ll use MovieLens data for this example.

Generate a model

rails generate model Movie name:string neighbor_vector:cube
rails db:migrate

And add has_neighbors

class Movie < ApplicationRecord
  has_neighbors dimensions: 20
end

Fit the recommender

data = Disco.load_movielens
recommender = Disco::Recommender.new(factors: 20)
recommender.fit(data)

Use item factors for the neighbor vector

recommender.item_ids.each do |item_id|
  Movie.create!(name: item_id, neighbor_vector: recommender.item_factors(item_id))
end

And get similar movies

movie = Movie.find_by(name: "Star Wars (1977)")
movie.nearest_neighbors.first(5).map(&:name)

Complete code

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/neighbor.git
cd neighbor
bundle install
bundle exec rake test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].