Documento 12 Formal Context Ganter dataset

12.1 Primera parte

library(readr)
library(knitr)
library(fcaR)
library(rmarkdown)

dataset_ganter <- read.csv("datasets/contextformal_tutorialGanter.csv", sep=";")
paged_table(dataset_ganter)

Los nombres de las filas aparecen como primera columna. Trasladamos el nombre de las filas a la variable rownames(dataset_ganter)

rownames(dataset_ganter) <- dataset_ganter[,1]

Eliminamos la primera columna

dataset_ganter <- dataset_ganter[,-1]
  • Introduce el dataset anterior en un contexto formal de nombre fc_ganter usando el paquete fcaR.
fc_ganter <- FormalContext$new(dataset_ganter)
  • Imprime el contexto formal (print). Haz plot también del contexto formal.
print(fc_ganter)
## Warning: Too many attributes, output will be truncated.
## FormalContext with 7 objects and 9 attributes.
## Attributes' names are: needs.water, lives.in.water, lives.on.hand,
##   needs.chlorophyll, two.seeds.leaves, one.seed.leaf, ...
## Matrix:
##            needs.water lives.in.water lives.on.hand needs.chlorophyll
## Leech                1              1             0                 0
## Bream                1              1             0                 0
## Frog                 1              1             1                 0
## Spike-Weed           1              1             0                 1
## Reed                 1              1             1                 1
## Bean                 1              0             1                 1
##            two.seeds.leaves one.seed.leaf can.move.around
## Leech                     0             0               1
## Bream                     0             0               1
## Frog                      0             0               1
## Spike-Weed                0             1               0
## Reed                      0             1               0
## Bean                      1             0               0
plot(fc_ganter)

Convierte a latex el contexto formal. En el Rmd introduce el código latex del contexto formal para visualizarlo.

fc_ganter$to_latex()
## \begin{table} \centering 
## \begin{tabular}{lrrrrrrrrr}
## \toprule
##   & needs.water & lives.in.water & lives.on.hand & needs.chlorophyll & two.seeds.leaves & one.seed.leaf & can.move.around & has.limbs & suckles.its.offspring\\
## \midrule
## Leech & 1 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\
## Bream & 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 & 0\\
## Frog & 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 1\\
## Spike-Weed & 1 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 0\\
## Reed & 1 & 1 & 1 & 1 & 0 & 1 & 0 & 0 & 0\\
## Bean & 1 & 0 & 1 & 1 & 1 & 0 & 0 & 0 & 0\\
## Maize & 1 & 0 & 1 & 1 & 0 & 1 & 0 & 0 & 0\\
## \bottomrule
## \end{tabular} \caption{\label{}} \end{table}

Guarda todos los atributos en una variable attr_ganter usando los comandos del paquete.

attr_ganter <- fc_ganter$attributes
attr_ganter
## [1] "needs.water"           "lives.in.water"        "lives.on.hand"        
## [4] "needs.chlorophyll"     "two.seeds.leaves"      "one.seed.leaf"        
## [7] "can.move.around"       "has.limbs"             "suckles.its.offspring"

Guarda todos los objetos en una variable obj_ganter usando los comandos del paquete fCar.

obj_ganter <- fc_ganter$objects
obj_ganter
## [1] "Leech"      "Bream"      "Frog"       "Spike-Weed" "Reed"      
## [6] "Bean"       "Maize"

¿De que tipo es la variable attr_ganter?

class(attr_ganter)
## [1] "character"

¿De que tipo es la variable attr_objetos?

class(obj_ganter)
## [1] "character"

Visualizando el contexto formal y utilizando los operadores de derivación, calcula dos conceptos sin usar el método que calcula todos los conceptos.

#Definimos un conjunto de atributos
concept1 <- SparseSet$new(attributes = fc_ganter$objects)
concept1$assign(Reed = 1, Frog = 1)
concept1
## {Frog, Reed}
# Compute the intent of S (atributos más comunes)
fc_ganter$intent(concept1)
## {needs.water, lives.in.water, lives.on.hand}
## -----------------------------------------------------------------------------
# Define a set of objects
concept2 <- SparseSet$new(attributes = fc_ganter$attributes)
concept2$assign(needs.water = 1)
concept2
## {needs.water}
# Compute the extent of S
fc_ganter$extent(concept2)
## {Leech, Bream, Frog, Spike-Weed, Reed, Bean, Maize}

Usar método de fcaR para calcular todos los conceptos.

fc_ganter$find_implications()

fc_ganter$concepts
## A set of 15 concepts:
## 1: ({Leech, Bream, Frog, Spike-Weed, Reed, Bean, Maize}, {needs.water})
## 2: ({Spike-Weed, Reed, Bean, Maize}, {needs.water, needs.chlorophyll})
## 3: ({Spike-Weed, Reed, Maize}, {needs.water, needs.chlorophyll, one.seed.leaf})
## 4: ({Frog, Reed, Bean, Maize}, {needs.water, lives.on.hand})
## 5: ({Reed, Bean, Maize}, {needs.water, lives.on.hand, needs.chlorophyll})
## 6: ({Reed, Maize}, {needs.water, lives.on.hand, needs.chlorophyll, one.seed.leaf})
## 7: ({Bean}, {needs.water, lives.on.hand, needs.chlorophyll, two.seeds.leaves})
## 8: ({Leech, Bream, Frog, Spike-Weed, Reed}, {needs.water, lives.in.water})
## 9: ({Leech, Bream, Frog}, {needs.water, lives.in.water, can.move.around})
## 10: ({Bream, Frog}, {needs.water, lives.in.water, can.move.around, has.limbs})
## 11: ({Spike-Weed, Reed}, {needs.water, lives.in.water, needs.chlorophyll, one.seed.leaf})
## 12: ({Frog, Reed}, {needs.water, lives.in.water, lives.on.hand})
## 13: ({Frog}, {needs.water, lives.in.water, lives.on.hand, can.move.around, has.limbs, suckles.its.offspring})
## 14: ({Reed}, {needs.water, lives.in.water, lives.on.hand, needs.chlorophyll, one.seed.leaf})
## 15: ({}, {needs.water, lives.in.water, lives.on.hand, needs.chlorophyll, two.seeds.leaves, one.seed.leaf, can.move.around, has.limbs, suckles.its.offspring})

¿Cuantos conceptos hemos calculado a partir del contexto formal?

15

Muestra los 10 primeros conceptos.

fc_ganter$concepts[1:10]
## ({Leech, Bream, Frog, Spike-Weed, Reed, Bean, Maize}, {needs.water})
## ({Spike-Weed, Reed, Bean, Maize}, {needs.water, needs.chlorophyll})
## ({Spike-Weed, Reed, Maize}, {needs.water, needs.chlorophyll, one.seed.leaf})
## ({Frog, Reed, Bean, Maize}, {needs.water, lives.on.hand})
## ({Reed, Bean, Maize}, {needs.water, lives.on.hand, needs.chlorophyll})
## ({Reed, Maize}, {needs.water, lives.on.hand, needs.chlorophyll, one.seed.leaf})
## ({Bean}, {needs.water, lives.on.hand, needs.chlorophyll, two.seeds.leaves})
## ({Leech, Bream, Frog, Spike-Weed, Reed}, {needs.water, lives.in.water})
## ({Leech, Bream, Frog}, {needs.water, lives.in.water, can.move.around})
## ({Bream, Frog}, {needs.water, lives.in.water, can.move.around, has.limbs})
fc_ganter$concepts$plot()

Calcular y guardar en una variable el subretículo con soporte mayor que 0.3.

fc_ganter$concepts$support()
##  [1] 1.0000000 0.5714286 0.4285714 0.5714286 0.4285714 0.2857143 0.1428571
##  [8] 0.7142857 0.4285714 0.2857143 0.2857143 0.2857143 0.1428571 0.1428571
## [15] 0.0000000
ind <- which(fc_ganter$concepts$support() > 0.3)
ind
## [1] 1 2 3 4 5 8 9
subreticulo <- fc_ganter$concepts$sublattice(ind)
subreticulo
## A set of 13 concepts:
## 1: ({Leech, Bream, Frog, Spike-Weed, Reed, Bean, Maize}, {needs.water})
## 2: ({Spike-Weed, Reed, Bean, Maize}, {needs.water, needs.chlorophyll})
## 3: ({Spike-Weed, Reed, Maize}, {needs.water, needs.chlorophyll, one.seed.leaf})
## 4: ({Frog, Reed, Bean, Maize}, {needs.water, lives.on.hand})
## 5: ({Reed, Bean, Maize}, {needs.water, lives.on.hand, needs.chlorophyll})
## 6: ({Reed, Maize}, {needs.water, lives.on.hand, needs.chlorophyll, one.seed.leaf})
## 7: ({Leech, Bream, Frog, Spike-Weed, Reed}, {needs.water, lives.in.water})
## 8: ({Leech, Bream, Frog}, {needs.water, lives.in.water, can.move.around})
## 9: ({Spike-Weed, Reed}, {needs.water, lives.in.water, needs.chlorophyll, one.seed.leaf})
## 10: ({Frog, Reed}, {needs.water, lives.in.water, lives.on.hand})
## 11: ({Frog}, {needs.water, lives.in.water, lives.on.hand, can.move.around, has.limbs, suckles.its.offspring})
## 12: ({Reed}, {needs.water, lives.in.water, lives.on.hand, needs.chlorophyll, one.seed.leaf})
## 13: ({}, {needs.water, lives.in.water, lives.on.hand, needs.chlorophyll, two.seeds.leaves, one.seed.leaf, can.move.around, has.limbs, suckles.its.offspring})

Dibujar dicho subretículo.

subreticulo$plot()

¿De que tipo es el subretículo obtenido?.

class(subreticulo)
## [1] "ConceptLattice" "R6"

¿Qué metodos puedes usar con el subretículo calculado? Pon ejemplos y explica.

Clone, extents, infimum, initialize, intents, is_empty, join_irreducibles, lower_neighbours, meet_irreducibles, plot, print, size, subconcepts, sublattice, superconcepts, support, supremum, to_latex, upper_neighbours

Calcula el superior y el infimo de los conceptos calculados para fc_ganter y lo mismo para el subretículo anterior. Visualizalos.

# Cogemos un subconjunto de concepts
C <- fc_ganter$concepts[8:10]

fc_ganter$concepts$supremum(C)
## ({Leech, Bream, Frog, Spike-Weed, Reed}, {needs.water, lives.in.water})
fc_ganter$concepts$infimum(C)
## ({Bream, Frog}, {needs.water, lives.in.water, can.move.around, has.limbs})
subreticulo$supremum(2:5)
## ({Leech, Bream, Frog, Spike-Weed, Reed, Bean, Maize}, {needs.water})
subreticulo$infimum(2:3)
## ({Spike-Weed, Reed, Maize}, {needs.water, needs.chlorophyll, one.seed.leaf})

Grabar el objeto fc_ganter en un fichero fc_ganter.rds.

fc_ganter$save(filename = "./fc.rds")

Elimina la variable fc_ganter. Carga otra vez en la variable del fichero anterior y comprueba que tenemos toda la información: atributos, conceptos, etc.

fc2 <- FormalContext$new()
fc2$load("./fc.rds")

#Comprobamos que se ha cargado correctamente
fc2$plot()

Calcula lo siguientes conjuntos usando los métodos del paquete fcaR:

{Bean}′ {livesonland}′ {twoseedleaves}′ {Frog,Maize}′ {needschlorophylltoproducefood,canmovearound}′ {livesinwater,livesonland}′ {needschlorophylltoproducefood,canmovearound}′

Únicamente calculamos el primero ya que todos siguen el mismo proceso:

conj <- SparseSet$new(attributes = fc_ganter$objects)
conj$assign(Bean = 1)
fc_ganter$intent(conj)
## {needs.water, lives.on.hand, needs.chlorophyll, two.seeds.leaves}

12.2 Segunda parte, clase 06/05/20

Calcula las implicaciones del contexto

# Compute implications
fc_ganter$find_implications(verbose = FALSE)

Muestra las implicaciones en pantalla

fc_ganter$implications
## Implication set with 10 implications.
## Rule 1: {} -> {needs.water}
## Rule 2: {needs.water, suckles.its.offspring} -> {lives.in.water,
##   lives.on.hand, can.move.around, has.limbs}
## Rule 3: {needs.water, has.limbs} -> {lives.in.water, can.move.around}
## Rule 4: {needs.water, can.move.around} -> {lives.in.water}
## Rule 5: {needs.water, one.seed.leaf} -> {needs.chlorophyll}
## Rule 6: {needs.water, two.seeds.leaves} -> {lives.on.hand,
##   needs.chlorophyll}
## Rule 7: {needs.water, lives.on.hand, needs.chlorophyll,
##   two.seeds.leaves, one.seed.leaf} -> {lives.in.water, can.move.around,
##   has.limbs, suckles.its.offspring}
## Rule 8: {needs.water, lives.in.water, needs.chlorophyll} ->
##   {one.seed.leaf}
## Rule 9: {needs.water, lives.in.water, needs.chlorophyll,
##   one.seed.leaf, can.move.around} -> {lives.on.hand, two.seeds.leaves,
##   has.limbs, suckles.its.offspring}
## Rule 10: {needs.water, lives.in.water, lives.on.hand, can.move.around}
##   -> {has.limbs, suckles.its.offspring}

¿Cuantas implicaciones se han extraido?

fc_ganter$implications$cardinality()
## [1] 10

Calcula el tamaño de las implicaciones y la media de la parte y derecha de dichas implicaciones.

sizes <- fc_ganter$implications$size()
colMeans(sizes)
## LHS RHS 
## 2.7 2.2

Aplica las reglas de la lógica de simplificación. ¿Cuántas implicaciones han aparecido tras aplicar la lógica?

fc_ganter$implications$apply_rules(rules = c("simplification"), 
                            reorder = FALSE,
                            parallelize = FALSE)
## Processing batch
## --> simplification: from 10 to 10 in 0.14 secs.
## Batch took 0.14 secs.
fc_ganter$implications$cardinality()
## [1] 10
fc_ganter$implications
## Implication set with 10 implications.
## Rule 1: {} -> {needs.water}
## Rule 2: {suckles.its.offspring} -> {lives.in.water, lives.on.hand,
##   can.move.around, has.limbs}
## Rule 3: {has.limbs} -> {lives.in.water, can.move.around}
## Rule 4: {can.move.around} -> {lives.in.water}
## Rule 5: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 6: {two.seeds.leaves} -> {lives.on.hand, needs.chlorophyll}
## Rule 7: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 8: {lives.in.water, needs.chlorophyll} -> {one.seed.leaf}
## Rule 9: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
## Rule 10: {lives.on.hand, can.move.around} -> {has.limbs,
##   suckles.its.offspring}
sizes <- fc_ganter$implications$size()
colMeans(sizes)
## LHS RHS 
## 1.3 2.2

Eliminar la redundancia en el conjunto de implicaciones. ¿Cuantas implicaciones han aparecido tras aplicar la lógica?

fc_ganter$implications$apply_rules(rules = c("composition",
                                             "generalization",
                                             "simplification"),
                                          parallelize = FALSE)
## Processing batch
## --> composition: from 10 to 10 in 0 secs.
## --> generalization: from 10 to 10 in 0.03 secs.
## --> simplification: from 10 to 10 in 0.06 secs.
## Batch took 0.09 secs.
fc_ganter$implications
## Implication set with 10 implications.
## Rule 1: {} -> {needs.water}
## Rule 2: {suckles.its.offspring} -> {lives.in.water, lives.on.hand,
##   can.move.around, has.limbs}
## Rule 3: {has.limbs} -> {lives.in.water, can.move.around}
## Rule 4: {can.move.around} -> {lives.in.water}
## Rule 5: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 6: {two.seeds.leaves} -> {lives.on.hand, needs.chlorophyll}
## Rule 7: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 8: {lives.in.water, needs.chlorophyll} -> {one.seed.leaf}
## Rule 9: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
## Rule 10: {lives.on.hand, can.move.around} -> {has.limbs,
##   suckles.its.offspring}
fc_ganter$implications$cardinality()
## [1] 10

Calcular el cierre de los atributos needs.water, one.seed.leaf.

S <- SparseSet$new(attributes = fc_ganter$attributes)
S$assign(needs.water = 1, one.seed.leaf=1)
S
## {needs.water, one.seed.leaf}
fc_ganter$implications$closure(S)
## $closure
## {needs.water, needs.chlorophyll, one.seed.leaf}

Copia (clona) el conjunto fc_ganter en una variable fc1.

fc1 <- fc_ganter$clone()
fc1$plot()

Elimina la implicación que está en la primera posición:

# Este comando elimina la primera, como en arules
fc1$implications
## Implication set with 10 implications.
## Rule 1: {} -> {needs.water}
## Rule 2: {suckles.its.offspring} -> {lives.in.water, lives.on.hand,
##   can.move.around, has.limbs}
## Rule 3: {has.limbs} -> {lives.in.water, can.move.around}
## Rule 4: {can.move.around} -> {lives.in.water}
## Rule 5: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 6: {two.seeds.leaves} -> {lives.on.hand, needs.chlorophyll}
## Rule 7: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 8: {lives.in.water, needs.chlorophyll} -> {one.seed.leaf}
## Rule 9: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
## Rule 10: {lives.on.hand, can.move.around} -> {has.limbs,
##   suckles.its.offspring}
fc1$implications <- fc1$implications[-1]

Extrae de todas las implicaciones la que tengan en el lado izquierdo de la implicación el atributo one.seed.leaf.

fc1$implications
## Implication set with 9 implications.
## Rule 1: {suckles.its.offspring} -> {lives.in.water, lives.on.hand,
##   can.move.around, has.limbs}
## Rule 2: {has.limbs} -> {lives.in.water, can.move.around}
## Rule 3: {can.move.around} -> {lives.in.water}
## Rule 4: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 5: {two.seeds.leaves} -> {lives.on.hand, needs.chlorophyll}
## Rule 6: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 7: {lives.in.water, needs.chlorophyll} -> {one.seed.leaf}
## Rule 8: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
## Rule 9: {lives.on.hand, can.move.around} -> {has.limbs,
##   suckles.its.offspring}
leaf_lhs <- fc1$implications$filter(rhs = NULL, lhs = "one.seed.leaf", drop = FALSE)
leaf_lhs
## Implication set with 3 implications.
## Rule 1: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 2: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 3: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
fc1$implications
## Implication set with 9 implications.
## Rule 1: {suckles.its.offspring} -> {lives.in.water, lives.on.hand,
##   can.move.around, has.limbs}
## Rule 2: {has.limbs} -> {lives.in.water, can.move.around}
## Rule 3: {can.move.around} -> {lives.in.water}
## Rule 4: {one.seed.leaf} -> {needs.chlorophyll}
## Rule 5: {two.seeds.leaves} -> {lives.on.hand, needs.chlorophyll}
## Rule 6: {two.seeds.leaves, one.seed.leaf} -> {lives.in.water,
##   can.move.around, has.limbs, suckles.its.offspring}
## Rule 7: {lives.in.water, needs.chlorophyll} -> {one.seed.leaf}
## Rule 8: {one.seed.leaf, can.move.around} -> {lives.on.hand,
##   two.seeds.leaves, has.limbs, suckles.its.offspring}
## Rule 9: {lives.on.hand, can.move.around} -> {has.limbs,
##   suckles.its.offspring}

Obtén los atributos que aparezcan en las implicaciones.

fc1$implications$get_attributes()
## [1] "needs.water"           "lives.in.water"        "lives.on.hand"        
## [4] "needs.chlorophyll"     "two.seeds.leaves"      "one.seed.leaf"        
## [7] "can.move.around"       "has.limbs"             "suckles.its.offspring"

Calcula el soporte de la implicación 3.

fc1$implications$support()
## [1] 0.1111111 0.1111111 0.3333333 0.3333333 0.2222222 0.1111111 0.1111111
## [8] 0.1111111 0.1111111
fc1$implications$support()[3]
## [1] 0.3333333