在看老师KNN代码时,对下面疑惑不解:
int classVal = (int) data.instance(i).classValue();
其中,data是instances(数据集)实例。分的类明明是yes/no,怎么还能强制转换?
数据集取自weka安装目录data
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature real
@attribute humidity real
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
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
此处省略N个字
weka处理离散属性时,根据arff文件的声明顺序做了个 0.0−>n.0 与属性值的映射,如上面数据
outlook {sunny=0.0, overcast=1.0, rainy=2.0}
classValue()返回的是对应的double数值。
public static void main(String[] args) throws Exception {
Instances instances=DataSource.read("E:/DataLearing/data/weather.numeric.arff");
instances.setClassIndex(instances.numAttributes()-1);
System.out.println(instances.numClasses());//输出分类类别数,即yes/no
System.out.println(instances.attribute(0).value(0));//输出第一个属性值,与上面结合可输出对应关系
System.out.println(instances.instance(0).classValue());//输出实例属性对应数值
System.out.println(instances.classAttribute().value((int) instances.instance(0).classValue()));//输出实例属性值
}