Skip to content

Commit

Permalink
v0.3.2 (#17)
Browse files Browse the repository at this point in the history
* Add empty_attribute flag to typicality
  • Loading branch information
mikulatomas authored Feb 23, 2022
1 parent 5f92109 commit e0e186b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fcapsy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.1"
__version__ = "0.3.2"
__author__ = "Tomáš Mikula"
__email__ = "[email protected]"
__license__ = 'MIT license'
58 changes: 41 additions & 17 deletions fcapsy/typicality.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import typing
import statistics
import operator
from functools import reduce

import concepts.lattices


__all__ = ["typicality_avg, typicality_min"]
__all__ = ["typicality_avg", "typicality_min"]


def _get_vectors(concept, item):
Expand All @@ -32,70 +34,92 @@ def _get_vectors(concept, item):


def typicality_min(
item: str, concept: "concepts.lattices.Concept", similarity: typing.Callable
item: str,
concept: "concepts.lattices.Concept",
similarity: typing.Callable,
empty_attributes: bool = True,
) -> float:
"""Calculate typicality of object/attribute in given concept based on worst case (minimal) similarity.
Args:
item (str): object or attribute name
concept (concepts.lattices.Concept)
similarity (typing.Callable)
empty_attributes (bool): if empty attributes (zeros columns) should be included
Example:
>>> from concepts import Context
>>> from binsdpy.similarity import jaccard
>>> context = Context.fromstring('''
... |2 legs |nests |flies |raptor |
... sparrow | X | X | X | |
... lark | X | X | X | |
... penguin | X | | | |
... chicken | X | X | X | |
... vulture | X | | X | X |
... |2 legs |nests |flies |raptor |engine |
... sparrow | X | X | X | | |
... lark | X | X | X | | |
... penguin | X | | | | |
... chicken | X | X | X | | |
... vulture | X | | X | X | |
... ''')
>>> birds = context.lattice.supremum
>>> typicality_min('sparrow', birds, jaccard) # doctest: +NUMBER
0.33
>>> typicality_min('penguin', birds, jaccard) # doctest: +NUMBER
0.33
>>> from binsdpy.similarity import smc
>>> typicality_min('penguin', birds, smc, empty_attributes=False) # doctest: +NUMBER
0.5
"""

vectors, item_vector = _get_vectors(concept, item)
mask = None

similarities = map(lambda v: similarity(v, item_vector), vectors)
if not empty_attributes:
mask = reduce(operator.or_, vectors)

similarities = map(lambda v: similarity(v, item_vector, mask), vectors)

return min(similarities)


def typicality_avg(
item: str, concept: "concepts.lattices.Concept", similarity: typing.Callable
item: str,
concept: "concepts.lattices.Concept",
similarity: typing.Callable,
empty_attributes: bool = True,
) -> float:
"""Calculate typicality of object/attribute in given concept based on average similarity.
Args:
item (str): object or attribute name
concept (concepts.lattices.Concept)
similarity (typing.Callable)
empty_attributes (bool): if empty attributes (zeros columns) should be included
Example:
>>> from concepts import Context
>>> from binsdpy.similarity import jaccard
>>> context = Context.fromstring('''
... |2 legs |nests |flies |raptor |
... sparrow | X | X | X | |
... lark | X | X | X | |
... penguin | X | | | |
... chicken | X | X | X | |
... vulture | X | | X | X |
... |2 legs |nests |flies |raptor |engine |
... sparrow | X | X | X | | |
... lark | X | X | X | | |
... penguin | X | | | | |
... chicken | X | X | X | | |
... vulture | X | | X | X | |
... ''')
>>> birds = context.lattice.supremum
>>> typicality_avg('sparrow', birds, jaccard) # doctest: +NUMBER
0.76
>>> typicality_avg('penguin', birds, jaccard) # doctest: +NUMBER
0.46
>>> from binsdpy.similarity import smc
>>> typicality_avg('penguin', birds, smc, empty_attributes=False) # doctest: +NUMBER
0.5
"""

vectors, item_vector = _get_vectors(concept, item)
mask = None

if not empty_attributes:
mask = reduce(operator.or_, vectors)

similarities = map(lambda v: similarity(v, item_vector), vectors)
similarities = map(lambda v: similarity(v, item_vector, mask), vectors)

return statistics.mean(similarities)

0 comments on commit e0e186b

Please sign in to comment.