【发布时间】:2020-03-08 17:25:35
【问题描述】:
我正在尝试对训练和测试数据集使用 NaiveBayes 函数。我正在使用这个有用的网站:https://rpubs.com/riazakhan94/naive_bayes_classifier_e1071
但是,由于某种原因它不起作用,这是我得到的错误:“表中的错误(train$Class,trainPred):所有参数必须具有相同的长度。”
这是我正在使用的代码,我猜它是一个超级简单的修复程序。数据集的 x 和 y 列在类列上进行预测:
https://github.com/samuelc12359/NaiveBayes.git
test <- read.csv(file="TestX.csv",header=FALSE)
train <- read.csv(file="TrainX.csv",header=FALSE)
Names <- c("x","y","Class")
colnames(test)<- Names
colnames(train)<- Names
NBclassfier=naiveBayes(Class~x+y, data=train)
print(NBclassfier)
trainPred=predict(NBclassfier,train, type="class")
trainTable=table(train$Class, trainPred)
testPred=predict(NBclassfier, newdata=test, type="class")
testTable=table(test$Class, testPred)
print(trainTable)
print(testTable)
【问题讨论】:
标签: r naivebayes