Skip to main content

Pycspade

What is this?

This is a python wrapper for the C++ implementation of C-SPADE algorithm by the author, Mohammed J. Zaki Original code was downloaded from http://www.cs.rpi.edu/~zaki/www-new/pmwiki.php/Software/Software#toc11 Since this is just a wrapper it is as fast as the C++ code

How to install?

Compatible with Python 2 and 3. On Windows, Visual Studio 2015 Build Tools is also required.

pip install Cython pycspade

How to use?

Your data needs to be in a particular format similar to the following:

1 1 3 8 37 42
1 2 4 4 11 37 42
2 1 2 10 73
2 2 1 72
2 3 3 4 24 77
...

The first number is the sequence index, the second is the event index, the third is the number of elements, followed by the element, space separated

Let's call this file data.txt. You will call cspade as following:

from pycspade.helpers import spade, print_result

# To get raw SPADE output
result = spade(filename='tests/zaki.txt', support=0.3, parse=False)
print(result['mined'])
1 -- 4 4 
2 -- 4 4 
4 -- 2 2 
6 -- 4 4 
4 -> 6 -- 2 2 
4 -> 2 -- 2 2 
2 -> 1 -- 2 2 
4 -> 1 -- 2 2 
6 -> 1 -- 2 2 
4 -> 6 -> 1 -- 2 2 
4 -> 2 -> 1 -- 2 2
print(result['logger'])
CONF 4 9 2.7 2.5
args.MINSUPPORT 2 4
MINMAX 1 4
1 SUPP 4
2 SUPP 4
4 SUPP 2
6 SUPP 4
numfreq 4 :   SUMSUP SUMDIFF = 0 0
EXTRARYSZ 2465792
OPENED /tmp/cspade-WWv9bQWBYdDyH85T.idx
OFF 9 38
Wrote Offt 
BOUNDS 1 5
WROTE INVERT 
Cleaned up successful: /tmp/cspade-WWv9bQWBYdDyH85T.tpose
Cleaned up successful: /tmp/cspade-WWv9bQWBYdDyH85T.idx
Cleaned up successful: /tmp/cspade-WWv9bQWBYdDyH85T.data
Cleaned up successful: /tmp/cspade-WWv9bQWBYdDyH85T.conf
print(result['summary'])
CONF 4 9 2.5 2.7 10 1 4 0.781025 4
TPOSE SEQ NOF2 /tmp/cspade-WWv9bQWBYdDyH85T.data 0.3 4 2 1 
F1stats = [ 4 0 0 ]
SPADE /tmp/cspade-WWv9bQWBYdDyH85T.tpose 0.3 2 7 0 0 0 0 0 -1 1 100 100 4 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# To also get other sequence mining's measures, incl. lift, support, confidence:
result = spade(filename='tests/zaki.txt', support=0.3, parse=True)
# Pretty print result:
print_result(result)
   Occurs     Accum   Support    Confid      Lift          Sequence
        4        14 1.0000000       N/A       N/A               (1) 
        4         6 1.0000000       N/A       N/A               (2) 
        2         4 0.5000000 0.5000000 0.5000000          (2)->(1) 
        2         2 0.5000000       N/A       N/A               (4) 
        2         2 0.5000000 1.0000000 1.0000000          (4)->(1) 
        2         2 0.5000000 1.0000000 1.0000000          (4)->(2) 
        2         2 0.5000000 1.0000000 1.0000000     (4)->(2)->(1) 
        2         2 0.5000000 1.0000000 1.0000000          (4)->(6) 
        2         2 0.5000000 1.0000000 1.0000000     (4)->(6)->(1) 
        4         6 1.0000000       N/A       N/A               (6) 
        2         4 0.5000000 0.5000000 0.5000000          (6)->(1) 
You can provide cspade with list of sequences instead of a file:
data = [
    [1, 10, [3, 4]],
    [1, 15, [1, 2, 3]],
    [1, 20, [1, 2, 6]],
    [1, 25, [1, 3, 4, 6]],
    [2, 15, [1, 2, 6]],
    [2, 20, [5]],
    [3, 10, [1, 2, 6]],
    [4, 10, [4, 7, 8]],
    [4, 20, [2, 6]],
    [4, 25, [1, 7, 8]]
]

result = spade(data=data, support=0.01)
print_result(result)

The result seq is a string, that have multiple rows and looks like this:

22 80 -> 72 -> 42 -> 22 -- 2 2
22 -> 45 71 -> 42 -- 1 1
80 -> 45 71 -> 42 -- 1 1
22 80 -> 45 71 -> 42 -- 1 1

Let's decipher the first row:

22 80 -> 72 -> 42 -> 22 -- 2 2

It gives you the frequent sequence followed by support (the last two numbers, which will be the same in this application). The row reads: the itemset (22 80) is followed by (72) followed by (42) followed by (22).

There're a lot of parameters that can be passed to this function. most important ones are:

  • support: this is the minimum support level, default to 0 (not excluding anything)
  • max_gap: The max number of itemset that can be skipped in a sequence
  • min_gap: The min number of itemset that must be skipped in a sequence

Read the original paper and the C++ implementation for more details

How to contribute?

  • Fork this repo
  • Make change
  • Pull request

How to recompile to use in IDE?

  • rm cspade.cpp; python setup.py build_ext --inplace

Licence

  • MIT

Metadata

Release files for pycspade 0.6.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycspade 0.6.6
File Size Uploaded Provenance Actions
pycspade-0.6.6.tar.gz 91.8 kB Without provenance Details for pycspade-0.6.6.tar.gz

Release files / pycspade-0.6.6.tar.gz

Download URL pycspade-0.6.6.tar.gz
Size 91.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a03f294db07a468626afa4fb7349440e6c13b5c4be5183dc4043e210c465d484
BLAKE2b-256 checksum
How to use checksums
ce1e944df80df772dd1c36fe2f7f8312fd0c27a7453b62ff35d2079cb0495d08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.5

Security for pycspade 0.6.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page