knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5, warning = FALSE )
library(fcaR)
Traditional Formal Concept Analysis (FCA) deals with binary data: an object either possesses an attribute or not. However, real-world data is often graded. For instance, a user might like a movie to a certain degree, or a patient might show a symptom with a specific intensity.
Fuzzy FCA generalizes the theory to handle degrees of membership in the interval $[0, 1]$. fcaR natively implements this framework, allowing the user to select the underlying fuzzy logic that governs the reasoning process.
To work with fuzzy logic, you create a FormalContext from a matrix containing numeric values between 0 and 1.
Let us consider a dataset representing user preferences for different movie genres:
# Matrix with membership degrees I <- matrix(c( 0.9, 0.1, 0.8, 0.0, 0.2, 1.0, 0.0, 0.9, 0.5, 0.5, 0.5, 0.5, 0.0, 0.8, 0.1, 1.0 ), nrow = 4, byrow = TRUE) rownames(I) <- c("User1", "User2", "User3", "User4") colnames(I) <- c("Action", "Romance", "SciFi", "Drama") fc <- FormalContext$new(I) print(fc)
In Fuzzy FCA, the structure of the lattice and the validity of implications depend on the choice of the fuzzy logic. A logic is defined by an adjoint pair of operators:
While the intersection of fuzzy sets (used for the infimum in the lattice) is typically modeled by the minimum, the derivation operators that form the concepts rely on the residuum: $$A^\uparrow(m) = \bigwedge_{g \in G} (A(g) \to I(g, m))$$
fcaR implements the following logics:
Lukasiewicz:
Gödel (also referred to as Zadeh in fcaR):
Product (Goguen):
By default, fcaR uses Zadeh (Gödel) logic.
For rigorous analysis where magnitude differences matter, Lukasiewicz is often the standard recommendation.
# Switch to Lukasiewicz logic fc$use_logic("Lukasiewicz") fc$get_logic()
Changing the logic changes the definition of $\to$, and therefore, the concepts and implications that will be discovered.
We compute the concept lattice using the selected logic. A fuzzy concept is a pair $(A, B)$ of fuzzy sets (vectors of values in $[0,1]$) closed under the derivation operators.
fc$find_concepts() fc$concepts
We can inspect the membership degrees of a specific concept. Note that an object can now belong "partially" to a concept.
# Select a concept (e.g., the 4th one) C <- fc$concepts$sub(4) # Extent (Degrees of objects) C$get_extent() # Intent (Degrees of attributes) C$get_intent()
The resulting fuzzy lattice can be visualized as a Hasse diagram. The hierarchical structure is preserved based on the inclusion of fuzzy sets.
fc$concepts$plot(mode = "reduced", method = "sugiyama")
Fuzzy implications take the form $A \Rightarrow B$, where $A$ and $B$ are fuzzy sets of attributes. The validity of these rules depends on the selected logic.
fcaR uses advanced algorithms (such as GreConD) adapted to handle degrees of confidence.
fc$find_implications() fc$implications
In the fuzzy setting:
With Lukasiewicz logic, confidence measures "how much is lost" when passing from $A$ to $B$. A confidence of 1 means that for every object, the degree of $A$ is less than or equal to the degree of $B$ (plus the residuum adjustment).
# Filter strong rules fc$implications[fc$implications$support() > 0.2]
The closure() method allows for inference. Given an observed fuzzy set of attributes (e.g., a new user profile), we can compute which other attributes should be present according to the logic of the system.
# Define a new user profile S <- Set$new(attributes = fc$attributes) # Likes Action very much, Drama somewhat S$assign(Action = 1.0, Drama = 0.5) # Compute the semantic closure using Lukasiewicz logic closure_S <- fc$implications$closure(S) # What do we infer about Romance or SciFi? print(closure_S)
| Logic ("Name") | T-Norm ($\otimes$) | Implication ($\to$) | | :--- | :--- | :--- | | Lukasiewicz | $\max(0, x+y-1)$ | $\min(1, 1-x+y)$ | | Product | $x \cdot y$ | $1$ if $x \le y$, else $y/x$ | | Gödel | $\min(x, y)$ | $1$ if $x \le y$, else $y$ |
Selecting the appropriate logic is vital for correctly interpreting the results of find_concepts() and closure().
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.