How to use the R package arulesViz from Python using arulespy¶
This document also runs directly in Google Colab.
Installation¶
This notebook needs arulespy installed and R set up (see Installation section on arulespy GitHub page).
On Google Colab, only arulespy needs to be installed by uncomment the installation instruction in following code block:
# %pip install arulespy
Import Data and Mine Rules¶
Import the arules module from package arulespy.
from arulespy.arules import Transactions, apriori, parameters
from arulespy.arulesViz import inspect_dt, plot
We read Zoo.cvs which contains the description of 101 animals containing nominal variables and one quantitative variable as a pandas dataframe.
import pandas as pd
df = pd.read_csv('Zoo.csv')
df
| hair | feathers | eggs | milk | airborne | aquatic | predator | toothed | backbone | breathes | venomous | fins | legs | tail | domestic | catsize | type | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | True | False | False | True | False | False | True | True | True | True | False | False | 4 | False | False | True | mammal |
| 1 | True | False | False | True | False | False | False | True | True | True | False | False | 4 | True | False | True | mammal |
| 2 | False | False | True | False | False | True | True | True | True | False | False | True | 0 | True | False | False | fish |
| 3 | True | False | False | True | False | False | True | True | True | True | False | False | 4 | False | False | True | mammal |
| 4 | True | False | False | True | False | False | True | True | True | True | False | False | 4 | True | False | True | mammal |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 96 | True | False | False | True | False | False | False | True | True | True | False | False | 2 | True | False | True | mammal |
| 97 | True | False | True | False | True | False | False | False | False | True | True | False | 6 | False | False | False | insect |
| 98 | True | False | False | True | False | False | True | True | True | True | False | False | 4 | True | False | True | mammal |
| 99 | False | False | True | False | False | False | False | False | False | True | False | False | 0 | False | False | False | mollusc.et.al |
| 100 | False | True | True | False | True | False | False | False | True | True | False | False | 2 | True | False | False | bird |
101 rows × 17 columns
Convert the pandas dataframe into a sparse transactions object.
trans = Transactions.from_df(df)
trans.as_df()
| items | transactionID | |
|---|---|---|
| 1 | {hair,milk,predator,toothed,backbone,breathes,... | 0 |
| 2 | {hair,milk,toothed,backbone,breathes,legs=[4,8... | 1 |
| 3 | {eggs,aquatic,predator,toothed,backbone,fins,l... | 2 |
| 4 | {hair,milk,predator,toothed,backbone,breathes,... | 3 |
| 5 | {hair,milk,predator,toothed,backbone,breathes,... | 4 |
| ... | ... | ... |
| 97 | {hair,milk,toothed,backbone,breathes,legs=[2,4... | 96 |
| 98 | {hair,eggs,airborne,breathes,venomous,legs=[4,... | 97 |
| 99 | {hair,milk,predator,toothed,backbone,breathes,... | 98 |
| 100 | {eggs,breathes,legs=[0,2),type=mollusc.et.al} | 99 |
| 101 | {feathers,eggs,airborne,backbone,breathes,legs... | 100 |
101 rows × 2 columns
rules = apriori(trans,
parameter = parameters({"supp": 0.1, "conf": 0.8}),
control = parameters({"verbose": False}))
rules.as_df().round(2)
| LHS | RHS | support | confidence | coverage | lift | count | |
|---|---|---|---|---|---|---|---|
| 1 | {} | {backbone} | 0.82 | 0.82 | 1.00 | 1.00 | 83 |
| 2 | {type=fish} | {fins} | 0.13 | 1.00 | 0.13 | 5.94 | 13 |
| 3 | {type=fish} | {legs=[0,2)} | 0.13 | 1.00 | 0.13 | 4.39 | 13 |
| 4 | {type=fish} | {aquatic} | 0.13 | 1.00 | 0.13 | 2.81 | 13 |
| 5 | {type=fish} | {eggs} | 0.13 | 1.00 | 0.13 | 1.71 | 13 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 5896 | {hair,milk,predator,toothed,backbone,breathes,... | {catsize} | 0.12 | 0.86 | 0.14 | 1.97 | 12 |
| 5897 | {hair,milk,predator,toothed,backbone,breathes,... | {legs=[4,8]} | 0.12 | 0.92 | 0.13 | 1.83 | 12 |
| 5898 | {milk,predator,toothed,backbone,breathes,legs=... | {hair} | 0.12 | 1.00 | 0.12 | 2.35 | 12 |
| 5899 | {hair,milk,predator,toothed,backbone,breathes,... | {type=mammal} | 0.12 | 1.00 | 0.12 | 2.46 | 12 |
| 5900 | {hair,predator,toothed,backbone,breathes,legs=... | {milk} | 0.12 | 1.00 | 0.12 | 2.46 | 12 |
5900 rows × 7 columns
arulesViz with ggplot¶
arulesViz provides ggplot as an drawing engine. arulesViz.plot() returns the ggplot object which can be
included in the notebook using image_png().
import rpy2.robjects.lib.ggplot2 as gp
from rpy2.ipython.ggplot import image_png
gg = plot(rules, method="scatter")
image_png(gg)
R callback write-console: To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter. R callback write-console: In addition: R callback write-console: Warning message: R callback write-console: Column(s) 13, 17 not logical or factor. Applying default discretization (see '? discretizeDF').
gg = plot(rules, method="grouped")
image_png(gg)
rules_20 = rules.sort(by = 'confidence')[0:100]
gg = plot(rules_20, method="graph")
image_png(gg)
Interactive Rules Table with HTML Widget¶
arulesViz can create interactive HTML widgets that can be integrated into a
Python notebook by saving the widget as a self-contained HTML file and embedding its contents in an iframe. The html_widget helper makes this easy.
from arulespy.arulesViz import html_widget
m = inspect_dt(rules)
html_widget(m, "datatable.html", height=600)
arulesViz with HTML Widgets¶
m = plot(rules_20, method="graph", engine="htmlwidget")
html_widget(m, "graph.html", height=800)
Interactive RuleExplorer¶
Rule Explorer only has experimental support at this point. The rule_explorer() function opens a Shiny app in the web browser. Shutting down the Shiny app from within a Python notebook seems to be difficult. Uncomment the following line to explore the rules.
from arulespy.arulesViz import rule_explorer
# rule_explorer(rules)
An example of a running Rule Explorer can be found here.