Merge and intersect sorted numpy arrays.
Project description
Numpy and Numpy arrays are a really great tool. However, intersecting and merging multiple sorted numpy arrays is rather less performant. The current numpy implementation concatenates the two arrays and sorts the combination. If you want to merge or intersect multiple numpy arrays, there is a much faster way, by using the property, that the resulting array is sorted.
Sortednp (sorted numpy) operates on sorted numpy arrays to calculate the intersection or the union of two numpy arrays in an efficient way. The resulting array is again a sorted numpy array, which can be merged or intersected with the next array. The intended use case is that sorted numpy arrays are sorted as the basic data structure and merged or intersected at request. Typical applications include information retrieval and search engines in particular.
It is also possible to implement a k-way merging or intersecting algorithm,
which operates on an arbitrary number of arrays at the same time. This package
is intended to deal with arrays with $10^6
$ or $10^{10}
$ items. Usually, these
arrays are too large to keep more than two of them in memory at the same
time. This package implements methods to merge and intersect multiple arrays,
which can be loaded on-demand.
Installation
There are two different methods to install sortednp
.
Using pip
(recommended)
You can install the package directly from PyPI using pip
(here pip3
). There are
pre-compiled wheels for linux
32- and 64bit.
$ pip3 install sortednp
Using setuptools
Alternatively, you can clone the git repository and run the setup script.
$ git clone https://gitlab.sauerburger.com/frank/sortednp.git
$ cd sortednp
$ python3 setup.py install
Numpy Dependency
The installation fails in some cases, because of a build-time dependency on
numpy. Usually, the problem can be solved by manually installing a recent numpy
version via pip3 install -U numpy
.
Usage
The package provides two different kinds of methods. The first class is intended to operate on two arrays. The second class operates on two or more arrays and calls the first class of methods internally.
Two-way methods
Two numpy sorted arrays can be merged with the merge
method, which takes two
numpy arrays and returns the sorted union of the two arrays.
# merge.py
import numpy as np
import sortednp as snp
a = np.array([0, 3, 4, 6, 7])
b = np.array([1, 2, 3, 5, 7, 9])
m = snp.merge(a, b)
print(m)
If you run this, you should see the union of both arrays as a sorted numpy array.
$ python3 merge.py
[0 1 2 3 3 4 5 6 7 7 9]
Two sorted numpy arrays can be intersected with the intersect
method, which takes two
numpy arrays and returns the sorted intersection of the two arrays.
# intersect.py
import numpy as np
import sortednp as snp
a = np.array([0, 3, 4, 6, 7])
b = np.array([1, 2, 3, 5, 7, 9])
i = snp.intersect(a, b)
print(i)
If you run this, you should see the intersection of both arrays as a sorted numpy array.
$ python3 intersect.py
[3 7]
Returning array indices
The intersect
method takes an optional argument indices
which is False
by default. If this is set to True
, the return value consists of the
intersection array and a tuple with the indices of the common values for both
arrays.
# intersect_indices.py
import numpy as np
import sortednp as snp
a = np.array([2,4,6,8,10])
b = np.array([1,2,3,4])
intersection, indices = snp.intersect(a,b, indices=True)
print(intersection)
print(indices)
The above example gives:
$ python3 intersect_indices.py
[2 4]
(array([0, 1]), array([1, 3]))
The first line shows the intersection of the two arrays. The second line
prints a tuple with the indices where the common values appeared in the input
arrays. For example, the value 4
is at position 1
in array a
and at position
3
in array b
.
k-way methods
Similarly, the k-way intersect and merge methods take two or more arrays and perform the merge or intersect operation on its arguments.
# kway_intersect.py
import numpy as np
import sortednp as snp
a = np.array([0, 3, 4, 6, 7])
b = np.array([0, 3, 5, 7, 9])
c = np.array([1, 2, 3, 5, 7, 9])
d = np.array([2, 3, 6, 7, 8])
i = snp.kway_intersect(a, b, c, d)
print(i)
If you run this, you should see the intersection of all four arrays as a sorted numpy array.
$ python3 kway_intersect.py
[3 7]
The k-way merger sortednp.kway_merge
works analogously. However, the native
numpy
implementation is faster compared to the merge provided by this package.
The k-way merger has been added for completeness. The package heapq
provides
efficient methods to merge multiple arrays simultaneously.
The methods kway_merge
and kway_intersect
accept the optional keyword
argument assume_sorted
. By default, it is set to True
. If it is set to False
,
the method calls sort()
on the input arrays before performing the operation.
The default should be kept if the arrays are already sorted to save the time it
takes to sort the arrays.
Since the arrays might be too large to keep all of them in memory at the same
time, it is possible to pass a callable
instead of an array to the methods.
The callable
is expected to return the actual array. It is called immediately
before the array is required. This reduces the memory consumption.
Algorithms
Intersections are calculated by iterating both arrays. For a given element in
one array, the method needs to search the other and check if the element is
contained. In order to make this more efficient, we can use the fact that the
arrays are sorted. There are three search methods, which can be selected via the
optional keyword argument algorithm
.
sortednp.SIMPLE_SEARCH
: Search for an element by linearly iterating over the array element-by-element. More Information.sortednp.BINARY_SEARCH
: Slice the remainder of the array in halves and repeat the procedure on the slice which contains the searched element. More Information.sortednp.GALLOPING_SEARCH
: First, search for an element linearly, doubling the step size after each step. If a step goes beyond the search element, perform a binary search between the last two positions. More Information.
The default is sortednp.GALLOPING_SEARCH
. The performance of all three
algorithms is compared in the next section.
Performance
The performance of the package can be compared with the default implementation of numpy, the intersect1d` method. The ratio of the execution time between sortednp and numpy is shown for various different benchmark tests.
The merge or intersect time can be estimated under two different assumptions. If the arrays, which are merged or intersected, are already sorted, one should not consider the time it takes to sort the random arrays in the benchmark. On the other hand, if one considers a scenario in which the arrays are not sorted, one should take the sorting time into account.
Intersect
The performance of the intersection operation depends on the sparseness of the two arrays. For example, if the first element of one of the arrays is larger than all elements in the other array, only the other array has to be searched (linearly, binarily, or exponentially). Similarly, if the common elements are far apart in the arrays (sparseness), large chunks of the arrays can be skipped. The arrays in the benchmark contain random (unique) integers. The sparseness is defined as the average difference between two consecutive elements in one array.
The first set of tests studies the performance dependence on the size of the arrays. The second set of tests studies the dependence on the sparseness of the array.
Assume sorted arrays
The following table summarizes the performance compared to numpy if one ignores the time it takes to sort the initial arrays.
Test | Simple Search | Binary Search | Galloping Search |
---|---|---|---|
Intersect | |||
Intersect Sparseness |
Include sorting time
The following table summarizes the performance compared to numpy if one takes the time it takes to sort the initial arrays into account.
Test | Simple Search | Binary Search | Galloping Search |
---|---|---|---|
Intersect | |||
Intersect Sparseness |
Merge
Assume sorted | Include sorting time |
---|---|
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for sortednp-0.2.1rc1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b8e303ae1b864bceed1ca6a2345f8aab7b5eb40d94127c8d42638dae657393e |
|
MD5 | e599aae036a6bbdfc5a67026ac2025e8 |
|
BLAKE2b-256 | 0360946fa07c33324a086f8a5b57ab818f6fb79e2d9451beec4dc9dc2ea25552 |
Hashes for sortednp-0.2.1rc1-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e684266b0ea749b11d743a86adc75044f8f318a29d2b574397839b5a919a73a0 |
|
MD5 | b1141157f0b738542c68d25fb681a7e9 |
|
BLAKE2b-256 | 60e0b06d05737ea222332b903531f4651d7860d01ceced0d748229d88a7b9dd9 |
Hashes for sortednp-0.2.1rc1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dcfc242f2b984a6689a03dcb9e61d8457a258f3e9edcc000ec4c4d9f5b3e6bf |
|
MD5 | a47d7e6808f2fcdc4bb9e04ab076fb1a |
|
BLAKE2b-256 | 5b7ac44c250995feef9db01f824efa13acb26ba9d4239aad64055aa9cf7bbbf6 |
Hashes for sortednp-0.2.1rc1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d5feb0f7a5f14d5995a6b65ac087f7ecf2c42b7c84bf40e1988a940d7b5e887 |
|
MD5 | c61cd560dcab4875e7624656e52717ed |
|
BLAKE2b-256 | 95c16accdc7091aac8d0cae5f113f220ca269d4934ac1abad5f2d23f223ccc69 |
Hashes for sortednp-0.2.1rc1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81c60617a50775d101a53587fb2d954dbcf0deab8c8306e42cabe601d14918b2 |
|
MD5 | 92e64b922b0243d7e07eb235268a30f8 |
|
BLAKE2b-256 | eb5ff66926c79d6f6428f3f84c92d07c8e647adfe5bc3bb598ccf2e6a631b016 |
Hashes for sortednp-0.2.1rc1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44211767e9decc9a36b5a5cdc6f7a8c694c90ac820218d491c13b55f81271180 |
|
MD5 | 1121795195a3ffe974d2f111c03690c3 |
|
BLAKE2b-256 | 510d2ca6864d5be6754a5c15c9fcb34d5ba2aa44c7ad51eaa01ee6396d8faf4d |
Hashes for sortednp-0.2.1rc1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d22f8cc05628abc0eee4684f6e8633ad097e45448eb642ada6179382ca4704a2 |
|
MD5 | ec7cde9a480023d8eccb5121e75c047e |
|
BLAKE2b-256 | 168f1181ef74a5d99f9f726b2b7ecfd1ac0c241d00042a88b535e08aa3e53b46 |
Hashes for sortednp-0.2.1rc1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6063dda7dd18a0cfc2fd2d1b02ab93dca01abb453d88ead39433365d1f34d88 |
|
MD5 | 4e7811c91b50be118ebec7217c75ba9b |
|
BLAKE2b-256 | 4c4f9595be0f95feddca01d12cefaeee841f465df4bc35abfaf2bedaa8339da1 |