java - 我想在 weka java 中显示每个属性值的实例数

标签 java weka

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;

public class main {

    public static void main(String[] args) {
        String s = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                new FileReader(
                    "C:\\Program Files\\Weka-3-8\\data\\weather.numeric.arff"
                )
            );
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        ArffReader arff = null;
        try {
            arff = new ArffReader(reader);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Instances data = arff.getData();
        data.setClassIndex(data.numAttributes() - 1);
        System.out.println("The number of attributes is  : " +data.numAttributes());
        for(int i= 0; i< data.numAttributes(); i++){
            if(data.attribute(i).isNominal()){
                s = data.attribute(i).name().toString();
                System.out.println(
                    "the " + s + " attribute is nominal and takes "
                    + data.attribute(i).numValues() + " values"
                );
            }
        }
    }
}

在上面的代码中,我为每个属性显示了它所采用的值的数量,但我更想显示它所采用的值(字符串) 我正在处理文件 weather.numeric.arff。 这是文件描述:

@relation weather

@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@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

最佳答案

您可以使用以下命令打印标称属性的字符串值:data.attribute(attributeNo).value(position)

其中 attributeNo 是属性的编号,position 是一个整数,表示 .arff 中定义的标称值的位置> 文件。

例如 data.attribute(0).value(0) 是属性的外观,晴天标称值

关于java - 我想在 weka java 中显示每个属性值的实例数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922843/

相关文章:

java - 为什么在Weka中使用libsvm时出现 "NoClassDefFoundError:libsvm/svm_print_interface"错误?

machine-learning - 将验证集与交叉验证方法一起使用是否有意义?

Java Weka获取具有给定属性值的所有实例

java - 如何将一种 JSON 模式映射到另一种模式?

java - 具有默认(包)访问级别的类中方法的默认(包)和公共(public)访问级别之间有什么区别吗?

java - 如何比较两个 HashMap 以查看键是否相等?

java - 无法在 Java 代码中使用 Weka 的 PCA 过滤器

machine-learning - 如何在SVM中对不同大小的特征向量进行编码

java - 如何在 Spring bean 中使用简单的类名?

java - Eclipse 插件的跨项目版本控制