Ejemplo n.º 1
0
def test_normal():
    """
    Test for normal data.
    """
    test_data = RelationRecord(frozenset(['A', 'B']), 0.5, [
        OrderedStatistic(frozenset(), frozenset(['B']), 0.8, 1.2),
        OrderedStatistic(frozenset(['A']), frozenset(), 0.8, 1.2),
        OrderedStatistic(frozenset(['A']), frozenset(['B']), 0.8, 1.2),
    ])
    output_file = StringIO()
    dump_as_two_item_tsv(test_data, output_file)

    output_file.seek(0)
    result = output_file.read()
    eq_(result, 'A\tB\t0.50000000\t0.80000000\t1.20000000' + linesep)
Ejemplo n.º 2
0
def test_normal():
    """
    Test for normal data.
    """
    transaction_manager = Mock(spec=TransactionManager)
    min_support = 0.1
    min_confidence = 0.1
    min_lift = 0.5
    max_length = 2
    support_record = SupportRecord(frozenset(['A', 'B']), 0.5)
    ordered_statistic1 = OrderedStatistic(frozenset(['A']), frozenset(['B']),
                                          0.1, 0.7)
    ordered_statistic2 = OrderedStatistic(frozenset(['A']), frozenset(['B']),
                                          0.3, 0.5)

    def gen_support_records(*args, **kwargs):
        """ Mock for apyori.gen_support_records. """
        eq_(args[1], min_support)
        eq_(kwargs['max_length'], max_length)
        yield support_record

    def gen_ordered_statistics(*_):
        """ Mock for apyori.gen_ordered_statistics. """
        yield ordered_statistic1
        yield ordered_statistic2

    def filter_ordered_statistics(*args, **kwargs):
        """ Mock for apyori.gen_ordered_statistics. """
        eq_(kwargs['min_confidence'], min_confidence)
        eq_(kwargs['min_lift'], min_lift)
        eq_(len(list(args[0])), 2)
        yield ordered_statistic1

    result = list(
        apriori(
            transaction_manager,
            min_support=min_support,
            min_confidence=min_confidence,
            min_lift=min_lift,
            max_length=max_length,
            _gen_support_records=gen_support_records,
            _gen_ordered_statistics=gen_ordered_statistics,
            _filter_ordered_statistics=filter_ordered_statistics,
        ))
    eq_(result, [
        RelationRecord(support_record.items, support_record.support,
                       [ordered_statistic1])
    ])
Ejemplo n.º 3
0
def test_filtered():
    """
    Test for filtered data.
    """
    transaction_manager = Mock(spec=TransactionManager)
    dummy_return = OrderedStatistic(frozenset(['A']), frozenset(['B']), 0.1,
                                    0.7)

    def gen_support_records(*args, **kwargs):  # pylint: disable=unused-argument
        """ Mock for apyori.gen_support_records. """
        yield dummy_return

    def gen_ordered_statistics(*_):
        """ Mock for apyori.gen_ordered_statistics. """
        yield dummy_return

    def filter_ordered_statistics(*args, **kwargs):  # pylint: disable=unused-argument
        """ Mock for apyori.gen_ordered_statistics. """
        return iter([])

    result = list(
        apriori(
            transaction_manager,
            _gen_support_records=gen_support_records,
            _gen_ordered_statistics=gen_ordered_statistics,
            _filter_ordered_statistics=filter_ordered_statistics,
        ))
    eq_(result, [])
Ejemplo n.º 4
0
def test_bad():
    """
    Test for bad data.
    """
    test_data = RelationRecord(
        set(['A']), 0.5,
        [OrderedStatistic(frozenset([]), frozenset(['A']), 0.8, 1.2)])
    output_file = StringIO()
    dump_as_json(test_data, output_file)
Ejemplo n.º 5
0
def test_normal():
    """
    Test for normal data.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.calc_support.side_effect = lambda key: {
        frozenset(['A']): 0.8,
        frozenset(['B']): 0.4,
        frozenset(['C']): 0.2,
        frozenset(['A', 'B']): 0.2,
        frozenset(['A', 'C']): 0.1,
        frozenset(['B', 'C']): 0.01,
    }.get(key, 0.0)

    test_data = SupportRecord(frozenset(['A', 'B', 'C']), 0.001)
    results = list(gen_ordered_statistics(transaction_manager, test_data))
    eq_(results, [
        OrderedStatistic(
            frozenset(['A', 'B']),
            frozenset(['C']),
            0.001 / 0.2,
            0.001 / 0.2 / 0.2,
        ),
        OrderedStatistic(
            frozenset(['A', 'C']),
            frozenset(['B']),
            0.001 / 0.1,
            0.001 / 0.1 / 0.4,
        ),
        OrderedStatistic(
            frozenset(['B', 'C']),
            frozenset(['A']),
            0.001 / 0.01,
            0.001 / 0.01 / 0.8,
        ),
    ])
Ejemplo n.º 6
0
def test_normal():
    """
    Test for normal data.
    """
    test_data = RelationRecord(
        frozenset(['A']), 0.5,
        [OrderedStatistic(frozenset([]), frozenset(['A']), 0.8, 1.2)])
    output_file = StringIO()
    dump_as_json(test_data, output_file)

    output_file.seek(0)
    result = json.loads(output_file.read())
    eq_(
        result, {
            'items': ['A'],
            'support':
            0.5,
            'ordered_statistics': [{
                'items_base': [],
                'items_add': ["A"],
                'confidence': 0.8,
                'lift': 1.2
            }]
        })
"""
Tests for apyori.filter_ordered_statistics.
"""

from nose.tools import eq_

from apyori import OrderedStatistic
from apyori import filter_ordered_statistics


TEST_DATA = [
    OrderedStatistic(frozenset(['A']), frozenset(['B']), 0.1, 0.7),
    OrderedStatistic(frozenset(['A']), frozenset(['B']), 0.3, 0.5),
]


def test_normal():
    """
    Test for normal data.
    """
    result = list(filter_ordered_statistics(
        TEST_DATA, min_confidence=0.1, min_lift=0.5))
    eq_(result, TEST_DATA)


def test_min_confidence():
    """
    Filter by minimum confidence.
    """
    result = list(filter_ordered_statistics(
        TEST_DATA, min_confidence=0.2, min_lift=0.1))