fcapy.context.FormalContext
- class fcapy.context.FormalContext(data: ~typing.Optional[~typing.Collection[bool]] = None, object_names=None, attribute_names=None, description: ~typing.Optional[str] = None, target=None, backend: {'BinTableLists': <class 'fcapy.context.bintable.BinTableLists'>, 'BinTableNumpy': <class 'fcapy.context.bintable.BinTableNumpy'>, 'BinTableBitarray': <class 'fcapy.context.bintable.BinTableBitarray'>} = 'auto')
A class used to represent Formal Context object from FCA theory.
- intention(objects) :noindex:
Return maximal set of attributes which are shared by given
objects
- extension(attributes) :noindex:
Return maximal set of objects which share given
attributes
- intention_i(object_indexes) :noindex:
Offer the same logic as intention(…) but objects and attributes are defined by their indexes
- extension_i(attribute_indexes) :noindex:
Offer the same logic as extension(…) but objects and attributes are defined by their indexes
- write_cxt(path=None) :noindex:
Convert the FormalContext into cxt file format (save if
pathis given)
- write_json(path=None) :noindex:
Convert the FormalContext into json file format (save if
pathis given)
- write_csv(path=None, \*\*kwargs) :noindex:
Convert the FormalContext into csv file format (save if
pathis given)
- to_pandas() :noindex:
Convert the FormalContext into pandas.DataFrame object
Notes
Formal Context K = (G, M, I) - is a triplet of: 1. set of objects G (the property
object_namesin this class) 2. set of attributes M (the propertyattribute_namesin this class) 3. binary relation I between G and M (i.e. “gIm holds True” means “object g has an attribute m”)(the property
datain this class)- __init__(data: ~typing.Optional[~typing.Collection[bool]] = None, object_names=None, attribute_names=None, description: ~typing.Optional[str] = None, target=None, backend: {'BinTableLists': <class 'fcapy.context.bintable.BinTableLists'>, 'BinTableNumpy': <class 'fcapy.context.bintable.BinTableNumpy'>, 'BinTableBitarray': <class 'fcapy.context.bintable.BinTableBitarray'>} = 'auto')
- Parameters
data (list of list) – Two dimensional list of bool variables. “data[i][j] = True” represents that i-th object shares j-th attribute
object_names (list of str, optional) – Names of objects (rows) of the FormalContext
attribute_names (list of str, optional) – Names of attributes (columns) of the FormalContext
description (str, optional) – Human readable description of the FormalContext (stored only in json file format)
backend (str, optional) – Name of BinTable class to work with data
Methods
__init__([data, object_names, ...])- param data
Two dimensional list of bool variables.
extension(attributes[, base_objects, ...])Return maximal set of objects which share given
attributesextension_i(attribute_indexes[, base_objects_i])Return indexes of maximal set of objects which share given
attribute_indexesextension_monotone_i(attribute_indexes[, ...])from_pandas(dataframe)Construct a FormalContext from a binarized pandas dataframe
get_minimal_generators(intent[, ...])Get a set of minimal generators for closed intent
intentget_minimal_generators_i(intent_i[, ...])Hash value of FormalContext which do not differ between sessions
intention(objects[, is_monotone])Return maximal set of attributes which are shared by given
objectsintention_i(object_indexes[, base_attrs_i])Return indexes of maximal set of attributes which are shared by given
object_indexesintention_monotone_i(object_indexes[, ...])Return indexes of maximal set of attributes shared by any of given
object_indexesprint_data([max_n_objects, max_n_attributes])Get the FormalContext date in the string formatted as the table
read_csv(path[, sep, word_true, word_false])Read the context from .csv file placed in
pathor from the .csv formatteddataread_cxt([path, data])Read the context from .cxt file placed in
pathor from the .cxt formatteddataread_json([path, data])A method to extract the data of the context in a numerical form (and the names of numerical attributes)
Convert the FormalContext into pandas.DataFrame object
write_csv([path])Convert the FormalContext into csv file format (save if
pathis given)write_cxt([path])Convert the FormalContext into cxt file format (save if
pathis given)write_json([path])Convert the FormalContext into json file format (save if
pathis given)Attributes
Transpose the context
Get or set the names of the attributes in the context
backendGet or set the data with relations between objects and attributes (list of list)
Get or set the human readable description of the context
Get the number of attributes in the context (i.e.
Get the number of objects in the context (i.e.
Get or set the names of the objects in the context
Get the size of the context (num.
A set of target values for supervised ML tasks
- property T: FormalContext
Transpose the context
- property attribute_names: List[str]
Get or set the names of the attributes in the context
- Parameters
value (list of str) – The list of names for the attributes (default are “0”,”1”,…,”n_attributes-1”)
- Raises
AssertionError – If the number of names in the
valuedoes not equal to the number of attributes in the context If the the elements ofvalueare not of type str
- property data: AbstractBinTable
Get or set the data with relations between objects and attributes (list of list)
- Parameters
value (list of list) – value[i][j] represents whether i-th object shares j-th attribute
- Raises
AssertionError – If
valueis not a list Ifvalueof type list is given (should be list of list) If some listsvalue[i]andvalue[j]have different length (should be the same for anyvalue[i]) If anyvalue[i][j]is not of type bool
- property description: str
Get or set the human readable description of the context
JSON is the only file format to store this information. The description will be lost when saving context to .cxt or .csv
- Parameters
value (str or None) – The human readable description of the context
- Raises
AssertionError – If the given
valueis not None and not of type str
- extension(attributes: Collection[str], base_objects: Optional[Collection[str]] = None, is_monotone: bool = False) List[str]
Return maximal set of objects which share given
attributes- Parameters
attributes (list of str) – Names of the attributes (subset of
attribute_names)base_objects (list of str) – Set of objects to look for extension on. Default value: all the objects.
is_monotone (bool (def. False)) – A flag whether to use antimonotone extension (as default) or the monotone one
- Returns
extension – Names of the maximal set of objects which share given
attributes- Return type
tuple of str
- extension_i(attribute_indexes: Collection[int], base_objects_i: Optional[Collection[int]] = None) List[int]
Return indexes of maximal set of objects which share given
attribute_indexes- Parameters
attribute_indexes (list of int) – Indexes of the attributes (from [0,
n_attributes-1])base_objects_i (list of int) – Indexes of set of objects on which to look for extension_i
- Returns
extension_indexes – Indexes of maximal set of objects which share
attribute_indexes- Return type
tuple of int
- static from_pandas(dataframe)
Construct a FormalContext from a binarized pandas dataframe
- Parameters
dataframe (pandas.DataFrame) – The dataframe with boolean values to construct a FormalContext
- Returns
context – A FormalContext corresponding to
dataframe- Return type
FormalContext
- get_minimal_generators(intent: Union[List[str], List[int]], base_generator: Optional[Union[List[str], List[int]]] = None, base_objects: Optional[Union[List[str], List[int]]] = None, use_indexes: bool = False) List[Tuple]
Get a set of minimal generators for closed intent
intentWARNING: The current algorithm looks for mimimUM generators instead of mimimAL
- Parameters
intent – A set of attribute names (or indexes if
use_indexesset to True) to construct generators for.base_generator – A set of attribute names (or indexes if
use_indexesset to True) which should be included in each constructed generatorbase_objects – A set of object names (or indexes if
use_indexesset to True) used to check the generatorsuse_indexes – A flag whether to use object and attribute names (if set to False) or indexes (otherwise)
- Returns
min_gens – A set of miminUM generators of the closed intent
- Return type
list of tuple
Notes
A generator D \subseteq M of a closed description (intent) B \subseteq M is a subset of attributes with the same closed description as B: D’’ = B
A mimimAL generator D \subseteq M of a closed description (intent) B \subseteq M is a generator of B s.t. there is no generator E \subseteq M of B smaller than D: D’’ = B, \nexists E \subset D, E’’=B
A mimimUM generator D \subseteq M of a closed description (intent) B \subseteq M is a generator of B s.t. there is no generator E \subseteq M of B with the smaller size: D’’ = B, \nexists E \subset B, | E | < | D |
- hash_fixed()
Hash value of FormalContext which do not differ between sessions
- intention(objects: Iterable[str], is_monotone: bool = False) List[str]
Return maximal set of attributes which are shared by given
objects- Parameters
objects (list of str) – Names of the objects (subset of
object_names)is_monotone (bool (def. False)) – Return monotone intention if set True else return antimonotone intention (as in classical FCA)
- Returns
intention – Names of maximal set of attributes which are shared by given
objects- Return type
list of str
Notes
Antimonotone intention views the common description of objects as intersection of their descriptions. That is, an attribute belongs to antimonotone intention if it is shared by _all_ the given objects.
Monotone intention view the common description of objects as union of their descriptions. So, an attribute belongs to monotone intention if it is shared by _any_ of the given objects.
- intention_i(object_indexes: Iterable[int], base_attrs_i: Optional[Iterable[int]] = None) List[int]
Return indexes of maximal set of attributes which are shared by given
object_indexes- Parameters
object_indexes (list of int) – Indexes of the objects (from [0,
n_objects-1])base_attrs_i (list of int) – Indexes of attribute indexes to compute the intention on. Default value: indexes of all the attributes
- Returns
intention_i – Indexes of maximal set of attributes which are shared by
objects_indexes- Return type
tuple of int
- intention_monotone_i(object_indexes: Iterable[int], base_attrs_i: Optional[Iterable[int]] = None) List[int]
Return indexes of maximal set of attributes shared by any of given
object_indexes
- property n_attributes: int
Get the number of attributes in the context (i.e. len(data[0])
- property n_objects: int
Get the number of objects in the context (i.e. len(data))
- property object_names: List[str]
Get or set the names of the objects in the context
- Parameters
value (list of str) – The list of names for the objects (default are ‘0’,’1’,…,’n_objects-1’)
- Raises
AssertionError – If the number of names in the
valuedoes not equal to the number of objects in the context If the the elements ofvalueare not of type str
- print_data(max_n_objects=20, max_n_attributes=10)
Get the FormalContext date in the string formatted as the table
- Parameters
max_n_objects (int) – Maximal number of objects to print. If it is less then
n_objectsthen printmax_n_objects/2objects from the “top” and the “bottom” of the contextmax_n_attributes (int) – Maximal number of attributes to print. If it is less then
n_attributesthen printmax_n_attributes/2attributes from the “left” and the “right” part of the context
- Returns
data_to_print – A string with the context data formatted as the table
- Return type
str
- static read_csv(path, sep=',', word_true='True', word_false='False')
Read the context from .csv file placed in
pathor from the .csv formatteddata- Parameters
path (str) – Path to the file to load
sep (str) – A separator used in .csv file
word_true (str) – A placeholder for True values used in .csv file
word_false (str) – A placeholder for False values used in .csv file
- Return type
FormalContext loaded from .csv file
- static read_cxt(path=None, data=None)
Read the context from .cxt file placed in
pathor from the .cxt formatteddata
- property size: Tuple[int, int]
Get the size of the context (num. of objects and num. of attributes)
- property target
A set of target values for supervised ML tasks
- to_numeric()
A method to extract the data of the context in a numerical form (and the names of numerical attributes)
The method is less straightforward for MVContext class
- Returns
data (list of list of bool) – Binary data of connections between objects and attributes
attribute_names (list of str) – Name of attributes from the context
- to_pandas()
Convert the FormalContext into pandas.DataFrame object
- Returns
df – The dataframe with boolean variables,
object_namesturned intodf.index,attribute_namesturned intodf.columns- Return type
pandas.DataFrame
- write_csv(path=None, **kwargs)
Convert the FormalContext into csv file format (save if
pathis given)- Parameters
path (str or None) – Path to save a context
**kwargs –
sepstrField delimiter for the output file
word_truestrA placeholder to put instead of ‘True’ for data[i][j]==True (default ‘True’)
word_falsestrA placeholder to put instead of ‘False’ for data[i][j]==False (default ‘False’)
- Returns
context – If
pathis None, the string with .csv file data is returned. Ifpathis given - return None- Return type
str
- write_cxt(path=None)
Convert the FormalContext into cxt file format (save if
pathis given)- Parameters
path (str or None) – Path to save a context
- Returns
context – If
pathis None, the string with .cxt file data is returned. Ifpathis given - return None- Return type
str
- write_json(path=None)
Convert the FormalContext into json file format (save if
pathis given)- Parameters
path (str or None) – Path to save a context
- Returns
context – If
pathis None, the string with .json file data is returned. Ifpathis given - return None- Return type
str