Coder Social home page Coder Social logo

fastjaccard's Introduction

fastJaccard

This packages its designed to run the Jaccard similarity for binary matrices in parallel using Rcpp and RcppParallel

Installation

You can install the development version of fastJaccard from GitHub with:

# install.packages("devtools")
devtools::install_github("alrobles/fastJaccard")

Example

We can create a binary matrix as example and run against a build r basic code

Implementation in R

As a baseline we implement the Jaccard distance in plain R

jaccard_distance <- function(mat) {
  
  
  intersection <- function(p,q){
   sum(ifelse(p + q == 2, 1 , 0))
  }
  union = function(p,q){
   sum(p) + sum(q) - intersection(p, q)
  }
  
  res = matrix(0, nrow(mat), nrow(mat))
  
  for (i in 1:(nrow(mat) - 1)) {
    for (j in (i+1):nrow(mat)) {
      d1 = intersection(mat[i,], mat[j,])
      d2 = union(mat[i,], mat[j,])
      res[j,i] = 1 - d1/d2
      res[i,j] = 1 - d1/d2
    }
  }
  res
}

Benchmarks

We create now a random binary matrix and run both implementations

library(fastJaccard)
## basic example code

# create a matrix
n = 1000
k = 2000
m = matrix(ifelse(runif(n*k) > 0.5, 1, 0), ncol = k)

# ensure that serial and parallel versions give the same result
r_res <- jaccard_distance(m)
rcpp_parallel_res <- fastJaccard::jaccard_fast_matrix(m)
stopifnot(all(rcpp_parallel_res - r_res < 1e-10)) ## precision differences

# compare performance
library(rbenchmark)
res <- benchmark(jaccard_distance(m),
                 jaccard_fast_matrix(m),
                 replications = 30,
                 order="relative")
res[,1:4]

jaccard for pair of vectors

We can also can get a Jaccard similarity for vectors

set.seed(1235)
x = rbinom(1e6,1,.5)
y = rbinom(1e6,1,.5)

fastJaccard::jaccard_fast(x, y)

fastjaccard's People

Contributors

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