你应该在这行 Instances instances = getArffData("E:\\Book2.arff")// 读入文件
后加入一条语句: instances.setClassIndex(instances.numAttributes() - 1)
You should set class index of for your dataset before passing it into classifier. Your classifier must know which is your outcome variable.
//Rest of your code
loader.setQuery("select * from data_training")Instances data = loader.getDataSet()//add this line here
data.setClassIndex(instances.numAttributes() - 1)
//if Hujan is your class attribute(outcome variable)
你可以用程序debug跟踪一下,以前是weka中NaiveBayesSimple类的主要函数和作用。(1) globalInfo()
返回该分类器的描述字符串.
(2) getTechnicalInformation()
返回一个TechnicalInformation类型的对象实例,包含该类的技术背景等信息.
(3) getCapabilities()
返回默认参数.
(4) BuildClassifier(Instances instances)
BuildClassifier()方法从一个训练数据集合instances构造一个分类器.求出所有名称型属性的后验概率,类属性的先验概率,数值属性的均值和方差,为后来的分类工作做准备.
(5) distributionForInstance (Instance instance)
该方法计算待分类实例instance属于各个类标的百分比,并且将各个百分比数值存于一个数组中,最后返回该数组.
(6)toString()
把分类器的参数(均值,方差,各先验概率,各后验概率)以字符串的形式返回.
(7)normalDens(double x, double mean, double stdDev)
该方法用于根据正态分布(均值为mean,方差为stdDev)计算数值型属性当属性值为x时的概率密度.
(8) getRevision()
返回程序的版本号.
(9) Main()
当类从命令行被执行时,就会调用main()方法.他只是用所给的命令行选项告诉Weka的Evaluation类来评估朴素贝叶斯,并且打印所得到的数组.完成这个功能的一行表达式包括在try-catch声明中.try-catch声明用于发现Weka例程或其他Java方法中抛出的各种异常.
这里我们要介绍一下WEKA中的术语。表格里的一个横行称作一个实例(Instance),相当于统计学中的一个样本,或者数据库中的一条记录。 竖行称作一个属性(Attrbute),相当于统计学中的一个变量,或者数据库中的一个字段。这样一个表格,或者叫数据集,在WEKA看来,呈现了属性之 间的一种关系(Relation)。图1中一共有14个实例,5个属性,关系名称为“weather”。WEKA存储数据的格式是ARFF(Attribute-Relation File Format)文件,这是一种ASCII文本文件。图1所示的二维表格存储在如下的ARFF文件中。这也就是WEKA自带的“weather.arff” 文件,在WEKA安装目录的“data”子目录下可以找到。
代码:
% ARFF file for the weather data with some numric features
%
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature real
@attribute humidity real
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
%
% 14 instances
%
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
overcast,83,86,FALSE,yes
rainy,70,96,FALSE,yes
rainy,68,80,FALSE,yes
rainy,65,70,TRUE,no
overcast,64,65,TRUE,yes
sunny,72,95,FALSE,no
sunny,69,70,FALSE,yes
rainy,75,80,FALSE,yes
sunny,75,70,TRUE,yes
overcast,72,90,TRUE,yes
overcast,81,75,FALSE,yes
rainy,71,91,TRUE,no
需要注意的是,在Windows记事本打开这个文件时,可能会因为回车符定义不一致而导致分行不正常。推荐使用UltraEdit这样的字符编辑软件察看ARFF文件的内容。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)