【发布时间】:2011-12-27 06:00:31
【问题描述】:
我正在尝试在 R 中进行朴素贝叶斯分类。我在以下链接中看到了这个示例。
http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Classification/Na%C3%AFve_Bayes
那里只有 2 行。先分类,再预测。
> classifier<-naiveBayes(iris[,1:4], iris[,5])
> table(predict(classifier, iris[,-5]), iris[,5])
“虹膜数据集”上的相同代码运行良好。但是当我在我的数据集上应用同样的方法时,我遇到了一些错误。
我的数据集包含 4 个属性,第 4 个属性是类属性。
> str(data1)
'data.frame': 1370 obs. of 4 variables:
$ TenScore : num 85 84.2 67.2 91.5 79.3 ...
$ TwelthScore : num 69 87.9 67.5 82.7 72.4 ...
$ GDegreeScore : num 63.3 70.7 61.3 78.2 62.1 ...
$ Got_Admission: chr "No" "No" "No" "No" ...
所以,我尝试了这个。
> classifier<-naiveBayes(data1[,1:3], data1[,4])
> table(predict(classifier, data1[,-4]), data1[,4])
Error in table(predict(classifier, data1[, -4]), data1[, 4]) :
all arguments must have the same length
执行命令时出现上述错误。当我只使用预测时,它会给我以下输出。
> predict(classifier, data1[,-4])
factor(0)
Levels:
str(data1) 'data.frame': 1370 obs. of 4 variables:
$ TenScore : num 85 84.2 67.2 91.5 79.3 ...
$ TwelthScore : num 69 87.9 67.5 82.7 72.4 ...
$ GDegreeScore : num 63.3 70.7 61.3 78.2 62.1 ...
$ Got_Admission: chr "No" "No" "No" "No" ...
请解释一下错误是什么以及如何解决?
【问题讨论】:
-
你至少需要发布str(data1)。
-
> str(data1) 'data.frame': 1370 obs。 4 个变量:$ TenScore:数字 85 84.2 67.2 91.5 79.3 ... $ TwelthScore:数字 69 87.9 67.5 82.7 72.4 ... $ GDegreeScore:数字 63.3 70.7 61.3 78.2 62.1 ... $ Got_Admission:chr "No" "No" “不” “不” ...
标签: r classification