Package: arulespy
Python interface to the R package arulesarulespyis a Python module available fromPyPI.
Thearulesmodule inarulespyprovides an easy to install Python interface to theR package arulesfor association rule mining built
withrpy2.The R arules package implements a comprehensive
infrastructure for representing, manipulating and analyzing transaction data and patterns using frequent itemsets and association rules.
The package also provides a wide range of interest measures and mining algorithms including the code of Christian Borgelt’s popular
and efficient C implementations of the association mining algorithms Apriori and Eclat,
and optimized C/C++ code for
mining and manipulating association rules using sparse matrix representation.ThearulesVizmodule providesplot()for visualizing association rules using
theR package arulesViz.arulespyprovides Python classes
forTransactions: Convert pandas dataframes into transaction dataRules: Association rulesItemsets: ItemsetsItemMatrix: sparse matrix representation of sets of items.with Phyton-style slicing andlen().Most arules functions are
interfaced as methods for the four classes with conversion from the R data structures to Python.
Documentation is avaialible in Python viahelp(). Detailed online documentation
for the R package is availablehere.Low-levelarulesfunctions can also be directly used in the formR.<arules R function>(). The result will be arpy2data type.
Transactions, itemsets and rules can manually be converted to Python
classes using the helper functiona2p().To cite the Python module ‘arulespy’ in publications use:Michael Hahsler. ARULESPY: Exploring association rules and frequent itemsets in Python. arXiv:2305.15263 [cs.DB], May 2023. DOI:10.48550/arXiv.2305.15263Installationarulespyis based on the python packagerpy2which requires an R installation. Here are the installation steps:Install the latest version of R (>4.0) fromhttps://www.r-project.org/Install required libraries on your OS:libcurl is needed by R packagecurl.Ubuntu:sudo apt-get install libcurl4-openssl-devMacOS:brew install curlWindows: no installation necessary, but read the Windows section below.Installarulespywhich will automatically installrpy2andpandas.pipinstallarulespyOptional: Set the environment variableR_LIBS_USERto decide where R packages are stored
(seelibPaths()for details). If not set then R will determine a suitable location.Optional:arulespywill install the needed R packages when it is imported for the first time.
This may take a while. R packages can also be preinstalled. Start R and runinstall.packages(c("arules", "arulesViz"))The most likely issue is thatrpy2does not find R or R's shared library.
This will lead the python kernel to die or exit without explanation when the packagearulespyis imported.
Checkpython -m rpy2.situationto see if R and R's libraries are found.
If you use iPython notebooks then you can include the following code block in your notebook to check:fromrpy2importsituationforrowinsituation.iter_info():print(row)The output should include a line sayingLoading R library from rpy2: OK.Note for Windows usersrpy2currently does not fully support Windows and the installation is somewhat tricky. I was able to use it with the following setup:Windows 10rpy2 version 3.5.14Python version 3.10.12R version 4.3.1I use the following code to set the needed environment variables needed by Windows
before I import fromarulespyfromrpy2importsituationimportosr_home=situation.r_home_from_registry()r_bin=r_home+'\\bin\\x64\\'os.environ['R_HOME']=r_homeos.environ['PATH']=r_bin+";"+os.environ['PATH']os.add_dll_directory(r_bin)forrowinsituation.iter_info():print(row)The output should include a line sayingLoading R library from rpy2: OKMore information on installingrpy2can be foundhere.Examplefromarulespy.arulesimportTransactions,apriori,parametersimportpandasaspd# define the data as a pandas dataframedf=pd.DataFrame([[True,True,True],[True,False,False],[True,True,True],[True,False,False],[True,True,True]],columns=list('ABC'))# convert dataframe to transactionstrans=transactions.from_df(df)# mine association rulesrules=apriori(trans,parameter=parameters({"supp":0.1,"conf":0.8}),control=parameters({"verbose":False}))# display the rules as a pandas dataframerules.as_df()LHSRHSsupportconfidencecoverageliftcount1{}{A}0.80.81182{}{C}0.80.81183{B}{A}0.40.80.5144{B}{C}0.510.51.2555{A,B}{C}0.410.41.2546{B,C}{A}0.40.80.514Complete examples:Using arulesUsing arulesVizReferencesMichael Hahsler.ARULESPY: Exploring association rules and frequent itemsets in
Python.arXiv:2305.15263 [cs.DB], May 2023.
DOI: 10.48550/arXiv.2305.15263Michael Hahsler, Sudheer Chelluboina, Kurt Hornik, and Christian
Buchta.The arules R-package ecosystem: Analyzing interesting
patterns from large transaction
datasets.Journal of Machine Learning Research,12:1977-1981, 2011.Michael Hahsler, Bettina Grün and Kurt Hornik.arules - A
Computational Environment for Mining Association Rules and Frequent
Item Sets.Journal of
Statistical Software,14(15), 2005. DOI: 10.18637/jss.v014.i15Hahsler, Michael.A Probabilistic Comparison of Commonly Used
Interest Measures for Association
Rules, 2015, URL:https://mhahsler.github.io/arules/docs/measures.Michael Hahsler.An R Companion for Introduction to Data Mining:
Chapter
5,
2021, URL:https://mhahsler.github.io/Introduction_to_Data_Mining_R_Examples/book/