GithubHelp home page GithubHelp logo

isabella232 / seriate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from src-d/seriate

0.0 0.0 0.0 2.4 MB

Optimal ordering of elements in a set given their distance matrix.

License: Other

Python 100.00%

seriate's Introduction

seriate

Optimal ordering of elements in a set given their distance matrix.

Travis build status Code coverage PyPi package status stability: stable Apache 2.0 license

example

OverviewHow To UseContributionsLicense

Overview

This is a Python implementation of Seriation algorithm. Seriation is an approach for ordering elements in a set so that the sum of the sequential pairwise distances is minimal. We state this task as a Travelling Salesman Problem (TSP) and leverage the powerful Google's or-tools to do heavy-lifting. Since TSP is NP-hard, it is not possible to calculate the precise solution for a big number of elements. However, the or-tools' heuristics work very well in practice, and they are used in e.g. Google Maps.

Any numpy.roll-ed result is equivalent.

How To Use

import numpy
from scipy.spatial.distance import pdist
from seriate import seriate

elements = numpy.array([
    [3, 3, 3],
    [5, 5, 5],
    [4, 4, 4],
    [2, 2, 2],
    [1, 1, 1]
])

print(seriate(pdist(elements)))

# Output: [4, 3, 0, 2, 1]

The example above shows how we order 5 elements: [3, 3, 3], [5, 5, 5], [4, 4, 4], [2, 2, 2] and [1, 1, 1]. The result is expected:

  1. [1, 1, 1]
  2. [2, 2, 2]
  3. [3, 3, 3]
  4. [4, 4, 4]
  5. [5, 5, 5]

pdist from scipy.spatial.distance uses Euclidean (L2) dstance metric by default, so the distance between [x, x, x] and [x + 1, x + 1, x + 1] is constant: √3. Any other distance is bigger, so the optimal ordering is to list our elements in the increasing norm order.

Contributions

Contributions are very welcome and desired! Please follow the code of conduct and read the contribution guidelines.

License

Apache-2.0, see LICENSE.md.

seriate's People

Contributors

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