Giter VIP home page Giter VIP logo

concepts's Introduction

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.

concepts's People

Contributors

jboynyc avatar mikulatomas avatar xflr6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

concepts's Issues

Concept from pandas dataframe

Hello,

Have you consider to allow the creation of a formal context based on a pandas DataFrame, which could be a sparse matrix (with empty cells and cells filled with 'X' or 1) or a binary matrix (filled with 0/1) ?

In my current work, I use a pandas dataframe filled with 'X' and blank cells, as needed by the package, but I have to export the file in a csv file before re-opening it with the concepts.Context.fromfile() method, which is not practical.

Thanks.

Scalable method for deserializing lattice objects?

Is there a scalable way to deserialize lattice objects to disk? Especially when concept lattices are large, I want to avoid recomputing them. However, for large lattices the max recursion depth is hit quickly when pickling. Increasing the max recursion depth may help in border cases, but is not a scalable solution. I was thinking about printing the lattice information to disk print(lattice, file=lattice.txt), but I do not know how I could possibly reconstruct a lattice object from that information.

Is a feature like this available?

Access to concepts themselves

It seems that currently, context's concepts are accessible only through the lattice representation, through the (implicitly protected) attribute _concepts.

Accessing the Concept instances is necessary for various reasons, including the mapping of a concept with its node in the lattice representation.

Hack: use _concepts even if it's not public API.

Plans for lattice reduction?

Many thanks for your work on this! After playing around with this for a while I inevitably wound up with some messy lattices, so I was wondering whether you have any plans to include simple lattice reduction techniques based on stability, support and the like in concepts.

I came across this repository with an MIT-licensed Python implementation of lattice reduction, and if it's of interest I could try to integrate that in this library.

missing middle level labels ?

Don't know is this bug or a feature... but when drawing the lattice the labels at middle layers are missing !!!
Not like here : https://concepts.readthedocs.io/en/stable/_images/relations.svg
here is the csv..

,does_carry_transport,has_wheels,does_drive_is_driven,has_a_seat_seats,made_of_metal
bus0,X,X,X,,
bus1,,X,X,X,
bus2,X,X,X,,
bus3,X,,X,X,
bus4,,X,X,X,
bus5,X,,X,X,
bus6,,X,,X,X
bus7,,X,X,,X
bus8,,X,X,,X
bus9,X,X,,,X
bus10,X,X,X,,
bus11,X,,,X,X
bus12,,X,,X,X
bus13,X,X,X,,
bus14,X,X,,X,
bus15,,X,X,X,
bus16,X,X,,,X
bus17,,X,X,X,
bus18,X,X,,,X
bus19,X,X,,X,

lattice_memebers.Concept vs _common.Concept API

Hi,

I found quite counter-intuitive (for me) API naming.

In case of _common.Concept we have

@property
def objects(self) -> typing.Tuple[str]:
    """The objects subsumed by the concept."""
    return self.extent.members()

In case of lattice_members.Concept we have

@property
def extent(self) -> typing.Tuple[str, ...]:
    """The objects subsumed by the concept.

    Example:
        >>> import concepts
        >>> lattice = concepts.Context.fromstring(concepts.EXAMPLE).lattice
        >>> lattice['+1',].extent
        ('1sg', '1pl')
    """
    return self._extent.members()

and Concept.objects works differently.

I would prefer to have same API, that is, Vector extent can be hidden in concept._extent property, public list of members names can be concept.extent or concept.objects and original concept.objects can be renamed.

Saving lattice visualization to a file

Hello!

I'm trying to improve my FCA libraries performance benchmark (https://github.com/EgorDudyrev/FCApy/tree/feature/benchmarking/notebooks/Performance_Benchmark). But I have some problems saving concepts lattice visualization to a file.

I use the function context.lattice.graphviz('concepts', render=True, ) to save a visualization into a file 'concepts.pdf'. It works for a small context (like 'animal_movement'). But when applied to 'Bob Ross elements-by-episode' dataset it results in a very wide and totally white pdf file.

What is the proper way to save (big) lattice visualization to a file? Can it be png file instead of pdf?

The code to reproduce the error:

import concepts
import pandas as pd

fname = 'bob_ross.csv'
!wget -O {fname} -q https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv 

df = pd.read_csv(fname)
df['EPISODE_TITLE'] = df['EPISODE']+' '+df['TITLE']
df = df.drop(['EPISODE','TITLE'],1).set_index('EPISODE_TITLE').astype(bool)

context = concepts.Context(df.index, df.columns, df.values)
context.lattice.graphviz('concepts', render=True, );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.