Coder Social home page Coder Social logo

Comments (5)

mhahsler avatar mhahsler commented on May 24, 2024

Hi sjain777,

The following is an item encoding error.
Error in .local(x, ...) :
All item labels in x must be contained in 'itemLabels' or 'match'._

You get it because the two transaction sets that you create do not have the same encoding for items. You can see that the sets have a different number of items.

t1 <- as(list(c("salt","water"),c("pepper")), "transactions")
t1
transactions in sparse format with
2 transactions (rows) and
3 items (columns)

t2 <- as(list(c("salt","water")), "transactions")
t2
transactions in sparse format with
1 transactions (rows) and
2 items (columns)

I think your second example is caused by the same problem...

-Michael

So to make them comparable you need to do this

t2_new <- recode(t2, match = t1)
t2_new
transactions in sparse format with
1 transactions (rows) and
3 items (columns)

is.subset tries to do this internally, but fails for your data (I will fix that).
You can read up on item encoding in ?encode. The man page is somewhat hidden, but I will add some references in the other manual pages to make this easier to find. Now subset works as expected.

is.subset(t1, t2_new)
{salt,water}
{salt,water} TRUE
{pepper} FALSE

from arules.

sjain777 avatar sjain777 commented on May 24, 2024

Thanks for your quick response. I tried adding "recode" in my process as you advised above, but that failed too. I think, probably new labels (columns) in my test data are causing the problem this time. Here is the example to demonstrate the error:

t1 <- as(list(c("salt","water"),c("pepper"), c("honey"), c("lemon")), "transactions")
t1
transactions in sparse format with
4 transactions (rows) and
5 items (columns)

t2 <- as(list(c("salt","sugar", "water")), "transactions")
t2
transactions in sparse format with
1 transactions (rows) and
3 items (columns)

t2_new <- recode(t2, match = t1)
Error in .local(x, ...) :
All item labels in x must be contained in 'itemLabels' or 'match'.

Thanks for offering to work on a fix

  • Supriya

from arules.

mhahsler avatar mhahsler commented on May 24, 2024

You have in each set an item that is not in the other:

t1 <- as(list(c("salt","water"),c("pepper"), c("honey"), c("lemon")), "transactions")
t1
transactions in sparse format with
4 transactions (rows) and
5 items (columns)
t2 <- as(list(c("salt","sugar", "water")), "transactions")
t2
transactions in sparse format with
1 transactions (rows) and
3 items (columns)
itemLabels(t1)
[1] "honey" "lemon" "pepper" "salt" "water"
itemLabels(t2)
[1] "salt" "sugar" "water"

So you need to recode them using all items (union)

il <- union(itemLabels(t1), itemLabels(t2))
t1 <- recode(t1, il)
t2 <- recode(t2, il)

now subsetting will work

is.subset(t1,t2)
{salt,water,sugar}
{salt,water} TRUE
{pepper} FALSE
{honey} FALSE
{lemon} FALSE

from arules.

sjain777 avatar sjain777 commented on May 24, 2024

Thanks again for your quick feedback. Doing the "union" and then "using "recode" as you suggested above worked! Thanks!

from arules.

dr-habeba avatar dr-habeba commented on May 24, 2024

i try to make classification using cba algorithm in rpy2 but i found this error:

Traceback (most recent call last):

File "", line 1, in runfile('D:/dm.py', wdir='D:')

File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "D:/dm.py", line 104, in c_p=class_pred.pred_class_on_test(c, dtest)

File "C:\ProgramData\Anaconda2\lib\site-packages\rpy2\robjects\functions.py", line 178, in call return super(SignatureTranslatedFunction, self).call(*args, **kwargs)

File "C:\ProgramData\Anaconda2\lib\site-packages\rpy2\robjects\functions.py", line 106, in call res = super(Function, self).call(*new_args, **new_kwargs)

RRuntimeError: Error in .local(x, ...) : All item labels in x must be contained in 'itemLabels' or 'match'.

code:

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

so how i can fix this error?

data set link :
https://archive.ics.uci.edu/ml/datasets/primary+tumor

from arules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.