将实例类型ArrayList转换为Instances对象

时间:2016-07-08 10:00:07

标签: java arraylist weka

我已经实现了ArrayList,其类型为Instance,并且通过编写一些代码,我已经从文件中读取了正面实例和负面实例,并根据其类别标签,将它们添加到相关的ArrayList。现在我想知道如何将此ArrayList的类型转换为Instances类型,然后能够使用Weka API的某些方法和类来重新取样并创建分类器?

下面的

是我的一些代码。

public class MyDecision {
 double m=0.1;
 double d=0.1;
 private ArrayList<Instance> positiveInstances;
 private ArrayList<Instance> negativeInstances;
 Filter filter;

 private double[] classlabels;
 public MyDecision() throws Exception{

       positiveInstances = new ArrayList<Instance>();
       negativeInstances = new ArrayList<Instance>();

    br = new BufferedReader(new FileReader("D:\\workspace\\MyDecision\\littleMyChurn\\Churn.arff"));
        ArffSaver saverTets = new ArffSaver();
        ArffSaver saverTraining = new ArffSaver();
        Instances trainData = new Instances(br);


     // Make the last attribute be the class
        trainData.setClassIndex(trainData.numAttributes() - 1);
        br.close();


            classlabels=trainData.attributeToDoubleArray(trainData.numAttributes()-1);

        for(int i=0;i<=trainData.numInstances()-1;i++){

            if(classlabels[i]==0){  
            positiveInstances.add(trainData.instance(i));
            }

            else if(classlabels[i]==1){             
            negativeInstances.add(trainData.instance(i));
                }
        }

        Resample resample = new Resample();

} }

1 个答案:

答案 0 :(得分:0)

尝试创建你的Arraylist:

ArrayList<Instance> positiveInstances = new ArrayList<>();