Example usage for weka.core Instances add

List of usage examples for weka.core Instances add

Introduction

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

Prototype

@Override
public boolean add(Instance instance) 

Source Link

Document

Adds one instance to the end of the set.

Usage

From source file:meddle.PredictByDomainOS.java

License:Open Source License

private static boolean predictOneFlow(String line, String domainOS) {
    if (!domainOSModel.containsKey(domainOS))
        return false;
    else {//from  ww w .  j  av a 2 s. c  om
        try {
            Classifier classifier = domainOSModel.get(domainOS);
            Map<String, Integer> fi = domainOSFeature.get(domainOS);
            Instances structure = domainOSStruct.get(domainOS);
            Instance current = getInstance(line, fi, fi.size());

            Instances is = new Instances(structure);
            is.setClassIndex(is.numAttributes() - 1);
            is.add(current);
            current = is.get(is.size() - 1);
            current.setClassMissing();
            double predicted = classifier.classifyInstance(current);
            if (predicted > 0) {
                return true;
            } else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:meddle.TrainModelByDomainOS.java

License:Open Source License

public static Instances populateArff(Info info, Map<String, Integer> wordCount,
        ArrayList<Map<String, Integer>> trainMatrix, ArrayList<Integer> PIILabels, int numSamples, int theta) {
    //      System.out.println(info);
    // Mapping feature_name_index
    Map<String, Integer> fi = new HashMap<String, Integer>();
    int index = 0;
    // Populate Features
    ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    int high_freq = trainMatrix.size();

    if (high_freq - theta < 30)
        theta = 0;//from www. java 2 s  .com
    for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
        // filter low frequency word
        String currentWord = entry.getKey();
        int currentWordFreq = entry.getValue();
        if (currentWordFreq < theta) {
            if (!SharedMem.wildKeys.get("android").containsKey(currentWord)
                    && !SharedMem.wildKeys.get("ios").containsKey(currentWord)
                    && !SharedMem.wildKeys.get("windows").containsKey(currentWord))
                continue;
        }
        Attribute attribute = new Attribute(currentWord);
        attributes.add(attribute);
        fi.put(currentWord, index);
        index++;
    }

    ArrayList<String> classVals = new ArrayList<String>();
    classVals.add("" + LABEL_NEGATIVE);
    classVals.add("" + LABEL_POSITIVE);
    attributes.add(new Attribute("PIILabel", classVals));

    // Populate Data Points
    Iterator<Map<String, Integer>> all = trainMatrix.iterator();
    int count = 0;
    Instances trainingInstances = new Instances("Rel", attributes, 0);
    trainingInstances.setClassIndex(trainingInstances.numAttributes() - 1);
    while (all.hasNext()) {
        Map<String, Integer> dataMap = all.next();
        double[] instanceValue = new double[attributes.size()];
        for (int i = 0; i < attributes.size() - 1; i++) {
            instanceValue[i] = 0;
        }
        int label = PIILabels.get(count);
        instanceValue[attributes.size() - 1] = label;
        for (Map.Entry<String, Integer> entry : dataMap.entrySet()) {
            if (fi.containsKey(entry.getKey())) {
                int i = fi.get(entry.getKey());
                int val = entry.getValue();
                instanceValue[i] = val;
            }
        }
        Instance data = new SparseInstance(1.0, instanceValue);
        trainingInstances.add(data);
        count++;
    }
    // Write into .arff file for persistence
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(RConfig.arffFolder + info.domainOS + ".arff"));
        bw.write(trainingInstances.toString());
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return trainingInstances;
}

From source file:meka.classifiers.multilabel.Maniac.java

License:Open Source License

@Override
public Instance transformInstance(Instance x) throws Exception {

    Instances tmpInst = new Instances(x.dataset());

    tmpInst.delete();//from  ww  w .ja v  a 2  s. co  m
    tmpInst.add(x);

    Instances features = this.extractPart(tmpInst, false);

    Instances pseudoLabels = new Instances(this.compressedTemplateInst);
    Instance tmpin = pseudoLabels.instance(0);
    pseudoLabels.delete();

    pseudoLabels.add(tmpin);

    for (int i = 0; i < pseudoLabels.classIndex(); i++) {
        pseudoLabels.instance(0).setMissing(i);
    }

    Instances newDataSet = Instances.mergeInstances(pseudoLabels, features);
    newDataSet.setClassIndex(pseudoLabels.numAttributes());

    return newDataSet.instance(0);
}

From source file:meka.classifiers.multilabel.meta.BaggingML.java

License:Open Source License

@Override
public void buildClassifier(Instances train) throws Exception {
    testCapabilities(train);/*  ww w  .j a  v  a2 s  .c om*/

    if (getDebug())
        System.out.print("-: Models: ");

    train = new Instances(train);
    m_Classifiers = ProblemTransformationMethod.makeCopies((ProblemTransformationMethod) m_Classifier,
            m_NumIterations);

    for (int i = 0; i < m_NumIterations; i++) {
        Random r = new Random(m_Seed + i);
        Instances bag = new Instances(train, 0);
        if (m_Classifiers[i] instanceof Randomizable)
            ((Randomizable) m_Classifiers[i]).setSeed(m_Seed + i);
        if (getDebug())
            System.out.print("" + i + " ");

        int ixs[] = new int[train.numInstances()];
        for (int j = 0; j < ixs.length; j++) {
            ixs[r.nextInt(ixs.length)]++;
        }
        for (int j = 0; j < ixs.length; j++) {
            if (ixs[j] > 0) {
                Instance instance = train.instance(j);
                instance.setWeight(ixs[j]);
                bag.add(instance);
            }
        }

        m_Classifiers[i].buildClassifier(bag);
    }
    if (getDebug())
        System.out.println(":-");
}

From source file:meka.classifiers.multilabel.meta.BaggingMLdup.java

License:Open Source License

@Override
public void buildClassifier(Instances train) throws Exception {
    testCapabilities(train);/*from ww w .j  ava  2 s.com*/

    if (getDebug())
        System.out.print("-: Models: ");

    //m_Classifiers = (MultilabelClassifier[]) AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);
    m_Classifiers = ProblemTransformationMethod.makeCopies((ProblemTransformationMethod) m_Classifier,
            m_NumIterations);

    for (int i = 0; i < m_NumIterations; i++) {
        Random r = new Random(m_Seed + i);
        Instances bag = new Instances(train, 0);
        if (m_Classifiers[i] instanceof Randomizable)
            ((Randomizable) m_Classifiers[i]).setSeed(m_Seed + i);
        if (getDebug())
            System.out.print("" + i + " ");

        int bag_no = (m_BagSizePercent * train.numInstances() / 100);
        //System.out.println(" bag no: "+bag_no);
        while (bag.numInstances() < bag_no) {
            bag.add(train.instance(r.nextInt(train.numInstances())));
        }
        m_Classifiers[i].buildClassifier(bag);
    }
    if (getDebug())
        System.out.println(":-");
}

From source file:meka.classifiers.multilabel.MLCBMaD.java

License:Open Source License

@Override
public Instance transformInstance(Instance x) throws Exception {
    Instances tmpInst = new Instances(x.dataset());

    tmpInst.delete();//from  w ww . j  a  v a 2 s . c o  m
    tmpInst.add(x);

    Instances features = this.extractPart(tmpInst, false);

    Instances pseudoLabels = new Instances(this.compressedMatrix);
    Instance tmpin = pseudoLabels.instance(0);
    pseudoLabels.delete();

    pseudoLabels.add(tmpin);

    for (int i = 0; i < pseudoLabels.classIndex(); i++) {
        pseudoLabels.instance(0).setMissing(i);
    }

    Instances newDataSet = Instances.mergeInstances(pseudoLabels, features);
    newDataSet.setClassIndex(this.size);

    return newDataSet.instance(0);
}

From source file:meka.classifiers.multilabel.PLST.java

License:Open Source License

/**
 * Transforms the instance in the prediction process before given to the internal multi-label
 * or multi-target classifier. The instance is passed having the original set of labels, these
 * must be replaced with the transformed labels (attributes) so that the internla classifier
 * can predict them.//ww  w .  ja va  2s  . c  o  m
 *
 * @param x The instance to transform. Consists of features and labels.
 * @return The transformed instance. Consists of features and transformed labels.
 */
@Override
public Instance transformInstance(Instance x) throws Exception {
    Instances tmpInst = new Instances(x.dataset());

    tmpInst.delete();
    tmpInst.add(x);

    Instances features = this.extractPart(tmpInst, false);

    Instances labels = new Instances(this.m_PatternInstances);

    labels.add(new DenseInstance(labels.numAttributes()));

    Instances result = Instances.mergeInstances(labels, features);

    result.setClassIndex(labels.numAttributes());

    return result.instance(0);
}

From source file:meka.classifiers.multitarget.NSR.java

License:Open Source License

public Instances convertInstances(Instances D, int L) throws Exception {

    //Gather combinations
    HashMap<String, Integer> distinctCombinations = MLUtils.classCombinationCounts(D);
    if (getDebug())
        System.out.println("Found " + distinctCombinations.size() + " unique combinations");

    //Prune combinations
    MLUtils.pruneCountHashMap(distinctCombinations, m_P);
    if (getDebug())
        System.out.println("Pruned to " + distinctCombinations.size() + " with P=" + m_P);

    // Remove all class attributes
    Instances D_ = MLUtils.deleteAttributesAt(new Instances(D), MLUtils.gen_indices(L));
    // Add a new class attribute
    D_.insertAttributeAt(new Attribute("CLASS", new ArrayList(distinctCombinations.keySet())), 0); // create the class attribute
    D_.setClassIndex(0);/*from   w  w  w  .  j av  a 2  s . c  om*/

    //Add class values
    for (int i = 0; i < D.numInstances(); i++) {
        String y = MLUtils.encodeValue(MLUtils.toIntArray(D.instance(i), L));
        // add it
        if (distinctCombinations.containsKey(y)) //if its class value exists
            D_.instance(i).setClassValue(y);
        // decomp
        else if (m_N > 0) {
            String d_subsets[] = SuperLabelUtils.getTopNSubsets(y, distinctCombinations, m_N);
            for (String s : d_subsets) {
                int w = distinctCombinations.get(s);
                Instance copy = (Instance) (D_.instance(i)).copy();
                copy.setClassValue(s);
                copy.setWeight(1.0 / d_subsets.length);
                D_.add(copy);
            }
        }
    }

    // remove with missing class
    D_.deleteWithMissingClass();

    // keep the header of new dataset for classification
    m_InstancesTemplate = new Instances(D_, 0);

    if (getDebug())
        System.out.println("" + D_);

    return D_;
}

From source file:meka.core.MatrixUtils.java

License:Open Source License

/**
 * Helper method that transforms a Matrix object to an Instances object.
 *
 * @param mat The Matrix to transform.//from   ww w.j  a v  a2  s . c o m
 * @param patternInst the Instances template to use
 * @return  The resulting Instances object.
 */
public static Instances matrixToInstances(Matrix mat, Instances patternInst) {
    Instances result = new Instances(patternInst);
    for (int i = 0; i < mat.getRowDimension(); i++) {
        double[] row = mat.getArray()[i];
        DenseInstance denseInst = new DenseInstance(1.0, row);
        result.add(denseInst);
    }

    return result;
}

From source file:meka.core.Metrics.java

License:Open Source License

/** Get Data for Plotting PR and ROC curves. */
public static Instances curveDataMacroAveraged(int Y[][], double P[][]) {

    // Note: 'Threshold' contains the probability threshold that gives rise to the previous performance values.

    Instances curveData[] = curveData(Y, P);

    int L = curveData.length;

    int noNullIndex = -1;

    for (int i = 0; i < curveData.length; i++) {
        if (curveData[i] == null) {
            L--;/*www  .  j ava2s. co  m*/
        } else {
            if (noNullIndex == -1) {
                // checking for the first curveData that is not null (=does not consist of
                // only missing values or 0s)
                noNullIndex = i;
            }

        }

    }

    Instances avgCurve = new Instances(curveData[noNullIndex], 0);
    int D = avgCurve.numAttributes();

    for (double t = 0.0; t < 1.; t += 0.01) {
        Instance x = (Instance) curveData[noNullIndex].instance(0).copy();
        //System.out.println("x1\n"+x);
        boolean firstloop = true;
        for (int j = 0; j < L; j++) {

            // if there are only missing values in a column, curveData[j] is null

            if (curveData[j] == null) {
                continue;
            }

            int i = ThresholdCurve.getThresholdInstance(curveData[j], t);
            if (firstloop) {
                // reset
                for (int a = 0; a < D; a++) {
                    x.setValue(a, curveData[j].instance(i).value(a) * 1. / L);
                }
                firstloop = false;
            } else {
                // add
                for (int a = 0; a < D; a++) {
                    double v = x.value(a);
                    x.setValue(a, v + curveData[j].instance(i).value(a) * 1. / L);
                }
            }
        }
        //System.out.println("x2\n"+x);
        avgCurve.add(x);
    }

    /*
      System.out.println(avgCurve);
      System.exit(1);
            
      // Average everything
      for (int i = 0; i < avgCurve.numInstances(); i++) {
      for(int j = 0; j < L; j++) {
      for (int a = 0; a < D; a++) {
      double o = avgCurve.instance(i).value(a);
      avgCurve.instance(i).setValue(a, o / L);
      }
      }
      }
    */
    return avgCurve;
}