GSP (Generalized Sequence Pattern) algorithm in Python
GSP-Py: A Python-powered library to mine sequential patterns in large datasets, based on the robust Generalized
Sequence Pattern (GSP) algorithm. Ideal for market basket analysis, temporal mining, and user journey discovery.
[!IMPORTANT]
GSP-Py is compatible with Python 3.8 and later versions!
The Generalized Sequential Pattern (GSP) algorithm is a sequential pattern mining technique based on Apriori
principles. Using support thresholds, GSP identifies frequent sequences of items in transaction datasets.
For example:
You will need Python installed on your system. On most Linux systems, you can install Python with:
sudo apt install python3
For package dependencies of GSP-Py, they will automatically be installed when using pip
.
GSP-Py can be easily installed from either the repository or PyPI.
To manually clone the repository and set up the environment:
git clone https://github.com/jacksonpradolima/gsp-py.git
cd gsp-py
Refer to the Developer Installation section and run:
rye sync
pip
Alternatively, install GSP-Py from PyPI with:
pip install gsppy
This project uses Rye for managing dependencies, running scripts, and setting up the environment. Follow these steps to install and set up Rye for this project:
Run the following command to install Rye:
curl -sSf https://rye.astral.sh/get | bash
If the ~/.rye/bin
directory is not in your PATH, add the following line to your shell configuration file (e.g., ~/.bashrc
, ~/.zshrc
, etc.):
export PATH="$HOME/.rye/bin:$PATH"
Reload your shell configuration file:
source ~/.bashrc # or `source ~/.zshrc`
To configure the project environment and install its dependencies, run:
rye sync
Once the environment is set up, you can run the following commands to simplify project tasks:
rye run test
rye run format
rye run lint
rye run typecheck
rye add <package-name>
rye add --dev <package-name>
pyproject.toml
file.requirements.txt
, as Rye manages all dependencies!The library is designed to be easy to use and integrate with your own projects. Below is an example of how you can
configure and run GSP-Py.
The input to the algorithm is a sequence of transactions, where each transaction contains a sequence of items:
transactions = [
['Bread', 'Milk'],
['Bread', 'Diaper', 'Beer', 'Eggs'],
['Milk', 'Diaper', 'Beer', 'Coke'],
['Bread', 'Milk', 'Diaper', 'Beer'],
['Bread', 'Milk', 'Diaper', 'Coke']
]
Import the GSP
class from the gsppy
package and call the search
method to find frequent patterns with a support
threshold (e.g., 0.3
):
from gsppy.gsp import GSP
# Example transactions: customer purchases
transactions = [
['Bread', 'Milk'], # Transaction 1
['Bread', 'Diaper', 'Beer', 'Eggs'], # Transaction 2
['Milk', 'Diaper', 'Beer', 'Coke'], # Transaction 3
['Bread', 'Milk', 'Diaper', 'Beer'], # Transaction 4
['Bread', 'Milk', 'Diaper', 'Coke'] # Transaction 5
]
# Set minimum support threshold (30%)
min_support = 0.3
# Find frequent patterns
result = GSP(transactions).search(min_support)
# Output the results
print(result)
The algorithm will return a list of patterns with their corresponding support.
Sample Output:
[
{('Bread',): 4, ('Milk',): 4, ('Diaper',): 4, ('Beer',): 3, ('Coke',): 2},
{('Bread', 'Milk'): 3, ('Milk', 'Diaper'): 3, ('Diaper', 'Beer'): 3},
{('Bread', 'Milk', 'Diaper'): 2, ('Milk', 'Diaper', 'Beer'): 2}
]
('Bread',): 4
means “Bread”('Bread', 'Milk'): 3
means the sequence “('Bread', 'Milk', 'Diaper'): 2
means the[!NOTE]
The support of a sequence is calculated as the fraction of transactions containing the sequence, e.g.,[Bread, Milk]
appears in 3 out of 5 transactions → Support =3 / 5 = 0.6
(60%).
This insight helps identify frequently occurring sequential patterns in datasets, such as shopping trends or user
behavior.[!TIP]
For more complex examples, find example scripts in thegsppy/tests
folder.
We are actively working to improve GSP-Py. Here are some exciting features planned for future releases:
Custom Filters for Candidate Pruning:
Support for Preprocessing and Postprocessing:
Support for Time-Constrained Pattern Mining:
Want to contribute or suggest an
improvement? Open a discussion or issue!
We welcome contributions from the community! If you’d like to help improve GSP-Py, read
our CONTRIBUTING.md guide to get started.
Development dependencies (e.g., testing and linting tools) are automatically managed using Rye. To install
these dependencies and set up the environment, run:
rye sync
After syncing, you can run the following scripts using Rye for development tasks:
rye run test
rye run lint
rye run typecheck
rye run format
git checkout -b feature/my-feature
.git commit -m "Add my feature."
git push origin feature/my-feature
.Looking for ideas? Check out our Planned Features section.
This project is licensed under the terms of the MIT License. For more details, refer to the LICENSE file.
If GSP-Py contributed to your research or project that led to a publication, we kindly ask that you cite it as follows:
@misc{pradolima_gsppy,
author = {Prado Lima, Jackson Antonio do},
title = {{GSP-Py - Generalized Sequence Pattern algorithm in Python}},
month = Dec,
year = 2025,
doi = {10.5281/zenodo.3333987},
url = {https://doi.org/10.5281/zenodo.3333987}
}