简体   繁体   中英

naive bayes classifier error in r

I use naiveBayes e1071 for classifying my data set (Classification class: "V32" 0/1).

Here is what I do:

    d <- read.table("Modeling_Data.txt",header=FALSE,sep="\t",
                    comment.char="",quote="")
    #divide into training and test data 70:30
    trainingIndex <- createDataPartition(d$V32, p=.7, list=F)
    d.training <- d[trainingIndex,]
    d.testing <- d[-trainingIndex,]
    nb.classifier <- naiveBayes(as.factor(d$V32) ~ ., data = d.training)

But I get this error:

    Error in names(dimnames(tables[[i]])) <- c(Yname, colnames(x)[i]) : 
    attempt to set an attribute on NULL
    predict(nb.classifier,d.testing[,-50000])
    Error in predict(nb.classifier, d.testing[, -50000]) : 
    object 'nb.classifier' not found

I tried to use the included the data set (iris) and everything works fine. What's wrong with my approach?

Seems like building of the model failing (and as a result the classifier is not constructed). Without looking at your data, my best guess would be that you have incomplete cases.

You could try removing cases with missing data using complete.cases as follows.

d <- read.table("Modeling_Data.txt",header=FALSE,sep="\t",comment.char="",quote="")

# remove incomplete cases
d[complete.cases(d),]

# divide into training and test data 70:30
trainingIndex <- createDataPartition(d$V32, p=.7, list=F)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM