使用基于分类的关联算法(cba)时rpy2中的错误

时间:2018-11-03 10:09:54

标签: python r rpy2

我尝试在rpy2中使用cba算法进行分类 但是我发现了这个错误:

回溯(最近通话最近一次):

文件“”,第1行,在     runfile('D:/dm.py',wdir ='D:')

Runfile中的文件“ C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第880行     execfile(文件名,命名空间)

execfile中的文件“ C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第87行     exec(compile(scripttext,filename,'e​​xec'),glob,loc)

文件“ D:/dm.py”,第104行,在     c_p = class_pred.pred_class_on_test(c,dtest)

在“ 调用”中的文件“ C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ rpy2 \ robjects \ functions.py”,第178行     返回super(SignatureTranslatedFunction,self)。调用(* args,** kwargs)

文件“ C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ rpy2 \ robjects \ functions.py”,在调用中     res = super(函数,自身)。调用(* new_args,** new_kwargs)

RRuntimeError:.local(x,...)中的错误:   x中的所有商品标签必须包含在“ itemLabels”或“ match”中。

代码:

import os
os.environ['R_HOME'] = 'C:/Program Files/R/R-3.5.1'
os.environ['R_USER'] = 'C:/ProgramData/Anaconda2/Lib/site-packages/rpy2'


import rpy2.robjects as rr
#to import any r backage
from rpy2.robjects.packages import importr
# to call any r user defiend function  from pythn string
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage as stap
# to convert to / from python/r dataframe
from rpy2.robjects import pandas2ri
pandas2ri.activate()

base = importr('base')

# read data set python
from sklearn.cross_validation import train_test_split 
import pandas as pd 
data= pd.read_csv("C://Users//El-Amir Tech//Desktop//tumor.csv")

print(data.info())

data=data.astype(str)

train,test=train_test_split(data, test_size=0.2, random_state=20)





# to convert python data frame to r data frame
dd=pandas2ri.py2ri(train)
dtest=pandas2ri.py2ri(test)

# data minin association rule function from r
rs=importr('arules')
clas=importr('arulesCBA')
rstring=""" 
 # generate rules

ruless <- function (d){

da<- as.data.frame(d)

trans <- as(da, "transactions")

# to get rhs=outcome only
incom <- grep("^Class=",itemLabels(trans), value=TRUE)

rules <- apriori(trans, parameter = list(supp = 0.01, conf = 0.8,minlen=2, target = "rules"),appearance=list(rhs=incom))
return (rules)
}

# convert rules to data frame

rule2dataframe<-function(r){
n<-DATAFRAME(r,separate=TRUE)
return (n)
}
# apply cba classifier

classf <- function (rs){
c1<- CBA_ruleset(Class~ ., rs)
return (c1)
}


pred_class_on_test <- function(classifir,dtest) {
d<- as.data.frame(dtest)
results <- predict(classifir, d)

return(results)}

"""
# call r function using stap
rule= stap(rstring,"ruless")
m=rule.ruless(dd)

r2d=stap(rstring,"rule2dataframe")
r=r2d.rule2dataframe(m)

## convert rules r dataframe to pandas dataframe
rules=pandas2ri.ri2py_dataframe(r)
print (rules)

classs=stap(rstring,"classf")
c=classs.classf(m)
print c

class_pred=stap(rstring,"pred_class_on_test")

c_p=class_pred.pred_class_on_test(c, dtest)
print c_p

那么我该如何解决此错误? data set link :

0 个答案:

没有答案