Skip to main content

Tests cbrlib - main branch PyPi

Case Based Reasoning the Pythonic Way

Case Based Reasoning library for Python

This library provides the possibility to integrate with native python. Basic implemetation can use instances of Python classes to calculate similarity between those objects.

You can find a sparse description of case base reasoning at the english wikipedia page. Please also take a look at the examples.

import dataclasses
import functools
from typing import Optional

from cbrlib import (
    Evaluator,
    FunctionCalculationParameter,
    NumericEvaluationOptions,
    ReasoningRequest,
    WeightedPropertyEvaluatorMapping,
)
from cbrlib import casebase, evaluate


@dataclasses.dataclass(slots=True, frozen=True)
class DataObject:
    color: Optional[str] = None
    shape: Optional[str] = None
    size: Optional[int] = None


color_lookup = {
    "red": {"red": 1, "orange": 0.8, "yellow": 0.4},
    "orange": {"orange": 1, "red": 0.8, "yellow": 0.8},
}
color_evaluator = functools.partial(evaluate.table_lookup, color_lookup)


def create_size_evaluator() -> Evaluator:
    options = NumericEvaluationOptions(
        min_=0,
        max_=100,
        if_less=FunctionCalculationParameter(tolerance=1.0),
        if_more=FunctionCalculationParameter(tolerance=1.0),
    )
    return functools.partial(evaluate.numeric, options)


def dataobject_evaluator() -> Evaluator:
    mappings = (
        WeightedPropertyEvaluatorMapping("color", color_evaluator, 2),
        WeightedPropertyEvaluatorMapping("size", create_size_evaluator(), 1),
        WeightedPropertyEvaluatorMapping("shape", evaluate.equality, 1),
    )
    return functools.partial(evaluate.case_average, mappings)


def main() -> None:
    data = [
        DataObject(color="red", shape="triangle", size=20),
        DataObject(color="orange", shape="circle", size=70),
        DataObject(color="green", shape="square", size=50),
    ]
    print("-" * 80)
    print(
        casebase.infer(
            data,
            ReasoningRequest(
                query=DataObject(color="red"),
            ),
            dataobject_evaluator(),
        )
    )
    print("-" * 80)
    print(
        casebase.infer(
            data,
            ReasoningRequest(
                query=DataObject(size=50),
            ),
            dataobject_evaluator(),
        )
    )

A big thanky you to myCBR for the example data.

Metadata

Release files for cbrlib 2.0.10

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

Source distribution (sdist)

Source distribution for cbrlib 2.0.10
File Size Uploaded
cbrlib-2.0.10.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cbrlib 2.0.10
File Interpreter ABI Platform
cbrlib-2.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / cbrlib-2.0.10.tar.gz

Download URL cbrlib-2.0.10.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b671ef52d887e1321288a718e8ac08dffca6c2cfb3ab467880252326279469ea
BLAKE2b-256 checksum
How to use checksums
cc32d00baf275a6fdd4a95ec30d21a41b13ba2251dee0b8bc8686a3641d4735b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.13 Linux/6.8.1-arch1-1

Release files / cbrlib-2.0.10-py3-none-any.whl

Download URL cbrlib-2.0.10-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46e74900aef5ebe3c11d0beacd4209176b034428c6cec212bd749d5c3bc38857
BLAKE2b-256 checksum
How to use checksums
68f3b70b418748504d45df4a0ede630319b044a6249984c1e0e819bdc9c62c99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.13 Linux/6.8.1-arch1-1

Release history Release notifications | RSS feed

This release

2.0.10 This release

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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