Git Product home page Git Product logo

swiftsim's Introduction

SwiftSim

Vector similarity utility in Swift. Currently, only arrays of Double are supported as input. In the next update, this will be changed to be more generic. The hope is that the numerical methods can operate on any arbitrary numerical input, and the set-based metrics can operate on any Hashable type. Future versions may have a more functional object Class called SwiftSwim. Future versions will also support arbitrary objects to be used and their data paths configurable (i.e., use objects). Here, the input vectors will be lazily constructed via closures. Another feature will be to pass in arrays of objects or arrays to find the top or bottom k entries according to some specified element.

Basic Usage

let Similarity = EMSimilarity()
let A = [0.0, 1.5, 3.0, 4.5, 6.0]
let B = [2.0, 4.0, 6.0, 8.0, 10.0]

// compute the cosine similarity of A and B
Similarity.compute(A, B: B)
// 0.984731927834662

Computation Modes

SwiftSim uses a stack to store its modes. To specify a new mode, you simply push a new computation mode to the stack. You can also pop modes off.

enum EMSimilarityMode {
    case Cosine
    case Tanimoto
    case Ochiai
    case JaccardIndex
    case JaccardDistance
    case Dice
    case Hamming
}
// push a new computation mode to the stack
Similarity.pushSimMode(.Hamming)
// compute the Hamming distance of A and B
Similarity.compute(A, B: B)
// 5.0
// go back to previous mode
Similarity.popSimMode()
Similarity.compute(A, B: B)
// 0.984731927834662

Vector Size Mismatch Mode

By default, if A and B have different lengths, and the mode is Cosine, Tanimoto, or Hamming, it will trigger a bail and return -1.0. However, you can specify a different mismatch mode like you do similarity mode.

enum EMVectorSizeMismatchMode {
    case Bail
    case Truncate
}

// truncate the larger vector
Similarity.pushMismatchMode(.Truncate)

Other Bail Conditions

Currently, another condition that will cause -1.0 to be returned is if both input arrays are empty and the similarity mode is not Hamming, or one of them is empty and the current mode is Cosine, Tanimoto, or Ochiai. This is to mitigate a divide by zero error, or to adhere to metric prequisites.

swiftsim's People

Contributors

evanmoss avatar gemmakbarlow avatar koishi 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.