Example usage for weka.core Instance value

List of usage examples for weka.core Instance value

Introduction

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

Prototype

public double value(Attribute att);

Source Link

Document

Returns an instance's attribute value in internal format.

Usage

From source file:dkpro.similarity.experiments.sts2013baseline.util.Evaluator.java

License:Open Source License

public static void runLinearRegressionCV(Mode mode, Dataset... datasets) throws Exception {
    for (Dataset dataset : datasets) {
        // Set parameters
        int folds = 10;
        Classifier baseClassifier = new LinearRegression();

        // Set up the random number generator
        long seed = new Date().getTime();
        Random random = new Random(seed);

        // Add IDs to the instances
        AddID.main(new String[] { "-i",
                MODELS_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".arff", "-o",
                MODELS_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString()
                        + "-plusIDs.arff" });

        String location = MODELS_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString()
                + "-plusIDs.arff";

        Instances data = DataSource.read(location);

        if (data == null) {
            throw new IOException("Could not load data from: " + location);
        }//from  w  w  w  .j  a va 2s  . c o  m

        data.setClassIndex(data.numAttributes() - 1);

        // Instantiate the Remove filter
        Remove removeIDFilter = new Remove();
        removeIDFilter.setAttributeIndices("first");

        // Randomize the data
        data.randomize(random);

        // Perform cross-validation
        Instances predictedData = null;
        Evaluation eval = new Evaluation(data);

        for (int n = 0; n < folds; n++) {
            Instances train = data.trainCV(folds, n, random);
            Instances test = data.testCV(folds, n);

            // Apply log filter
            Filter logFilter = new LogFilter();
            logFilter.setInputFormat(train);
            train = Filter.useFilter(train, logFilter);
            logFilter.setInputFormat(test);
            test = Filter.useFilter(test, logFilter);

            // Copy the classifier
            Classifier classifier = AbstractClassifier.makeCopy(baseClassifier);

            // Instantiate the FilteredClassifier
            FilteredClassifier filteredClassifier = new FilteredClassifier();
            filteredClassifier.setFilter(removeIDFilter);
            filteredClassifier.setClassifier(classifier);

            // Build the classifier
            filteredClassifier.buildClassifier(train);

            // Evaluate
            eval.evaluateModel(classifier, test);

            // Add predictions
            AddClassification filter = new AddClassification();
            filter.setClassifier(classifier);
            filter.setOutputClassification(true);
            filter.setOutputDistribution(false);
            filter.setOutputErrorFlag(true);
            filter.setInputFormat(train);
            Filter.useFilter(train, filter); // trains the classifier

            Instances pred = Filter.useFilter(test, filter); // performs predictions on test set
            if (predictedData == null) {
                predictedData = new Instances(pred, 0);
            }
            for (int j = 0; j < pred.numInstances(); j++) {
                predictedData.add(pred.instance(j));
            }
        }

        // Prepare output scores
        double[] scores = new double[predictedData.numInstances()];

        for (Instance predInst : predictedData) {
            int id = new Double(predInst.value(predInst.attribute(0))).intValue() - 1;

            int valueIdx = predictedData.numAttributes() - 2;

            double value = predInst.value(predInst.attribute(valueIdx));

            scores[id] = value;

            // Limit to interval [0;5]
            if (scores[id] > 5.0) {
                scores[id] = 5.0;
            }
            if (scores[id] < 0.0) {
                scores[id] = 0.0;
            }
        }

        // Output
        StringBuilder sb = new StringBuilder();
        for (Double score : scores) {
            sb.append(score.toString() + LF);
        }

        FileUtils.writeStringToFile(
                new File(OUTPUT_DIR + "/" + mode.toString().toLowerCase() + "/" + dataset.toString() + ".csv"),
                sb.toString());
    }
}

From source file:edu.drexel.psal.jstylo.verifiers.WLSVM.java

License:Open Source License

/**
 * Converts an ARFF Instance into a string in the sparse format accepted by
 * LIBSVM/*ww  w  .jav a2 s .c  om*/
 * 
 * @param instance
 * @return
 */
protected String InstanceToSparse(Instance instance) {
    String line = new String();
    int c = (int) instance.classValue();
    if (c == 0)
        c = -1;
    line = c + " ";
    for (int j = 1; j < instance.numAttributes(); j++) {
        if (j - 1 == instance.classIndex()) {
            continue;
        }
        if (instance.isMissing(j - 1))
            continue;
        if (instance.value(j - 1) != 0)
            line += " " + j + ":" + instance.value(j - 1);
    }
    // LOG.info(line); 
    return (line + "\n");
}

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

License:Open Source License

private static Fn.Function2<Boolean, Instance, Instance> createPlausiblePredicate(final Configuration config) {
    if ("fuelworld".equals(config.domain)) {
        return new Fn.Function2<Boolean, Instance, Instance>() {
            final FuelWorldState s = FuelWorldState.createDefaultWithChoices(null);

            @Override//ww  w  . j av  a2 s .  c o  m
            public Boolean apply(final Instance a, final Instance b) {
                // The same action can't put you in two different locations
                if (a.value(0) != b.value(0)) {
                    return false;
                } else if (Math.abs(a.value(1) - b.value(1)) > s.fuel_consumption
                        && !s.fuel_depots.contains((int) a.value(0))) {
                    return false;
                }

                return true;
            }
        };
    } else if ("tamarisk".equals(config.domain)) {
        return new Fn.Function2<Boolean, Instance, Instance>() {
            @Override
            public Boolean apply(final Instance a, final Instance b) {
                return true;
            }
        };
    } else if ("yahtzee".equals(config.domain)) {
        return new Fn.Function2<Boolean, Instance, Instance>() {
            @Override
            public Boolean apply(final Instance a, final Instance b) {
                final int reroll_diff = (int) (a.value(6) - b.value(6));
                if (reroll_diff != 0) {
                    return false;
                }

                for (final int i : Fn.range(7, 19)) {
                    if (a.value(i) != b.value(i)) {
                        return false;
                    }
                }

                return true;
            }
        };
    } else {
        throw new IllegalArgumentException("domain = " + config.domain);
    }
}

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

License:Open Source License

/**
 * Constructs an InformationTheoreticMetricLearner from a set of labeled
 * state vector differences.// w w w  . java  2  s . c om
 * 
 * @param config
 * @param A0
 * @param XL A labeled set of state vector differences. The label must be
 * the last attribute, and it must be 1 if the states are similar and 0
 * if they are not.
 * @return
 */
private static <A> InformationTheoreticMetricLearner learnMetric(final Configuration config,
        final RealMatrix A0, final Instances XL) {
    final int d = XL.numAttributes() - 1; //XL.get( 0 ).getDimension();
    System.out.println("d = " + d);
    final double u;
    final double ell;
    final double gamma = config.getDouble("itml.gamma");
    // We will set 'ell' and 'u' using sample quantiles as described in
    // the ITML paper.
    final QuantileAccumulator qacc = new QuantileAccumulator(0.05, 0.95);

    final ArrayList<double[]> S = new ArrayList<double[]>();
    final ArrayList<double[]> D = new ArrayList<double[]>();
    for (int i = 0; i < XL.size(); ++i) {
        final Instance ii = XL.get(i);
        final double diff[] = new double[d];
        for (int j = 0; j < d; ++j) {
            diff[j] = ii.value(j);
        }

        if (ii.classValue() == 0.0) {
            D.add(diff);
        } else {
            S.add(diff);
        }

        qacc.add(Math.sqrt(HilbertSpace.inner_prod(diff, A0, diff)));
    }
    // Set bounds to quantile esimates
    ell = qacc.estimates[0];
    u = qacc.estimates[1];
    System.out.println("ITML: ell = " + ell);
    System.out.println("ITML: u = " + u);

    final InformationTheoreticMetricLearner itml = new InformationTheoreticMetricLearner(S, D, u, ell, A0,
            gamma, config.rng);
    itml.run();
    return itml;
}

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

License:Open Source License

public static double[] unlabeledFeatures(final Instance i) {
    assert (i.dataset() != null);
    assert (i.dataset().classIndex() == i.numAttributes() - 1);
    final double[] phi = new double[i.numAttributes() - 1];
    for (int j = 0; j < i.numAttributes() - 1; ++j) {
        phi[j] = i.value(j);
    }//from  w w w  .j  av  a2  s. c  o m
    return phi;
}

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

License:Open Source License

public static Pair<ArrayList<double[]>, int[]> splitLabels(final Instances train) {
    assert (train.classAttribute() != null);

    final ArrayList<double[]> X = new ArrayList<double[]>();
    final int[] Y = new int[train.size()];

    for (int i = 0; i < train.size(); ++i) {
        final Instance inst = train.get(i);
        final double[] x = new double[train.numAttributes() - 1];
        int idx = 0;
        for (int j = 0; j < train.numAttributes(); ++j) {
            if (j == train.classIndex()) {
                Y[i] = (int) inst.classValue();
            } else {
                x[idx++] = inst.value(j);
            }//  w w w  .  j a v  a  2s  .  c o m
        }
        X.add(x);
    }

    return Pair.makePair(X, Y);
}

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

License:Open Source License

public static Instances powerSet(final Instances D, final int n) {
    final Attribute class_attr = D.classAttribute();

    final ImmutableSet.Builder<Integer> b = new ImmutableSet.Builder<Integer>();
    final int Nattr = class_attr != null ? D.numAttributes() - 1 : D.numAttributes();
    for (final int i : Fn.range(1, Nattr)) {
        b.add(i);/*from   w w  w  .j av a 2  s .  c  om*/
    }
    final Set<Set<Integer>> index = Sets.powerSet(b.build());

    final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    for (final Set<Integer> subset : index) {
        if (subset.isEmpty() || subset.size() > n) {
            continue;
        }

        final StringBuilder attr_name = new StringBuilder();
        int count = 0;
        for (final Integer i : subset) {
            if (count++ > 0) {
                attr_name.append("_x_");
            }
            attr_name.append(D.attribute(i).name());
        }

        attributes.add(new Attribute(attr_name.toString()));
    }
    if (class_attr != null) {
        assert (class_attr.isNominal());
        attributes.add(WekaUtil.createNominalAttribute(class_attr.name(), class_attr.numValues()));
    }

    final String Pname = "P" + n + "_" + D.relationName();
    final Instances P = new Instances(Pname, attributes, 0);
    if (class_attr != null) {
        P.setClassIndex(attributes.size() - 1);
    }

    for (final Instance inst : D) {
        final double[] xp = new double[attributes.size()];
        int idx = 0;
        for (final Set<Integer> subset : index) {
            if (subset.isEmpty() || subset.size() > n) {
                continue;
            }

            double p = 1.0;
            for (final Integer i : subset) {
                p *= inst.value(i);
            }
            xp[idx++] = p;
        }
        if (class_attr != null) {
            xp[idx++] = inst.classValue();
        }

        WekaUtil.addInstance(P, new DenseInstance(inst.weight(), xp));
    }

    return P;
}

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

License:Open Source License

public static Instances allPairwiseProducts(final Instances single, final boolean reflexive,
        final boolean symmetric) {
    final int c = single.classIndex();
    System.out.println("Class attribute = " + c);

    final ArrayList<Attribute> pair_attributes = new ArrayList<Attribute>();
    for (int i = 0; i < single.numAttributes(); ++i) {
        if (i == c) {
            continue;
        }//from w  ww  .  j  a  v  a  2s . c  o m
        final Attribute ai = single.attribute(i);
        final int j0 = (symmetric ? 0 : i);
        for (int j = j0; j < single.numAttributes(); ++j) {
            if (j == c) {
                continue;
            }
            if (!reflexive && i == j) {
                continue;
            }

            final Attribute aj = single.attribute(j);

            final String name = ai.name() + "_x_" + aj.name();
            pair_attributes.add(new Attribute(name));
        }
    }

    String pair_name = single.relationName();
    pair_name += "_x";
    if (reflexive) {
        pair_name += "r";
    }
    if (symmetric) {
        pair_name += "s";
    }
    pair_name += "_";
    pair_name += single.relationName();
    final Instances result = new Instances(pair_name, pair_attributes, 0);

    for (final Instance inst : single) {
        final double[] xp = new double[pair_attributes.size()];
        int idx = 0;
        for (int i = 0; i < single.numAttributes(); ++i) {
            if (i == c) {
                continue;
            }
            final double xi = inst.value(i);
            final int j0 = (symmetric ? 0 : i);
            for (int j = j0; j < single.numAttributes(); ++j) {
                if (j == c) {
                    continue;
                }
                if (!reflexive && i == j) {
                    continue;
                }
                final double xj = inst.value(j);
                xp[idx++] = xi * xj;
            }
        }
        WekaUtil.addInstance(result, new DenseInstance(inst.weight(), xp));
    }

    return result;
}

From source file:edu.oregonstate.eecs.mcplan.domains.frogger.FroggerRepresentationConverter.java

License:Open Source License

public static Instances absoluteToRelative(final FroggerParameters params, final Instances src,
        final int vision) {
    final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    attributes.add(new Attribute("x"));
    attributes.add(new Attribute("y"));
    for (int i = vision; i >= -vision; --i) {
        for (int j = -vision; j <= vision; ++j) {
            if (i == 0 && j == 0) {
                continue;
            }/*from   w  w  w .j  a v a2s. co  m*/

            final String name = "car_x" + (j >= 0 ? "+" : "") + j + "_y" + (i >= 0 ? "+" : "") + i;
            attributes.add(new Attribute(name));
        }
    }
    attributes.add(src.classAttribute());

    final Instances dest = new Instances(src.relationName() + "_relative", attributes, src.size());
    for (final Instance inst : src) {
        final double[] phi = new double[attributes.size()];
        int idx = 0;

        final int x = (int) inst.value(0);
        final int y = (int) inst.value(1);
        phi[idx++] = x;
        phi[idx++] = y;

        for (int i = vision; i >= -vision; --i) {
            for (int j = -vision; j <= vision; ++j) {
                if (i == 0 && j == 0) {
                    continue;
                }

                final int xoff = x + j;
                final int yoff = y + i;

                if (xoff >= 0 && xoff < params.road_length && yoff >= 1 && yoff <= params.lanes) {
                    final int car = (int) inst.value(2 + (yoff - 1) * params.road_length + xoff);
                    phi[idx] = car; // s.grid[dy][dx] == Tile.Car ? 1.0 : 0.0; // fv[2 + (dy-1)*road_length + dx]
                }
                idx += 1;
            }
        }

        phi[idx++] = inst.classValue();
        assert (idx == phi.length);

        WekaUtil.addInstance(dest, new DenseInstance(inst.weight(), phi));
    }

    return dest;
}

From source file:edu.oregonstate.eecs.mcplan.ml.WekaGlue.java

License:Open Source License

public static RealVector toRealVector(final Instance inst) {
    final RealVector v = new ArrayRealVector(inst.numAttributes());
    for (int i = 0; i < inst.numAttributes(); ++i) {
        v.setEntry(i, inst.value(i));
    }/*from  ww w . jav a2 s .  c om*/
    return v;
}