fcapy.algorithms.concept_construction

This module contains a number of functions which take a FormalContext (or MVContext) and return a set of formal (or pattern) concepts. Some of them return a ConceptLattice instead of just a set of concepts

Functions

close_by_one(context[, output_as_concepts, …])

Return a list of concepts generated by CloseByOne (CbO) algorithm

parse_decision_tree_to_extents(tree, X[, n_jobs])

Return a set of extents of nodes from sklearn DecisionTree (or RandomForest)

random_forest_concepts(context[, rf_params, …])

Fit a RandomForest model and return a set of pattern concepts used by this model

sofia_binary(context[, L_max, …])

Return a lattice of the most interesting concepts generated by SOFIA algorithm.

sofia_general(context[, L_max, measure, …])

Return a lattice of the most interesting concepts generated by SOFIA algorithm.

fcapy.algorithms.concept_construction.close_by_one(context: fcapy.mvcontext.mvcontext.MVContext, output_as_concepts=True, iterate_extents=None, initial_combinations=None, iter_elements_to_check=None)

Return a list of concepts generated by CloseByOne (CbO) algorithm

Parameters
  • context (FormalContext or MVContext) – A context to build a set of concepts on

  • output_as_concepts (bool) – A flag whether to return a list of concepts as a list of FormalConcept/PatternConcept objects (if set True) or as a dictionary with concepts extents and intents

  • iterate_extents (bool) – A flag whether to run CbO by iterating through subsets of objects (if set True) or of attributes (if set False) By default it sets to True if the set of objects is smaller than the set of attributes

  • initial_combinations (list of int) – A list of subsets of objects/attributes indexes (depends on iterate_extents) to start CbO algorithm from Default value is empty list []

  • iter_elements_to_check (list of int) – A list of attributes/objects indexes (depends on iterate_extents) to run CbO algorithm on

Returns

  • data or concepts depends on output_as_concepts attribute

  • data (dict) – A dictionary which contains a set of concepts extents and concepts intents

  • concepts (list of FormalConcept or PatternConcept) – A list of concepts of class FormalConcept (if given context is of type FormalContext) or PatternConcept (if given context is of type MVContext)

fcapy.algorithms.concept_construction.parse_decision_tree_to_extents(tree, X, n_jobs=1)

Return a set of extents of nodes from sklearn DecisionTree (or RandomForest)

Parameters
  • tree (DecisionTreeRegressor or DecisionTreeClassifier or RandomForestRegressor or RandomForestClassifier) – sklearn DecisionTree or RandomForest to retrieve a set of extents from

  • X (numpy.ndarray) – An input data for tree model. The same format it is used for tree.predict(X) function

  • n_jobs (int) – A number of parallel jobs to run. WARNING: A number of jobs works slower than a single one.

Returns

exts – A list of objects indexes from X described by nodes of decision tree(s) from tree

Return type

list of int

fcapy.algorithms.concept_construction.random_forest_concepts(context: fcapy.mvcontext.mvcontext.MVContext, rf_params=None, rf_class=None)

Fit a RandomForest model and return a set of pattern concepts used by this model

Parameters
  • context (MVContext) – A context to fit a RandomForest on. Training features data for the Forest are kept in context.data Target values are kept in context.target

  • rf_params (dict) – A dict of parameters to initialize RandomForest model with

  • rf_class (type RandomForestRegressor or RandomForestClassifier) – A type of RandomForest model to fit. By default this value is set to RandomForestClassifier if context.target value has only 2 distinct values

Returns

concepts – A list of PatternConcepts retrieved from context by RandomForest

Return type

list of PatternConcept

fcapy.algorithms.concept_construction.sofia_binary(context: fcapy.mvcontext.mvcontext.MVContext, L_max=100, iterate_attributes=True, measure='LStab', projection_sorting=None, proj_to_start=None, use_tqdm=False)

Return a lattice of the most interesting concepts generated by SOFIA algorithm. Optimized for FormalContext

Parameters
  • context (FormalContext or MVContext) – A context to build a list of concepts on

  • L_max (int) – Maximum size of returned lattice. That is the maximum number of most interesting concepts

  • iterate_attributes (bool) – A flag whether to iterate a set of attributes as a chain of projections (if set to True) or a set of objects o/w

  • measure (string) – The name of a concept interesting measure to maximize

  • projection_sorting (str of {‘ascending’, ‘descending’, ‘random’, None}) – A way to sort a chain of projections by their support. E.g. if iterate_attributes is set to True, ‘Ascending’ sorting start running projections from an attribute shared by the least amount of objects to an attributes shared by the most amount of objects

  • proj_to_start (int) – A number of projection (a set of attributes/objects) to construct a basic ConceptLattice on

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

lattice – A ConceptLattice which contains a set of Formal (or Pattern) concepts with high values of given interesting measure

Return type

ConceptLattice

fcapy.algorithms.concept_construction.sofia_general(context: fcapy.mvcontext.mvcontext.MVContext, L_max=100, measure='LStab', proj_to_start=None, use_tqdm=False)

Return a lattice of the most interesting concepts generated by SOFIA algorithm. Can work with any MVContext

Parameters
  • context (FormalContext or MVContext) – A context to build a list of concepts on

  • L_max (int) – Maximum size of returned lattice. That is the maximum number of most interesting concepts

  • measure (string) – The name of a concept interesting measure to maximize

  • proj_to_start (int) – A number of projection (a set of attributes/objects) to construct a basic ConceptLattice on

  • use_tqdm (bool) – A flag whether to visualize the progress of the algorithm with tqdm bar or not

Returns

lattice – A ConceptLattice which contains a set of Formal (or Pattern) concepts with high values of given interesting measure

Return type

ConceptLattice