Example usage for weka.core Instances Instances

List of usage examples for weka.core Instances Instances

Introduction

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

Prototype

public Instances(String name, ArrayList<Attribute> attInfo, int capacity) 

Source Link

Document

Creates an empty set of instances.

Usage

From source file:edu.illinois.cs.cogcomp.lbjava.learn.WekaWrapper.java

License:Open Source License

/**
 * Takes <code>attributeString</code> and initializes this wrapper's {@link #instances}
 * collection to take those attributes.// www . ja v  a  2  s .co m
 **/
public void initializeAttributes() {
    String[] atts = attributeString.split(":");

    for (int i = 0; i < atts.length; ++i) {
        String[] parts = atts[i].split("_");

        if (parts[0].equals("str")) {
            String attributeName = parts[1];
            Attribute newAttribute = new Attribute(attributeName, (FastVector) null);
            attributeInfo.addElement(newAttribute);
        } else if (parts[0].equals("nom")) {
            String[] valueStrings = parts[2].split(",");
            FastVector valueVector = new FastVector(valueStrings.length);
            for (int j = 0; j < valueStrings.length; ++j)
                valueVector.addElement(valueStrings[j]);

            Attribute a = new Attribute(parts[1], valueVector);
            attributeInfo.addElement(a);
        } else if (parts[0].equals("num")) {
            attributeInfo.addElement(new Attribute(parts[1]));
        } else {
            System.err
                    .println("WekaWrapper: Error - Malformed attribute information string: " + attributeString);
            new Exception().printStackTrace();
            System.exit(1);
        }
    }

    instances = new Instances(name, attributeInfo, 0);
    instances.setClassIndex(0);
}

From source file:edu.illinois.cs.cogcomp.lbjava.learn.WekaWrapper.java

License:Open Source License

/**
 * Destroys the learned version of the WEKA classifier and empties the {@link #instances}
 * collection of examples./*from  www. j a  va  2  s. c om*/
 **/
public void forget() {
    super.forget();

    try {
        baseClassifier = weka.classifiers.Classifier.makeCopy(freshClassifier);
    } catch (Exception e) {
        System.err.println("LBJava ERROR: WekaWrapper.forget: Can't copy classifier:");
        e.printStackTrace();
        System.exit(1);
    }

    instances = new Instances(name, attributeInfo, 0);
    instances.setClassIndex(0);
    trained = false;
}

From source file:edu.illinois.cs.cogcomp.lbjava.learn.WekaWrapper.java

License:Open Source License

/**
 * Indicates that the classifier is finished learning. This method <I>must</I> be called if the
 * WEKA classifier is to learn anything. Since WEKA classifiers cannot learn online, all of the
 * training examples must be gathered and committed to first. This method invokes the WEKA
 * classifier's <code>buildClassifier(Instances)</code> method.
 **//*from  w  ww.ja v  a  2 s  . c  o m*/
public void doneLearning() {
    if (trained) {
        System.err.println("WekaWrapper: Error - Cannot call 'doneLearning()' again without "
                + "first calling 'forget()'");
        new Exception().printStackTrace();
        System.exit(1);
    }

    /*
     * System.out.println("\nWekaWrapper Data Summary:");
     * System.out.println(instances.toSummaryString());
     */

    try {
        baseClassifier.buildClassifier(instances);
    } catch (Exception e) {
        System.err.println("WekaWrapper: Error - There was a problem building the classifier");
        if (baseClassifier == null)
            System.out.println("WekaWrapper: baseClassifier was null.");
        e.printStackTrace();
        System.exit(1);
    }

    trained = true;
    instances = new Instances(name, attributeInfo, 0);
    instances.setClassIndex(0);
}

From source file:edu.illinois.cs.cogcomp.saul.learn.SaulWekaWrapper.java

License:Open Source License

/**
 * Destroys the learned version of the WEKA classifier and empties the {@link #wekaInstances}
 * collection of wekaInstances./*from www .j  a  v a 2  s . c  o  m*/
 **/
public void forget() {
    super.forget();

    try {
        baseClassifier = weka.classifiers.Classifier.makeCopy(freshClassifier);
    } catch (Exception e) {
        System.err.println("LBJava ERROR: WekaWrapper.forget: Can't copy classifier:");
        e.printStackTrace();
        System.exit(1);
    }

    lbjavaInstances = new ArrayList<>();
    wekaInstances = new Instances(name, attributeInfo, 0);
    wekaInstances.setClassIndex(0);
    trained = false;
}

From source file:edu.illinois.cs.cogcomp.saul.learn.SaulWekaWrapper.java

License:Open Source License

private void initializeAttributes() {
    attributeInfo = new FastVector(lexicon.size() + 1);
    /*//from   ww  w  . j av a 2s .com
     * Here, we assume that if either the labels FeatureVector is empty of features, or is null,
     * then this example is to be considered unlabeled.
     */
    if (labelLexicon.size() < 1) {
        System.err.println("WekaWrapper: Error - Weka Instances may only take a single class " + "value, ");
        new Exception().printStackTrace();
        System.exit(1);
    } else {
        Feature label = labelLexicon.lookupKey(0);
        if (!label.isDiscrete()) {
            Attribute a = new Attribute(label.getStringIdentifier());
            attributeInfo.addElement(a);
        } else {
            FastVector valueVector = new FastVector(labelLexicon.size());
            for (int v = 0; v < labelLexicon.size(); v++)
                valueVector.addElement(labelLexicon.lookupKey(v).getStringValue());
            Attribute a = new Attribute(label.getGeneratingClassifier(), valueVector);
            attributeInfo.addElement(a);
        }
    }
    /*
     * Construct weka attribute for each lexicon entry.
     * If entry is discrete use a binary attribute
     * If it is real, use a numerical attribute
     */
    FastVector binaryValues = new FastVector(2);
    binaryValues.addElement("0");
    binaryValues.addElement("1");
    for (int featureIndex = 0; featureIndex < lexicon.size(); ++featureIndex) {
        Feature f = lexicon.lookupKey(featureIndex);
        Attribute a = f.isDiscrete() ? new Attribute(f.toString(), binaryValues) : new Attribute(f.toString());

        attributeInfo.addElement(a);
    }

    // The first attribute is the label
    wekaInstances = new Instances(name, attributeInfo, 0);
    wekaInstances.setClassIndex(0);
}

From source file:edu.illinois.cs.cogcomp.saul.learn.SaulWekaWrapper.java

License:Open Source License

/**
 * Indicates that the classifier is finished learning. This method <I>must</I> be called if the
 * WEKA classifier is to learn anything. Since WEKA classifiers cannot learn online, all of the
 * training lbjavaInstances must be gathered and committed to first. This method invokes the WEKA
 * classifier's <code>buildClassifier(Instances)</code> method.
 **///from   w  w w  .  java2  s . c om
public void doneLearning() {

    checkIfCanTrain();
    /*
     * System.out.println("\nWekaWrapper Data Summary:");
     * System.out.println(wekaInstances.toSummaryString());
     */

    try {
        initializeAttributes();
        for (LBJavaInstance i : lbjavaInstances)
            wekaInstances.add(makeInstance(i));
        lbjavaInstances.clear();

        baseClassifier.buildClassifier(wekaInstances);
    } catch (Exception e) {
        System.err.println("WekaWrapper: Error - There was a problem building the classifier");
        if (baseClassifier == null)
            System.out.println("WekaWrapper: baseClassifier was null.");
        e.printStackTrace();
        System.exit(1);
    }

    trained = true;
    wekaInstances = new Instances(name, attributeInfo, 0);
    wekaInstances.setClassIndex(0);
}

From source file:edu.insight.finlaw.multilabel.rough.CreateInstances.java

License:Open Source License

/**
 * Generates the Instances object and outputs it in ARFF format to stdout.
 *
 * @param args   ignored//w w  w .j  a  v a2 s  . c  om
 * @throws Exception   if generation of instances fails
 */
public static void main(String[] args) throws Exception {
    ArrayList<Attribute> atts;
    ArrayList<Attribute> attsRel;
    ArrayList<String> attVals;
    ArrayList<String> attValsRel;
    Instances data;
    Instances dataRel;
    double[] vals;
    double[] valsRel;
    int i;

    // 1. set up attributes
    atts = new ArrayList<Attribute>();
    // - numeric
    atts.add(new Attribute("att1"));
    // - nominal
    attVals = new ArrayList<String>();
    for (i = 0; i < 5; i++)
        attVals.add("val" + (i + 1));
    atts.add(new Attribute("att2", attVals));
    // - string   
    atts.add(new Attribute("att3", (ArrayList<String>) null));
    // - date
    atts.add(new Attribute("att4", "yyyy-MM-dd"));
    // - relational
    attsRel = new ArrayList<Attribute>();
    // -- numeric
    attsRel.add(new Attribute("att5.1"));
    // -- nominal
    attValsRel = new ArrayList<String>();
    for (i = 0; i < 5; i++)
        attValsRel.add("val5." + (i + 1));
    attsRel.add(new Attribute("att5.2", attValsRel));
    dataRel = new Instances("att5", attsRel, 0);
    atts.add(new Attribute("att5", dataRel, 0));

    // 2. create Instances object
    data = new Instances("MyRelation", atts, 0);

    // 3. fill with data
    // first instance
    vals = new double[data.numAttributes()];
    // - numeric
    vals[0] = Math.PI;
    // - nominal
    vals[1] = attVals.indexOf("val3");
    // - string
    vals[2] = data.attribute(2).addStringValue("This is a string!");
    // - date
    vals[3] = data.attribute(3).parseDate("2001-11-09");
    // - relational
    dataRel = new Instances(data.attribute(4).relation(), 0);
    // -- first instance
    valsRel = new double[2];
    valsRel[0] = Math.PI + 1;
    valsRel[1] = attValsRel.indexOf("val5.3");
    dataRel.add(new DenseInstance(1.0, valsRel));
    // -- second instance
    valsRel = new double[2];
    valsRel[0] = Math.PI + 2;
    valsRel[1] = attValsRel.indexOf("val5.2");
    dataRel.add(new DenseInstance(1.0, valsRel));
    vals[4] = data.attribute(4).addRelation(dataRel);
    // add
    data.add(new DenseInstance(1.0, vals));

    // second instance
    vals = new double[data.numAttributes()]; // important: needs NEW array!
    // - numeric
    vals[0] = Math.E;
    // - nominal
    vals[1] = attVals.indexOf("val1");
    // - string
    vals[2] = data.attribute(2).addStringValue("And another one!");
    // - date
    vals[3] = data.attribute(3).parseDate("2000-12-01");
    // - relational
    dataRel = new Instances(data.attribute(4).relation(), 0);
    // -- first instance
    valsRel = new double[2];
    valsRel[0] = Math.E + 1;
    valsRel[1] = attValsRel.indexOf("val5.4");
    dataRel.add(new DenseInstance(1.0, valsRel));
    // -- second instance
    valsRel = new double[2];
    valsRel[0] = Math.E + 2;
    valsRel[1] = attValsRel.indexOf("val5.1");
    dataRel.add(new DenseInstance(1.0, valsRel));
    vals[4] = data.attribute(4).addRelation(dataRel);
    // add
    data.add(new DenseInstance(1.0, vals));

    // 4. output data
    System.out.println(data);
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.AbstractionBuilder.java

License:Open Source License

private void mergeInstances(final HashMap<List<ActionNode<X, A>>, Instances> novel) {
    for (final Map.Entry<List<ActionNode<X, A>>, Instances> e : novel.entrySet()) {
        Instances local = instances_.get(e.getKey());
        if (local == null) {
            local = new Instances("foobar", attributes_, 0); // TODO: "foobar"
            instances_.put(e.getKey(), local);
        }//from   www  . j a  v  a2s . c  om
        local.addAll(e.getValue());
    }
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.EvaluateSimilarityFunction.java

License:Open Source License

public static Instances transformInstances(final Instances src, final CoordinateTransform transform) {
    final ArrayList<Attribute> out_attributes = new ArrayList<Attribute>();
    for (int i = 0; i < transform.outDimension(); ++i) {
        out_attributes.add(new Attribute("x" + i));
    }//  w w w  . j av  a2  s  .  c o  m
    out_attributes.add((Attribute) src.classAttribute().copy());
    final Instances out = new Instances(src.relationName() + "_" + transform.name(), out_attributes, 0);
    for (int i = 0; i < src.size(); ++i) {
        final Instance inst = src.get(i);
        final RealVector flat = new ArrayRealVector(WekaUtil.unlabeledFeatures(inst));
        final RealVector transformed_vector = transform.encode(flat).x;
        final double[] transformed = new double[transformed_vector.getDimension() + 1];
        for (int j = 0; j < transformed_vector.getDimension(); ++j) {
            transformed[j] = transformed_vector.getEntry(j);
        }
        transformed[transformed.length - 1] = inst.classValue();
        final Instance transformed_instance = new DenseInstance(inst.weight(), transformed);
        out.add(transformed_instance);
        transformed_instance.setDataset(out);
    }
    out.setClassIndex(out.numAttributes() - 1);
    return out;
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.Experiments.java

License:Open Source License

/**
 * Creates a labeled dataset of states pair with optimal actions. Action
 * labels are represented as indexes into an array list. Mappings in both
 * directions are also returned.//from  www  .j a  v a 2  s  .  c  om
 * @param config
 * @param attributes
 * @param data
 * @param labels
 * @param iter
 * @return
 */
private static <A extends VirtualConstructor<A>> SingleInstanceDataset<A> makeSingleInstanceDataset(
        final Configuration config, final ArrayList<Attribute> attributes, final ArrayList<double[]> data,
        final ArrayList<A> labels, final ArrayList<Pair<ArrayList<A>, TDoubleList>> qtable, final int iter) {
    //      System.out.println( "data.size() = " + data.size() );
    final int[] ii = Fn.range(0, data.size());
    Fn.shuffle(config.rng, ii);

    final HashMap<A, Integer> action_to_int = new HashMap<A, Integer>();
    final ArrayList<A> int_to_action = new ArrayList<A>();
    final ArrayList<Pair<ArrayList<A>, TDoubleList>> abridged_qtable = (qtable != null
            ? new ArrayList<Pair<ArrayList<A>, TDoubleList>>()
            : null);

    final TIntArrayList counts = new TIntArrayList();
    final int max_per_label = config.getInt("training.max_per_label");
    final int max_instances = config.getInt("training.max_single");

    final ArrayList<DenseInstance> instance_list = new ArrayList<DenseInstance>();
    for (int i = 0; i < Math.min(data.size(), max_instances); ++i) {
        final int idx = ii[i];
        final A a = labels.get(idx);
        final Integer idx_obj = action_to_int.get(a);
        final int label;
        if (idx_obj == null) {
            //            System.out.println( "\tNew action: " + a );
            label = int_to_action.size();
            int_to_action.add(a);
            action_to_int.put(a, label);
            counts.add(0);
        } else {
            //            System.out.println( "\tRepeat action: " + a );
            label = idx_obj;
        }

        final int c = counts.get(label);
        if (max_per_label <= 0 || c < max_per_label) {
            //            System.out.println( "Adding " + label );
            final double[] phi = Fn.append(data.get(idx), label);
            final DenseInstance instance = new DenseInstance(1.0, phi);
            instance_list.add(instance);
            counts.set(label, c + 1);
            if (qtable != null) {
                abridged_qtable.add(qtable.get(idx));
            }
        }
    }

    final int Nlabels = int_to_action.size();
    final ArrayList<Attribute> labeled_attributes = addLabelToAttributes(attributes, Nlabels);

    final Instances instances = new Instances(deriveDatasetName(config.training_data_single, iter),
            labeled_attributes, counts.sum());
    instances.setClassIndex(instances.numAttributes() - 1);
    for (final DenseInstance instance : instance_list) {
        instances.add(instance);
        instance.setDataset(instances);
    }

    return new SingleInstanceDataset<A>(instances, action_to_int, int_to_action, abridged_qtable);
}