r中的朴素贝叶斯分类器错误(naive bayes classifier error in r)
我使用naiveBayes e1071对我的数据集进行分类(Classification class:“V32”0/1)。
这是我做的:
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)但我得到这个错误:
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 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 foundI tried to use the included the data set (iris) and everything works fine. What's wrong with my approach?
最满意答案
似乎构建模型失败(因此未构造分类器)。 如果不查看您的数据,我最好的猜测是您的案例不完整。
您可以尝试使用complete.cases删除缺少数据的案例,如下所示。
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)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)更多推荐
发布评论