R NaiveBayes 与数值变量有关的问题
Posted
技术标签:
【中文标题】R NaiveBayes 与数值变量有关的问题【英文标题】:R NaiveBayes issue with numeric variables 【发布时间】:2017-09-05 00:13:38 【问题描述】:尽管 NaiveBayes() 帮助说可以在第一个参数“x”中传递数字,但我无法成功运行它。没有数字变量(转售)它工作正常。这是脚本:
library(readr)
library(klaR)
### load dataset
Dataset <- read_csv("D:/sampledata.csv")
### converting 'model' and 'type' to factor
Dataset$model <- factor(Dataset$model)
Dataset$type <- factor(Dataset$type)
### Executing NaiveBayes with numeric 'resale'
NaiveBayesModel1 <- NaiveBayes(model~type+mylogical+resale,data=Dataset,na.action =na.omit)
### now removing resale. Following works as expected.
NaiveBayesModel1 <- NaiveBayes(model~type+mylogical,data=Dataset,na.action =na.omit)
'model' 和 'type' 是因子, 'mylogical' 是一个合乎逻辑的和 “转售”是一个数值变量。 因为,我无法附加我的数据文件,所以我在这里粘贴了几行。复制这些行并将其另存为驱动器上的 sampledata.csv 文件。修改上述脚本中的 read_csv() 指向这个 csv 文件。
"model","sales","resale","type","mylogical"
"Integra",16.919,16.36,"Automobile",TRUE
"TL",39.384,19.875,"Automobile",FALSE
"Camry",247.994,13.245,"Automobile",FALSE
"Avalon",63.849,18.14,"Automobile",TRUE
"Celica",33.269,15.445,"Automobile",TRUE
"Tacoma",84.087,9.575,"Truck",TRUE
"RAV4",25.106,13.325,"Truck",FALSE
"4Runner",68.411,19.425,"Truck",FALSE
"Land Cruiser",9.835,34.08,"Truck",TRUE
"Golf",9.761,11.425,"Automobile",FALSE
"Jetta",83.721,13.24,"Automobile",FALSE
"Passat",51.102,16.725,"Automobile",TRUE
"Cabrio",9.569,16.575,"Automobile",FALSE
"GTI",5.596,13.76,"Automobile",FALSE
如果我使用“转售”运行 NaiveBayes,我会收到以下错误。
Error in if (any(temp)) stop("Zero variances for at least one class in variables: ", :
missing value where TRUE/FALSE needed
R help ( help(NaiveBayes) ) 说我可以使用数字。我不明白出了什么问题。请帮忙。
问候, 新加坡
【问题讨论】:
【参考方案1】:该错误是由 model
中每个结果的变量 resale
值的零方差引起的。您的训练集很可能包含 model
中每个不同值的单个训练记录。
【讨论】:
我现在看到了问题。每个模型的频率为 1。我用不同的数据进行了测试,并且效果很好。感谢您的帮助。以上是关于R NaiveBayes 与数值变量有关的问题的主要内容,如果未能解决你的问题,请参考以下文章