-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e43ebc0
commit a3afa72
Showing
14 changed files
with
458 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
__version__ = "0.3.3" | ||
__author__ = "Tomáš Mikula" | ||
__email__ = "mail@tomasmikula.cz" | ||
__license__ = "MIT license" | ||
from .category import Category |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import typing | ||
import concepts | ||
|
||
from .utils import get_vectors | ||
|
||
|
||
class Category: | ||
"""Category defined as group of exemplars (extent).""" | ||
|
||
def __init__(self, context: "concepts.Context", extent) -> None: | ||
self.context = context | ||
self._extent = extent | ||
|
||
@property | ||
def extent(self) -> typing.Tuple[str, ...]: | ||
return self._extent.members() | ||
|
||
def vectors(self, item: str) -> typing.Dict: | ||
return get_vectors(self, item) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import typing | ||
|
||
import concepts.lattices | ||
import fcapsy.category | ||
|
||
from .utils import get_context | ||
|
||
|
||
__all__ = ["presence", "absence", "characteristic", "characteristic_r"] | ||
|
||
|
||
def _get_vectors(item, concept): | ||
context = get_context(concept) | ||
|
||
try: | ||
instances_with_item = context.intension([item], raw=True) | ||
Vector = type(concept._intent) | ||
universum = Vector.supremum | ||
concept_core = concept._intent | ||
except KeyError: | ||
instances_with_item = context.extension([item], raw=True) | ||
Vector = type(concept._extent) | ||
universum = Vector.supremum | ||
concept_core = concept._extent | ||
|
||
return instances_with_item, concept_core, Vector, universum | ||
|
||
|
||
def presence( | ||
item, concept: typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"] | ||
): | ||
"""_summary_ | ||
Args: | ||
item (str): object or attribute name | ||
concept (typing.Union[concepts.lattices.Concept, fcapsy.category.Category]) | ||
Returns: | ||
_type_: _description_ | ||
Example: | ||
>>> from concepts import Context | ||
>>> 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 | | ||
... ''') | ||
>>> birds = context.lattice.supremum | ||
>>> presence("2 legs", birds) | ||
5 | ||
>>> presence("nests", birds) | ||
3 | ||
>>> flies = birds.lower_neighbors[0] | ||
>>> presence("2 legs", flies) | ||
4 | ||
""" | ||
instances_with_item, concept_core, Vector, _ = _get_vectors(item, concept) | ||
|
||
occurence_in_concept = Vector.fromint(instances_with_item & concept_core).count() | ||
|
||
return occurence_in_concept | ||
|
||
|
||
def absence( | ||
item, concept: typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"] | ||
): | ||
"""_summary_ | ||
Args: | ||
item (str): object or attribute name | ||
concept (typing.Union[concepts.lattices.Concept, fcapsy.category.Category]) | ||
Returns: | ||
_type_: _description_ | ||
Example: | ||
>>> from concepts import Context | ||
>>> 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 | | ||
... ''') | ||
>>> birds = context.lattice.supremum | ||
>>> absence("2 legs", birds) | ||
0 | ||
>>> absence("nests", birds) | ||
2 | ||
>>> flies = birds.lower_neighbors[0] | ||
>>> absence("raptor", flies) | ||
3 | ||
""" | ||
instances_with_item, concept_core, Vector, universum = _get_vectors(item, concept) | ||
|
||
absence_in_concept = Vector.fromint( | ||
(universum ^ instances_with_item) & concept_core | ||
).count() | ||
|
||
return absence_in_concept | ||
|
||
|
||
def characteristic( | ||
item: str, | ||
concept: typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"], | ||
) -> float: | ||
"""Calculates characteristic of object/attribute in the given concept. | ||
The object/attribute does not has to be from the extent/intent. | ||
Args: | ||
item (str): object or attribute name | ||
concept (typing.Union[concepts.lattices.Concept, fcapsy.category.Category]) | ||
Returns: | ||
float: characteristic | ||
Example: | ||
>>> from concepts import Context | ||
>>> 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 | | ||
... ''') | ||
>>> birds = context.lattice.supremum | ||
>>> birds.extent | ||
('sparrow', 'lark', 'penguin', 'chicken', 'vulture') | ||
>>> characteristic("raptor", birds) | ||
0.2 | ||
>>> characteristic("flies", birds) | ||
0.8 | ||
>>> characteristic("2 legs", birds) | ||
1.0 | ||
""" | ||
instances_with_item, concept_core, Vector, _ = _get_vectors(item, concept) | ||
|
||
occurence_in_concept = Vector.fromint(instances_with_item & concept_core).count() | ||
|
||
if not instances_with_item.count(): | ||
return 0.0 | ||
|
||
return (occurence_in_concept / instances_with_item.count()) * ( | ||
occurence_in_concept / concept_core.count() | ||
) | ||
|
||
|
||
def characteristic_r( | ||
item: str, | ||
concept: typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"], | ||
) -> float: | ||
instances_with_item, concept_core, Vector, _ = _get_vectors(item, concept) | ||
|
||
occurence_in_concept = Vector.fromint(instances_with_item & concept_core).count() | ||
|
||
if not instances_with_item.count(): | ||
return 0.0 | ||
|
||
universum = Vector.supremum | ||
universum_minus_core = Vector.fromint(universum & (universum ^ concept_core)) | ||
universum_minus_core_minus_instances_with_item = Vector.fromint( | ||
universum_minus_core & (universum ^ instances_with_item) | ||
) | ||
|
||
return (occurence_in_concept / concept_core.count()) * ( | ||
universum_minus_core_minus_instances_with_item.count() | ||
/ universum_minus_core.count() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import typing | ||
|
||
import concepts.lattices | ||
import fcapsy.category | ||
import numpy as np | ||
|
||
from .utils import get_vectors | ||
|
||
|
||
def prototype( | ||
concept: typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"], | ||
) -> np.ndarray: | ||
"""Calcualtes prototype of given concept. | ||
Args: | ||
concept (typing.Union["concepts.lattices.Concept", "fcapsy.category.Category"]): concept | ||
Returns: | ||
np.ndarray: prototype | ||
Example: | ||
>>> from concepts import Context | ||
>>> from binsdpy.similarity import jaccard | ||
>>> context = Context.fromstring(''' | ||
... |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 | ||
>>> prototype(birds) | ||
array([1. , 0.6, 0.8, 0.2, 0. ]) | ||
""" | ||
vectors = get_vectors(concept, concept.extent[0]).values() | ||
|
||
return np.array([a.bools() for a in vectors]).astype(int).mean(axis=0) |
Oops, something went wrong.