Skip to main content

A tool to display categories and subcategories based on hand-crafted predicates

Project description

Simple categorisation

Define categories and sub-categories with predicates, and categorise lists of items based on these categories:

cat = Categoriser()
# The category of items that are < 0.5
cat.add("x < 0.5", lambda x: x < 0.5)
# Items over 1
over_1 = cat.add("x >= 1", lambda x: x >= 1)
# Items between 1.1 and 1.2, as a subcategory of items over 1
over_1.add("1.2 > x >= 1.1", lambda x: 1.2 > x >= 1.1)
over_1.add("x > 1.2", lambda x: x >= 1.2)

result = cat.categorise_list([0.01, 0.6, 1.001, 1.05, 1.1, 1.2, 3])
summarised = result.summarise()
print(summarised)

This should show something along the lines of:

{
    "categories": [
        {"category_name": "x < 0.5", "matches": 1},
        {
            "category_name": "x >= 1", "matches": 5,
            "categories": [
                {"category_name": "1.2 > x >= 1.1", "matches": 1},
                {"category_name": "x > 1.2", "matches": 2},
                {"unmatched_items": 2}
            ]
        },
        {"unmatched_items": 1}
    ]
}

It is possible to obtain a Sankey diagram with plotly for the same example.

First, install plotly

pip install plotly

Then:

parameters = result.plotly_sankey(top_label="Top")

import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(parameters)])
fig.update_layout(title_text="Example Sankey Diagram", font_size=20)
fig.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-categorisation-0.2.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

simple_categorisation-0.2-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page