Comments (5)
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.
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.
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.
Thanks again for your quick feedback. Doing the "union" and then "using "recode" as you suggested above worked! Thanks!
from arules.
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)
- Error in validObject(.Object) : invalid class “ngTMatrix” object: all row indices (slot 'i') must be between 0 and nrow-1 in a TsparseMatrix HOT 1
- interestMeasure count for itemsets HOT 1
- continuous features? HOT 1
- Help installing arules HOT 3
- Bug: RStudio generates X ammount of Rules, Knited Document has Y HOT 10
- Check item encoding HOT 1
- error in evaluating the argument 'x' in selecting a method for function 'colSums': 'NA' indices are not (yet?) supported for sparse Matrices HOT 4
- Error in transactions(AdultUCI) : could not find function "transactions" HOT 2
- issue when aggregating items HOT 5
- What is transIdenticalToItemsets? HOT 1
- Apriori " Error in deparse1(call) : could not find function "deparse1" " HOT 1
- Parameter to define minimum support (inclusion) HOT 7
- arules 1.7.3 doesn't seem to work with R 4.2 HOT 1
- Discretize is not passing the dig.lab method to cut HOT 1
- dependency with Matrix ? HOT 1
- Least Contradiction Range incorrect HOT 3
- (can't install package) ERROR: compilation failed for package 'fim4r' HOT 8
- find rule on the right side RHS apriori vs fpgrowth HOT 7
- arulesSequences error with timeout HOT 1
- Difference between is.significant() and InterestMeasure() - with same method HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arules.