using kohonen package in knime

dear all.,

i want to use the power of Kohonen package in knime. I used the iris data for example . for learner node i used this code

library(kohonen)
DF <- data.frame(a=R[1:4],stringsAsFactors = TRUE)
x<-as.matrix(DF)
set.seed(10)
R<-xyf(data = x,Y=classvec2classmat(R$"class"),grid=somgrid(5,4,"hexagonal"))

for a predictor

library(kohonen)
R<-cbind(RDATA, predict(RMODEL, type="class")

but i found this error

package 'kohonen' was built under R version 2.13.2
> R<-cbind(RDATA, predict(RMODEL, type="class")
+ write.csv(R, "E:/tempknime/R-outDataTempFile-4552160382749656245.csv", row.names = TRUE);
Error: unexpected symbol in:
"R<-cbind(RDATA, predict(RMODEL, type="class")
write.csv"

Can anyone help me?

thanks
Execution halted

hm, there is a bracket missing.

R<-cbind(RDATA, predict(RMODEL, type="class"))

thanks ,

but i found this new error

Error in data.frame(prediction = c("Iris-setosa", "Iris-setosa", "Iris-setosa",  :
  arguments imply differing number of rows: 150, 20
Calls: cbind ... as.data.frame -> as.data.frame.list -> eval -> eval -> data.frame
Execution halted

i need to post the simple workflow or anyone found and overcome this error ?

thanks in adance for any help

Hi,

there are two issues in your code:

First, you use diferent data for training the model and for making prediction, this is the same problem as described here:

http://tech.knime.org/forum/r-statistics-nodes-and-integration/serious-bug-in-r-predictor-always-predicts-training-set-not

Second, the R Predictor expects as output a vector of predicted values. The predict() method from kohonen package returns a more complicated object, which R Predictor cannot cast into vector. You have to extract predicted values by yourself.

The correct code for your R Predictor is:

library(kohonen)
DF <- data.frame(a=RDATA[1:4],stringsAsFactors = TRUE)
x<-as.matrix(DF)
R<-predict(RMODEL,x)$prediction

 

This should work.

Best,

Dawid

dear dawid,

Thanks for your help, but i found the errors in my code . and for overcome what you noted i partitioned the set in 50% train and test.

For the code :

In the learner

library(kohonen)

x<-as.matrix(R[1:4])
y<-factor(R[[5]])
set.seed(20)
model <- xyf(scale(x),y,grid = somgrid(5, 5, "rectangular"),rlen=300)
R<-model

Is extremly important to set factor,

for the predictor

library(kohonen)
R<-cbind(RDATA, predict(RMODEL,trainY=class));

this wokrs good, and the scorer node is about 93% of accurancy.

In my hand the R learner/predicrtor nodes works well. maybe you forgot to set the path for exe.

 

thnaks for your help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.