简体   繁体   中英

How can i make arules.apriori work in python using rpy2

I'm trying to run an apriori algorithm in python using rpy2. i've hit a wall because I want to give the algorithm some parameters but than the code doesn't work. if I leave the parameter blank it runs. Is there a way to make the apriori algorithm work with paramters?

I've got some R experience and in R my code would look something like this.

output <- apriori(input, parameter = list(support=.01, confidence=.01, minlen=2))

python code:

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages

# import R packages
base = importr('base')
arules = importr('arules')
arulesViz = importr('arulesViz')
Matrix = importr('Matrix')
utils = importr('utils')
grid = importr('grid')

data = robjects.r('read.transactions("input_data.csv", sep = ",",rm.duplicates=FALSE)')
summary_r = arules.itemFrequency(data, type="absolute")
apr = arules.apriori(data,parameter=list(support=0.001, confidence=0.001, minlen=2))

print(apr)

I've found the answer to the question above on a different forum.

you need to add the following code

from rpy2.robjects.vectors import ListVector

apr = arules.apriori(data,parameter= robjects.ListVector({"support":0.01, "confidence":0.01, "minlen":2}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM