Hi, I want to save a RWeka model into a file, in order to retrive it latter with a load function. See this example: library(RWeka) NB <- make_Weka_classifier("weka/classifiers/bayes/NaiveBayes") model<-NB(formula,data=data,...) # does not run but you get the idea save(model,file="model.dat") # simple save R command # ... load("model.dat") # load the model from the previously saved file... model # should work but I get instead this error: Error in .jcall(x$classifier, "S", "toString") : RcallMethod: attempt to call a method of a NULL object. What is wrong and how can I solve this problem? Regards, -- Paulo Alexandre Ribeiro Cortez (PhD, MSc) Lecturer (Prof. Auxiliar) at the Department of Information Systems (DSI) University of Minho, Campus de Azur?m, 4800-058 Guimaraes, Portugal http://www.dsi.uminho.pt/~pcortez +351253510313 Fax:+351253510300
On Thu, 16 Oct 2008, Paulo Cortez wrote:> Hi, > > I want to save a RWeka model into a file, in order to retrive it latter with > a load function. > > See this example: > > library(RWeka) > > NB <- make_Weka_classifier("weka/classifiers/bayes/NaiveBayes") > model<-NB(formula,data=data,...) # does not run but you get the idea > > save(model,file="model.dat") # simple save R command > # ... > load("model.dat") # load the model from the previously saved file... > model # should work but I get instead this error: > Error in .jcall(x$classifier, "S", "toString") : > RcallMethod: attempt to call a method of a NULL object. > > What is wrong and how can I solve this problem?The R object is just a reference to the corresponding object on the Java side (in Weka). When you close R, the Java/Weka session is also closed and the model is gone. Thus, when you load the object again in a new session, you only have a reference to a Java object that does not live anymore. Z> Regards, > -- > Paulo Alexandre Ribeiro Cortez (PhD, MSc) > Lecturer (Prof. Auxiliar) at the Department of Information Systems (DSI) > University of Minho, Campus de Azur?m, 4800-058 Guimaraes, Portugal > http://www.dsi.uminho.pt/~pcortez +351253510313 Fax:+351253510300 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
Achim Zeileis wrote:> On Thu, 16 Oct 2008, Paulo Cortez wrote: > >> Hi, >> >> I want to save a RWeka model into a file, in order to retrive it >> latter with a load function. >> >> See this example: >> >> library(RWeka) >> >> NB <- make_Weka_classifier("weka/classifiers/bayes/NaiveBayes") >> model<-NB(formula,data=data,...) # does not run but you get the idea >> >> save(model,file="model.dat") # simple save R command >> # ... >> load("model.dat") # load the model from the previously saved file... >> model # should work but I get instead this error: >> Error in .jcall(x$classifier, "S", "toString") : >> RcallMethod: attempt to call a method of a NULL object. >> >> What is wrong and how can I solve this problem? > > The R object is just a reference to the corresponding object on the Java > side (in Weka). When you close R, the Java/Weka session is also closed > and the model is gone. Thus, when you load the object again in a new > session, you only have a reference to a Java object that does not live > anymore. > ZThanks, I understand now what is happening. Yet, I still need to save/load a RWeka model to/from a file. Thus, how can I do this? Is it impossible in R? Any help? Regards, -- Paulo Alexandre Ribeiro Cortez (PhD, MSc) Lecturer (Prof. Auxiliar) at the Department of Information Systems (DSI) University of Minho, Campus de Azur?m, 4800-058 Guimaraes, Portugal http://www.dsi.uminho.pt/~pcortez +351253510313 Fax:+351253510300
>>>>> Paulo Cortez writes:> Achim Zeileis wrote: >> On Thu, 16 Oct 2008, Paulo Cortez wrote: >> >>> Hi, >>> >>> I want to save a RWeka model into a file, in order to retrive it >>> latter with a load function. >>> >>> See this example: >>> >>> library(RWeka) >>> >>> NB <- make_Weka_classifier("weka/classifiers/bayes/NaiveBayes") >>> model<-NB(formula,data=data,...) # does not run but you get the idea >>> >>> save(model,file="model.dat") # simple save R command >>> # ... >>> load("model.dat") # load the model from the previously saved file... >>> model # should work but I get instead this error: >>> Error in .jcall(x$classifier, "S", "toString") : >>> RcallMethod: attempt to call a method of a NULL object. >>> >>> What is wrong and how can I solve this problem? >> >> The R object is just a reference to the corresponding object on the Java >> side (in Weka). When you close R, the Java/Weka session is also closed >> and the model is gone. Thus, when you load the object again in a new >> session, you only have a reference to a Java object that does not live >> anymore. >> Z> Thanks, I understand now what is happening.> Yet, I still need to save/load a RWeka model to/from a file. > Thus, how can I do this? Is it impossible in R? Any help?Using a current version of rJava, you can register the Java side objects for serialization using .jcache(). E.g., m1 <- J48(Species ~ ., data = iris) .jcache(m1$classifier) then save/load will work as expected. Eventually, the above may happen automagically. -k
Kurt Hornik wrote:>>>>>> Paulo Cortez writes: > Using a current version of rJava, you can register the Java side objects > for serialization using .jcache(). > > E.g., > > m1 <- J48(Species ~ ., data = iris) > .jcache(m1$classifier) > > then save/load will work as expected. > > Eventually, the above may happen automagically. > > -k >Thanks, it works quite well in my MacOS. Yet, in a linux machine (Fedora 9, R version 2.7.2, latest rJava and RWeka versions has found in the berkeley server), sometimes the save function produces a Segmentation fault. Since I am working with large datasets (email filtering), I wonder if there is any bug/limitation/problem of rJava/RWeka for large models (e.g. NaiveBayes with more than 1000 inputs). Regards, -- Paulo Alexandre Ribeiro Cortez (PhD, MSc) Lecturer (Prof. Auxiliar) at the Department of Information Systems (DSI) University of Minho, Campus de Azur?m, 4800-058 Guimaraes, Portugal http://www.dsi.uminho.pt/~pcortez +351253510313 Fax:+351253510300
Possibly Parallel Threads
- Installing RWeka package in CentOS 5: problems with JAVA?
- RStem with portuguese language
- clasificador estandar maximum likelihood
- Why use numFold in evaluate_Weka_classifier of RWeka
- Tr: Re: how to pass weka classifier options with a meta classifier in RWeka?