Source code for fca.concept

import numpy as np


[docs]class Concept: """Implements Formal Concept. Formal concept in Context :math:`c` is pair :math:`<A,B>`, where :math:`A` is subset of :math:`X`, :math:`B` is subset of :math:`Y` such that :math:`c.up(A) = B` and :math:`c.down(B) = A`. This formal definition is not required, but you have to include extent (objects) and intent (attributes) to create concept. Parameters ---------- extent : :py:class:`numpy.array` All object (covered by attributes) in concept, empty list is allowed. Represented as :py:class:`numpy.array` of bool values. intent : :py:class:`numpy.array` All attributes (covered by objects) in concept, empty list is allowed. Represented as :py:class:`numpy.array` of bool values. Attributes ---------- extent : :py:class:`numpy.array` All object (covered by attributes) in concept. Represented as :py:class:`numpy.array` of bool values. intent : :py:class:`numpy.array` All attributes (covered by objects) in concept. Represented as :py:class:`numpy.array` of bool values. """ def __init__(self, extent, intent): self.extent = extent self.intent = intent def __str__(self): return "Concept (Extent: {}, Intent:{})".format( str(self.extent), str(self.intent)) def __repr__(self): return "concept({}, {})".format( str(self.extent), str(self.intent)) def __eq__(self, other): if isinstance(self, other.__class__): return (np.array_equal(self.extent, other.extent) and np.array_equal(self.intent, other.intent)) return False def __hash__(self): return hash(str((self.extent, self.intent)))
[docs] def human_readable(self, context): """Return human readable strings of extent and intent""" intent = context.data_frame.columns.base[self.intent] extent = context.data_frame.index.base[self.extent] return extent, intent
[docs] @staticmethod def save_list(list_of_concepts, outfile): """save list of concepts into file via numpy.savez""" return np.savez(outfile, np.array(list_of_concepts))
[docs] @staticmethod def load_list(infile): """load list of concepts from file via numpy.load""" loaded = np.load(infile) for arr in loaded.files: return list(loaded[arr])