Skip to content

xflr6/concepts

Repository files navigation

Concepts

Latest PyPI Version License Supported Python Versions Wheel format

Build Codecov Readthedocs stable Readthedocs latest

Concepts is a simple Python implementation of Formal Concept Analysis (FCA).

Logo

FCA provides a mathematical model for describing a set of objects (e.g. King Arthur, Sir Robin, and the holy grail) with a set of properties (e.g. human, knight, king, and mysterious) which each of the objects either has or not. A table called formal context defines which objects have a given property and vice versa which properties a given object has.

Links

Installation

This package runs under Python 3.8+, use pip to install:

$ pip install concepts

This will also install the bitsets and graphviz packages from PyPI as required dependencies.

Rendering lattice graphs depends on the Graphviz software. Make sure its dot executable is on your systems' path.

Quickstart

Create a formal context defining which object has which property, e.g. from a simple ASCII-art style cross-table with object rows and property columns (alternatively load a CXT or CSV file):

>>> import concepts
>>> context = concepts.Context.fromstring('''
...            |human|knight|king |mysterious|
... King Arthur|  X  |  X   |  X  |          |
... Sir Robin  |  X  |  X   |     |          |
... holy grail |     |      |     |     X    |
... ''')
>>> context  # doctest: +ELLIPSIS
<Context object mapping 3 objects to 4 properties [dae7402a] at 0x...>

Query common properties of objects or common objects of properties (derivation):

>>> context.intension(['King Arthur', 'Sir Robin'])
('human', 'knight')

>>> context.extension(['knight', 'mysterious'])
()

Get the closest matching objects-properties pair of objects or properties (formal concepts):

>>> context['Sir Robin', 'holy grail']
(('King Arthur', 'Sir Robin', 'holy grail'), ())

>>> context['king',]
(('King Arthur',), ('human', 'knight', 'king'))

Iterate over the concept lattice of all objects-properties pairs:

>>> for extent, intent in context.lattice:
...     print(extent, intent)
() ('human', 'knight', 'king', 'mysterious')
('King Arthur',) ('human', 'knight', 'king')
('holy grail',) ('mysterious',)
('King Arthur', 'Sir Robin') ('human', 'knight')
('King Arthur', 'Sir Robin', 'holy grail') ()

Make a Graphviz visualization of the lattice (use .graphviz(view=True) to directly render it and display the resulting PDF):

>>> context.lattice.graphviz()  # doctest: +ELLIPSIS
<graphviz.graphs.Digraph object at 0x...>

https://raw.github.com/xflr6/concepts/master/docs/holy-grail.png

Further reading

The generation of the concept lattice is based on the algorithm from C. Lindig. Fast Concept Analysis. In Gerhard Stumme, editors, Working with Conceptual Structures - Contributions to ICCS 2000, Shaker Verlag, Aachen, Germany, 2000.

Most of the included example CXT files are taken from Uta Priss' FCA homepage.

The mushroom dataset is converted from the UCI Mashine Learning repsitory.

See also

The implementation is based on these Python packages:

  • bitsets – Ordered subsets over a predefined domain
  • graphviz – Simple Python interface for Graphviz

The following package is build on top of concepts:

  • features – Feature set algebra for linguistics

If you want to apply FCA to bigger data sets, you might want to consider other implementations based on more sophisticated algorithms like In-Close or Fcbo.

License

Concepts is distributed under the MIT license.

About

Formal Concept Analysis with Python

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages