fcapy.context.FormalContext¶
-
class
fcapy.context.FormalContext(data=None, object_names=None, attribute_names=None, **kwargs)¶ A class used to represent Formal Context object from FCA theory.
-
intention(objects)¶ Return maximal set of attributes which are shared by given
objects
-
extension(attributes)¶ Return maximal set of objects which share given
attributes
-
intention_i(object_indexes)¶ Offer the same logic as intention(…) but objects and attributes are defined by their indexes
-
extension_i(attribute_indexes)¶ Offer the same logic as extension(…) but objects and attributes are defined by their indexes
-
to_cxt(path=None)¶ Convert the FormalContext into cxt file format (save if
pathis given)
-
to_json(path=None)¶ Convert the FormalContext into json file format (save if
pathis given)
-
to_csv(path=None, \*\*kwargs)¶ Convert the FormalContext into csv file format (save if
pathis given)
-
to_pandas()¶ 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 attribute m”) (the propertydatain this class)-
__init__(data=None, object_names=None, attribute_names=None, **kwargs)¶ - 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
**kwargs –
description:str with human readable description of the FormalContext (stored only in json file format)
Methods
__init__([data, object_names, attribute_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_indexesfrom_pandas(dataframe)Construct a FormalContext from a binarized pandas dataframe
get_minimal_generators(intent[, …])Get a set of minimal generators for closed intent
intentintention(objects)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_indexesprint_data([max_n_objects, max_n_attributes])Get the FormalContext date in the string formatted as the table
to_csv([path])Convert the FormalContext into csv file format (save if
pathis given)to_cxt([path])Convert the FormalContext into cxt file format (save if
pathis given)to_json([path])Convert the FormalContext into json file format (save if
pathis given)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
Attributes
Get of set the names of the attributes in the context
Get 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 of set the names of the objects in the context
A set of target values for supervised ML tasks
-
property
attribute_names¶ Get of 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¶ 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¶ 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, base_objects=None)¶ 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 on which to look for extension
- Returns
extension – Names of the maximal set of objects which share given
attributes- Return type
list of str
-
extension_i(attribute_indexes, base_objects_i=None)¶ 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
list 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, base_generator=None, base_objects=None, use_indexes=False)¶ Get a set of minimal generators for closed intent
intentWARNING: The current algorithm looks for mimimUM generators instead of mimimAL
- Parameters
intent (list of string or int) – A set of attribute names (or indexes if
use_indexesset to True) to construct generators for.base_generator (list of string or int) – A set of attribute names (or indexes if
use_indexesset to True) which should be included in each constructed generatorbase_objects (list of string or int) – A set of object names (or indexes if
use_indexesset to True) used to check the generatorsuse_indexes (bool) – 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 |
-
intention(objects)¶ Return maximal set of attributes which are shared by given
objects- Parameters
objects (list of str) – Names of the objects (subset of
object_names)- Returns
intention – Names of maximal set of attributes which are shared by given
objects- Return type
list of str
-
intention_i(object_indexes, base_attrs_i=None)¶ 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])- Returns
intention_i – Indexes of maximal set of attributes which are shared by
objects_indexes- Return type
list of int
-
property
n_attributes¶ Get the number of attributes in the context (i.e. len(data[0])
-
property
n_objects¶ Get the number of objects in the context (i.e. len(data))
-
property
object_names¶ Get of 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
-
property
target¶ A set of target values for supervised ML tasks
-
to_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
-
to_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
-
to_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
-
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
-