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)  :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 path is given)

write_json(path=None)  :noindex:

Convert the FormalContext into json file format (save if path is given)

write_csv(path=None, \*\*kwargs)  :noindex:

Convert the FormalContext into csv file format (save if path is 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_names in this class) 2. set of attributes M (the property attribute_names in this class) 3. binary relation I between G and M (i.e. “gIm holds True” means “object g has attribute m”) (the property data in 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 attributes

extension_i(attribute_indexes[, base_objects_i])

Return indexes of maximal set of objects which share given 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 intent

hash_fixed()

Hash value of FormalContext which do not differ between sessions

intention(objects)

Return maximal set of attributes which are shared by given objects

intention_i(object_indexes[, base_attrs_i])

Return indexes of maximal set of attributes which are shared by given object_indexes

print_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 path or from the .csv formatted data

read_cxt([path, data])

Read the context from .cxt file placed in path or from the .cxt formatted data

read_json([path, data])

to_numeric()

A method to extract the data of the context in a numerical form (and the names of numerical attributes)

to_pandas()

Convert the FormalContext into pandas.DataFrame object

write_csv([path])

Convert the FormalContext into csv file format (save if path is given)

write_cxt([path])

Convert the FormalContext into cxt file format (save if path is given)

write_json([path])

Convert the FormalContext into json file format (save if path is given)

Attributes

attribute_names

Get or set the names of the attributes in the context

data

Get or set the data with relations between objects and attributes (list of list)

description

Get or set the human readable description of the context

n_attributes

Get the number of attributes in the context (i.e.

n_objects

Get the number of objects in the context (i.e.

object_names

Get or set the names of the objects in the context

target

A set of target values for supervised ML tasks

property attribute_names

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 value does not equal to the number of attributes in the context If the the elements of value are 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 value is not a list If value of type list is given (should be list of list) If some lists value[i] and value[j] have different length (should be the same for any value[i]) If any value[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 value is 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 to look for extension on. Default value: all the objects.

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 intent

WARNING: 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_indexes set to True) to construct generators for.

  • base_generator (list of string or int) – A set of attribute names (or indexes if use_indexes set to True) which should be included in each constructed generator

  • base_objects (list of string or int) – A set of object names (or indexes if use_indexes set to True) used to check the generators

  • use_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 |

hash_fixed()

Hash value of FormalContext which do not differ between sessions

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])

  • 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

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 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 value does not equal to the number of objects in the context If the the elements of value are 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_objects then print max_n_objects/2 objects from the “top” and the “bottom” of the context

  • max_n_attributes (int) – Maximal number of attributes to print. If it is less then n_attributes then print max_n_attributes/2 attributes 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 path or from the .csv formatted data

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 path or from the .cxt formatted data

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_names turned into df.index, attribute_names turned into df.columns

Return type

pandas.DataFrame

write_csv(path=None, **kwargs)

Convert the FormalContext into csv file format (save if path is given)

Parameters
  • path (str or None) – Path to save a context

  • **kwargs

    sepstr

    Field delimiter for the output file

    word_truestr

    A placeholder to put instead of ‘True’ for data[i][j]==True (default ‘True’)

    word_falsestr

    A placeholder to put instead of ‘False’ for data[i][j]==False (default ‘False’)

Returns

context – If path is None, the string with .csv file data is returned. If path is given - return None

Return type

str

write_cxt(path=None)

Convert the FormalContext into cxt file format (save if path is given)

Parameters

path (str or None) – Path to save a context

Returns

context – If path is None, the string with .cxt file data is returned. If path is given - return None

Return type

str

write_json(path=None)

Convert the FormalContext into json file format (save if path is given)

Parameters

path (str or None) – Path to save a context

Returns

context – If path is None, the string with .json file data is returned. If path is given - return None

Return type

str