Skip to main content

Formal Concept Analysis with Python

Project description

Latest PyPI Version License Supported Python Versions Format Readthedocs

Travis Codecov

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

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.

Installation

This package runs under Python 2.7 and 3.5+, 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):

>>> from concepts import Context

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

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

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

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

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

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

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

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

>>> for extent, intent in c.lattice:
...     print('%r %r' % (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):

>>> c.lattice.graphviz()  # doctest: +ELLIPSIS
<graphviz.dot.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.

The included example CXT files are taken from Uta Priss’ FCA homepage

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

concepts-0.9.1.zip (238.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

concepts-0.9.1-py2.py3-none-any.whl (29.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file concepts-0.9.1.zip.

File metadata

  • Download URL: concepts-0.9.1.zip
  • Upload date:
  • Size: 238.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/2.7.16

File hashes

Hashes for concepts-0.9.1.zip
Algorithm Hash digest
SHA256 a2709bde99ece5dd53790e61e5761686ffe1e4b739f832e3364cb2e893e78e64
MD5 0bcf4db5af216e36997a28a51daf10db
BLAKE2b-256 6a0542bdb87a7915a7d279c816eb2405d1beaaf3274d60d2bc54689ea8b281b2

See more details on using hashes here.

File details

Details for the file concepts-0.9.1-py2.py3-none-any.whl.

File metadata

  • Download URL: concepts-0.9.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/2.7.16

File hashes

Hashes for concepts-0.9.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b9a9bc70b8b312c35bcda73d01bbf82ccdad561d6b6b4fd0c51874f0a146c738
MD5 17d506062b4551d22cba38ee8287759e
BLAKE2b-256 5659691a3b7b42c2c6664618ac41efe38a59cc2a28bf8b3d189f16fa3ee3105f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page