Example usage for weka.core Instances setClassIndex

List of usage examples for weka.core Instances setClassIndex

Introduction

In this page you can find the example usage for weka.core Instances setClassIndex.

Prototype

public void setClassIndex(int classIndex) 

Source Link

Document

Sets the class index of the set.

Usage

From source file:fantail.algorithms.RankingByPairwiseComparison.java

License:Open Source License

public double[] recommendRanking2(Instance testInst) throws Exception {
    Instances tempData = new Instances(testInst.dataset(), 0);
    tempData.add((Instance) testInst.copy());
    // remove the relation att
    tempData.setClassIndex(-1);
    tempData.deleteAttributeAt(tempData.numAttributes() - 1);
    tempData = Filter.useFilter(tempData, m_Add);
    tempData.setClassIndex(tempData.numAttributes() - 1);
    double predRanking[] = new double[m_NumLabels];
    for (int i = 0; i < m_Classifiers.size(); i++) {
        double predIndex = m_Classifiers.get(i).classifyInstance(tempData.instance(0));
        double predProb = m_Classifiers.get(i).distributionForInstance(tempData.instance(0))[0];
        String algoPair = m_AlgoPairs.get(i);
        String[] parts = algoPair.split("\\|");
        int trueIndex = Integer.parseInt(parts[(int) predIndex]);
        predRanking[trueIndex] -= predProb;
    }//from  w  ww  . j a v  a 2s  . co m
    return Tools.doubleArrayToRanking(predRanking);
}

From source file:fantail.algorithms.RankingWithkNN.java

License:Open Source License

@Override
public void buildRanker(Instances metaData) throws Exception {

    Instances workingData = new Instances(metaData);
    workingData.setClassIndex(workingData.numAttributes() - 1);
    m_kNN = new IBkEnhanced();
    // EuclideanDistance, ChebyshevDistance, ManhattanDistance
    String ops = "-W 0 -A \"weka.core.neighboursearch.LinearNNSearch -A \\\"weka.core.EuclideanDistance -R first-last\\\"\"";
    m_kNN.setOptions(weka.core.Utils.splitOptions(ops));
    m_kNN.setKNN(m_K);//from   w  w w . j a  v  a 2 s  . c om
    m_kNN.buildClassifier(workingData);
    workingData.setClassIndex(-1);
}

From source file:fantail.core.Tools.java

License:Open Source License

public static Instances loadFantailARFFInstances(String arffPath) throws Exception {
    ArffLoader loader = new ArffLoader();
    loader.setSource(new File(arffPath));
    Instances data = loader.getDataSet();
    data.setClassIndex(data.numAttributes() - 1);
    if (data.classAttribute().isRelationValued() != true) {
        throw new Exception("The last attribute needs to be 'RelationValued'");
    }//from  w  w  w  . jav  a2s. co  m
    return data;
}

From source file:farm_ads.MyClassifier.java

public Instances readIntances(String URL) throws Exception {
    FarmAds fa = new FarmAds(URL);
    FarmAdsVector fav = new FarmAdsVector();
    fav.writeFile("data\\dataVecto.dat", fa);
    DataSource source = new DataSource("data\\dataVecto.dat");
    Instances instances = source.getDataSet();
    if (instances.classIndex() == -1) {
        instances.setClassIndex(instances.numAttributes() - 1);
    }//from ww  w.  j a  va2s.c  o  m
    return instances;
}

From source file:farm_ads.MyClassifier.java

public Instances readIntances(String URL, Hashtable att, Hashtable numAtt, String iv) throws Exception {
    FarmAds fa = new FarmAds(att, numAtt, iv, URL);
    FarmAdsVector fav = new FarmAdsVector();
    fav.writeFile("data\\dataVecto.dat", fa);
    DataSource source = new DataSource("data\\dataVecto.dat");
    Instances instances = source.getDataSet();
    if (instances.classIndex() == -1) {
        instances.setClassIndex(instances.numAttributes() - 1);
    }//from   w  ww. ja  v a2 s .c o m
    return instances;
}

From source file:farm_ads.MyClassifier.java

public Instances readIntancesVecto(String URL) throws Exception {
    DataSource source = new DataSource(URL);
    Instances instances = source.getDataSet();
    if (instances.classIndex() == -1) {
        instances.setClassIndex(instances.numAttributes() - 1);
    }/*from w ww.j  a  va 2  s  .c o  m*/
    return instances;
}

From source file:farm_ads.MyClassifier.java

public String ClassifyInstance(Classifier c, String instance) throws Exception {

    String format = "%4s %15s %15s\n";
    FarmAds fa = new FarmAds(instance, 1);
    FarmAdsVector fav = new FarmAdsVector();
    fav.writeFile("data\\dataVecto.dat", fa);
    DataSource source = new DataSource("data\\dataVecto.dat");
    Instances instances = source.getDataSet();
    if (instances.classIndex() == -1) {
        instances.setClassIndex(instances.numAttributes() - 1);
    }//from   w ww  .  jav a  2 s .co m

    String s = new String();

    s += "======= Kt qu d on qung co========\n";
    s += String.format(format, "STT", "Trc d on", "Sau d on");

    String[] classAds = { "Ph hp", "Khng Ph Hp" };
    double actValue = instances.firstInstance().classValue();

    Instance newInst = instances.firstInstance();

    double pred = c.classifyInstance(newInst);

    s += String.format(format, Integer.toString(1), classAds[(int) actValue], classAds[(int) pred]);

    if (actValue == pred) {
        s += "\n\n ==> D on ng";
    } else {
        s += "\n\n ==> D on sai";
    }

    return s;
}

From source file:FeatureSelection.ReliefFAttributeEval.java

License:Open Source License

public Instances createReliefInput(ArrayList<ArrayList<Double>> dataset, String[] featureNames_Arr,
        ArrayList<Double> labels) {

    this.featureNames_Arr = featureNames_Arr;
    // create attributes
    FastVector fv = new FastVector();
    for (int i = 0; i <= featureNames_Arr.length; i++) {
        if (i == featureNames_Arr.length) {
            fv.addElement(new Attribute("@@class@@"));
            continue;
        }/*ww w  . j  a v  a2s. c  om*/
        fv.addElement(new Attribute(featureNames_Arr[i]));
    }

    // transform dataset so that each line represents each window - add
    // class label as well
    ArrayList<ArrayList<Double>> ReliefInput = new ArrayList<ArrayList<Double>>();
    for (int i = 0; i < dataset.get(0).size(); i++) {
        ArrayList<Double> featT = new ArrayList<Double>();
        for (int j = 0; j < dataset.size(); j++) {
            featT.add(dataset.get(j).get(i));
        }
        featT.add(labels.get(i));
        ReliefInput.add(featT);
    }

    // transform dataset into Instances type
    Instances ReliefInstances = new Instances("Features", fv, dataset.size());
    for (int i = 0; i < ReliefInput.size(); i++) {
        double[] vals = CollectionUtilities.listToArray(ReliefInput.get(i));

        Instance instWeka = new Instance(vals.length);

        for (int j = 0; j < vals.length; j++) {
            instWeka.setValue(j, vals[j]);
        }
        ReliefInstances.add(instWeka);
    }
    ReliefInstances.setClassIndex(ReliefInstances.numAttributes() - 1);

    return ReliefInstances;

}

From source file:feature_construction.GeneticProgramming.java

License:LGPL

public static double[][] convertInstancesToInputFeaturesArray(String fileName) {
    // Create instances (file that contains the inputs to feed through the program)
    double[][] inputFeatures;

    try {/*from w w w.  ja  v a 2s. co  m*/
        //load CSV
        CSVLoader loaderInputs = new CSVLoader();
        loaderInputs.setSource(new File(fileName));
        Instances inputSet = loaderInputs.getDataSet();
        inputSet.setClassIndex(inputSet.numAttributes() - 1);

        inputFeatures = new double[inputSet.numInstances()][inputSet.numAttributes()];

        // Convert instances to double[][]
        for (int i = 0; i < inputSet.numInstances(); i++) {
            for (int j = 0; j < inputSet.numAttributes(); j++) {
                inputFeatures[i][j] = inputSet.get(i).value(j);
            }
        }

        return inputFeatures;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:ffnn.FFNN.java

/**
 * @param args the command line arguments
 *///from w ww .  j a  v a 2  s .  c o  m
public static void main(String[] args) throws Exception {
    FFNNTubesAI cls;
    Scanner scan = new Scanner(System.in);
    System.out.print("new / read? (n/r)");
    if (scan.next().equals("n")) {
        cls = new FFNNTubesAI();
    } else {
        cls = (FFNNTubesAI) TucilWeka.readModel();
    }
    int temp;
    Instances data = TucilWeka.readDataSet("C:\\Program Files\\Weka-3-8\\data\\Team.arff");
    //Tampilkan opsi
    for (int i = 0; i < data.numAttributes(); i++) {
        System.out.println(i + ". " + data.attribute(i));
    }
    System.out.print("Class Index : ");
    temp = scan.nextInt();
    data.setClassIndex(temp);
    data = preprocess(data);
    System.out.println(data);

    System.out.print("full train? (y/n)");
    if (scan.next().equals("y")) {
        try {
            cls.buildClassifier(data);
        } catch (Exception ex) {
            Logger.getLogger(FFNNTubesAI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    int fold = 10;

    //FFNNTubesAI.printMatrix(cls.weight1, cls.input_layer+1, cls.hidden_layer);
    //FFNNTubesAI.printMatrix(cls.weight2, cls.hidden_layer, cls.output_layer);
    //FFNNTubesAI.printMatrix(cls.bias2, 1, cls.output_layer);
    Evaluation eval = new Evaluation(data);
    System.out.print("eval/10-fold? (e/f)");
    if (scan.next().equals("e")) {
        eval.evaluateModel(cls, data);
    } else {
        eval.crossValidateModel(cls, data, fold, new Random(1));
    }
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());
}