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(Instances dataset) 

Source Link

Document

Constructor copying all instances and references to the header information from the given set of instances.

Usage

From source file:distributions.NominalNominalDistribution.java

public NominalNominalDistribution(Instances inst, int kID, int pID) {
    this.inst = new Instances(inst);
    this.kID = kID;
    this.pID = pID;
    attwerten1 = new int[inst.attribute(kID).numValues()];
    attwerten2 = new int[inst.attribute(pID).numValues()];
    for (int i = 0; i < attwerten1.length; i++) {
        attwerten1[i] = i;//from   w  w  w  .j  av  a2  s. c o m
    }
    for (int i = 0; i < attwerten2.length; i++) {
        attwerten2[i] = i;
    }
    probs = new double[inst.attribute(kID).numValues()][inst.attribute(pID).numValues()];
    zaelt = new double[this.inst.attribute(kID).numValues()][inst.attribute(pID).numValues()];

}

From source file:distributions.NominalNumericDistribution.java

public NominalNumericDistribution(Instances insta, int kID, int pID) {
    super(insta);
    this.inst = new Instances(insta);
    this.kID = kID;

    attwerten = new double[inst.numInstances()];
    for (int i = 0; i < attwerten.length; i++) {
        attwerten[i] = inst.instance(i).value(pID);
    }/*from w  w  w  . jav  a  2s .c o  m*/
    attwerten = super.EliminiereDopelt(attwerten);
    attwertindexen = new int[this.inst.attribute(kID).numValues()];
    probs = new double[this.inst.attribute(kID).numValues()][attwerten.length];
    zaelt = new double[this.inst.attribute(kID).numValues()][attwerten.length];
    for (int i = 0; i < attwertindexen.length; i++) {
        attwertindexen[i] = i;
    }
    /* for (int i = 0; i < inst.numInstances(); i++) {
    attwerten[(int) inst.instance(i).value(pID)] = inst.instance(i).value(pID);
     }*/

}

From source file:distributions.NumericDistribution.java

public NumericDistribution(Instances inst, int kID) {

    this.inst = new Instances(inst);
    this.kID = kID;
    attwerten = new double[inst.numAttributes()];
    for (int i = 0, j = 0; i < attwerten.length && j < inst.numInstances(); i++, j++) {
        attwerten[i] = inst.instance(j).value(kID);
    }/* w w w  .j  a v  a 2s  .c  o m*/

    attwerten = super.EliminiereDopelt(attwerten);
    /* for (int k = 0; k < inst.numInstances(); k++) {
    attwerten[(int) inst.instance(k).value(kID)] = inst.instance(k).value(kID);
     }*/

    probs = new double[attwerten.length];

}

From source file:distributions.NumericNominalDistribution.java

public NumericNominalDistribution(Instances insta, int kID, int pID) {

    this.inst = new Instances(insta);
    this.pID = pID;
    this.kID = kID;

    attwertindexen = new double[inst.attribute(pID).numValues()];
    attwertindexen2 = new double[inst.numInstances()];
    for (int k = 0; k < attwertindexen2.length; k++) {
        attwertindexen2[k] = inst.instance(k).value(kID);
    }/*from  w  ww  .java  2  s .  c o m*/

    /*for (int i = 0; i < inst.numInstances(); i++) {
    attwertindexen2[(int) inst.instance(i).value(kID)] = inst.instance(i).value(kID);*/
    attwertindexen2 = super.EliminiereDopelt(attwertindexen2);

    probs = new double[attwertindexen2.length][inst.attribute(pID).numValues()];
    zaelt = new double[attwertindexen2.length][inst.attribute(pID).numValues()];
    for (int i = 0; i < attwertindexen.length; i++) {
        attwertindexen[i] = i;
    }

}

From source file:distributions.NumericNumericDistribution.java

public NumericNumericDistribution(Instances inst, int kID, int pID) {

    this.inst = new Instances(inst);
    attwerten1 = new double[inst.numInstances()];
    attwerten2 = new double[inst.numInstances()];
    for (int k = 0, j = 0; k < attwerten1.length && j < attwerten2.length; k++, j++) {
        attwerten2[k] = inst.instance(k).value(pID);
        attwerten1[j] = inst.instance(j).value(kID);
    }//from  w  w  w .  j a v a2s .  c o  m
    /* for (int i = 0; i < inst.numInstances(); i++) {
            
    attwerten1[(int) inst.instance(i).value(kID)] = inst.instance(i).value(kID);
            
            
    attwerten2[(int) inst.instance(i).value(pID)] = inst.instance(i).value(pID);
            
     }*/

    this.attwerten1 = super.EliminiereDopelt(this.attwerten1);
    this.attwerten2 = super.EliminiereDopelt(this.attwerten2);
    this.probs = new double[attwerten1.length][attwerten2.length];
    zaelt = new double[attwerten1.length][attwerten2.length];

}

From source file:DiversifyQuery.DivTopK.java

/**
 * Load a set of Instances from an ARFF/*w  ww . ja  v a  2s.co m*/
 *
 * @param fileName the file name of the ARFF
 * @return a set of Instances from the ARFF
 */
public static Instances loadData(String fileName) {
    Instances data = null;
    try {
        FileReader r;
        r = new FileReader(fileName);
        data = new Instances(r);

        data.setClassIndex(data.numAttributes() - 1);
    } catch (Exception e) {
        System.out.println(" Error =" + e + " in method loadData");
        e.printStackTrace();
    }
    return data;
}

From source file:edu.oregonstate.eecs.mcplan.domains.blackjack.AbstractionDiscovery.java

License:Open Source License

private static <X extends FactoredRepresentation<BlackjackState>, R extends Representer<BlackjackState, X>> void runExperiment(
        final BlackjackParameters params, final int Niterations, final int Ntrain_games,
        final int Ntrain_episodes, final int Ntest_games, final int Ntest_episodes, final File root)
        throws Exception {
    final BlackjackAggregator repr = new BlackjackAggregator();
    final BlackjackMdp mdp = new BlackjackMdp(params);
    System.out.println("Solving MDP");
    final Pair<String[][], String[][]> soln = mdp.solve();
    final String[][] hard_actions = soln.first;
    final String[][] soft_actions = soln.second;

    System.out.println("****************************************");
    System.out.println("game = " + params.max_score + " x (" + Ntrain_games + "(" + Ntrain_episodes + ")"
            + " / " + Ntest_games + "(" + Ntest_episodes + ")) " + ": " + repr);
    final Csv.Writer data_out = new Csv.Writer(new PrintStream(new File(root, "data.csv")));
    data_out.cell("abstraction").cell("game").cell("iteration").cell("Ntrain_games").cell("Ntrain_episodes")
            .cell("Ntest_games").cell("Ntest_episodes").cell("mean").cell("var").cell("conf").newline();

    final ActionGenerator<BlackjackState, JointAction<BlackjackAction>> action_gen = new BlackjackJointActionGenerator(
            1);//from   ww  w .ja  v a 2s  .c  o  m
    final Policy<BlackjackState, JointAction<BlackjackAction>> rollout_policy = new RandomPolicy<BlackjackState, JointAction<BlackjackAction>>(
            0 /*Player*/, rng.nextInt(), action_gen.create());

    final double c = 1.0;
    final int rollout_width = 1;
    final int rollout_depth = 1;
    // Optimistic default value
    final double[] default_value = new double[] { 1.0 };
    Representer<BlackjackState, ClusterAbstraction<BlackjackState>> Crepr = new TrivialClusterRepresenter(
            params, mdp.S());

    // NOTE: In the Blackjack domain, we can easily enumerate all
    // legal states, so I'm punting the issue of how to collect them
    // properly during search. In reality, there's a question of
    // whether we should be weighting them, e.g. by their reachability.
    final ArrayList<RealVector> Phi = enumerateStates(params);

    RealMatrix A0 = MatrixUtils.createRealIdentityMatrix(Phi.get(0).getDimension());

    for (int iter = 0; iter < Niterations; ++iter) {
        System.out.println("Iteration " + iter);
        //         final UnlabeledStateAccumulator<ClusterAbstraction<BlackjackState>> train_visitor
        //            = new UnlabeledStateAccumulator<ClusterAbstraction<BlackjackState>>( Crepr.create() );
        final MctsVisitor<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> train_visitor = new DefaultMctsVisitor<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>();
        final BackupRule<ClusterAbstraction<BlackjackState>, BlackjackAction> train_backup = BackupRule
                .<ClusterAbstraction<BlackjackState>, BlackjackAction>MaxQ();

        // Gather training examples
        System.out.println("Gathering training examples...");
        final SolvedStateAccumulator<HandValueAbstraction> acc = new SolvedStateAccumulator<HandValueAbstraction>(
                repr);
        for (int i = 0; i < Ntrain_games; ++i) {
            if (i % 100000 == 0) {
                System.out.println("Episode " + i);
            }

            final Deck deck = new InfiniteDeck();
            final BlackjackSimulator sim = new BlackjackSimulator(deck, 1, params);

            final GameTreeFactory<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> factory = new UctSearch.Factory<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>(
                    sim, Crepr.create(), action_gen.create(), c, Ntrain_episodes, rng, rollout_policy,
                    rollout_width, rollout_depth, train_backup, default_value);

            final SearchPolicy<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> search_policy = new SearchPolicy<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>(
                    factory, train_visitor, null) {
                @Override
                protected JointAction<BlackjackAction> selectAction(
                        final GameTree<ClusterAbstraction<BlackjackState>, BlackjackAction> tree) {
                    return BackupRules.MaxAction(tree.root()).a();
                }

                @Override
                public int hashCode() {
                    return System.identityHashCode(this);
                }

                @Override
                public boolean equals(final Object that) {
                    return this == that;
                }
            };

            final Episode<BlackjackState, BlackjackAction> episode = new Episode<BlackjackState, BlackjackAction>(
                    sim, search_policy);
            episode.addListener(acc);
            episode.run();
        }

        // Train classifier
        System.out.println("Training classifier...");
        final String algorithm = "kmeans"; //"rf";
        final boolean with_metric_learning = true;
        if ("kmeans".equals(algorithm)) {
            // final ArrayList<RealVector> Phi = train_visitor.Phi_;
            final MetricConstrainedKMeans kmeans = makeClustering(rng, A0, acc.Phi_, acc.actions_, Phi,
                    with_metric_learning);
            final VoronoiClassifier classifier = new VoronoiClassifier(kmeans.mu()) {
                @Override
                protected double distance(final RealVector x1, final RealVector x2) {
                    return kmeans.distance(x1, x2);
                }
            };
            // Update reference matrix. This has the effect of keeping some of
            // the information from previous training episodes.
            A0 = kmeans.metric.copy();
            writeClustering(kmeans, root, iter, params, hard_actions, soft_actions);
            Crepr = new ClusterRepresenter(classifier, repr.create());
        } else if ("rf".equals(algorithm)) {
            final Instances train = makeTrainingSet(acc, HandValueAbstraction.makeAttributes(params), iter);
            writeDataset(root, train);
            final Classifier classifier = makeClassifier(train);
            SerializationHelper.write(new File(root, "rf" + iter + ".model").getAbsolutePath(), classifier);
            Crepr = new PairwiseSimilarityRepresenter<BlackjackState, HandValueAbstraction>(repr.create(),
                    new Instances(train), classifier);
        }

        // Test
        System.out.println("Testing...");
        final MctsVisitor<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> test_visitor = new DefaultMctsVisitor<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>();
        final BackupRule<ClusterAbstraction<BlackjackState>, BlackjackAction> test_backup = BackupRule
                .<ClusterAbstraction<BlackjackState>, BlackjackAction>MaxQ();
        final MeanVarianceAccumulator ret = new MeanVarianceAccumulator();
        for (int i = 0; i < Ntest_games; ++i) {
            if (i % 10000 == 0) {
                System.out.println("Episode " + i);
            }

            final Deck deck = new InfiniteDeck();
            final BlackjackSimulator sim = new BlackjackSimulator(deck, 1, params);

            final GameTreeFactory<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> factory = new UctSearch.Factory<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>(
                    sim, Crepr.create(), action_gen.create(), c, Ntest_episodes, rng, rollout_policy,
                    rollout_width, rollout_depth, test_backup, default_value);

            final SearchPolicy<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction> search_policy = new SearchPolicy<BlackjackState, ClusterAbstraction<BlackjackState>, BlackjackAction>(
                    factory, test_visitor, null) {
                @Override
                protected JointAction<BlackjackAction> selectAction(
                        final GameTree<ClusterAbstraction<BlackjackState>, BlackjackAction> tree) {
                    return BackupRules.MaxAction(tree.root()).a();
                }

                @Override
                public int hashCode() {
                    return System.identityHashCode(this);
                }

                @Override
                public boolean equals(final Object that) {
                    return this == that;
                }
            };

            final Episode<BlackjackState, BlackjackAction> episode = new Episode<BlackjackState, BlackjackAction>(
                    sim, search_policy);
            episode.run();
            //         System.out.println( sim.state().token().toString() );
            //         System.out.println( "Reward: " + sim.reward()[0] );
            ret.add(sim.reward()[0]);
        }
        System.out.println("****************************************");
        System.out.println("Average return: " + ret.mean());
        System.out.println("Return variance: " + ret.variance());
        final double conf = 0.975 * ret.variance() / Math.sqrt(Ntest_games);
        System.out.println("Confidence: " + conf);
        System.out.println();
        //         data_out.println( "abstraction,game,iterations,Ntrain_games,Ntrain_episodes,Ntest_games,Ntest_episodes,mean,var,conf" );
        data_out.cell(repr).cell(params.max_score).cell(iter).cell(Ntrain_games).cell(Ntrain_episodes)
                .cell(Ntest_games).cell(Ntest_episodes).cell(ret.mean()).cell(ret.variance()).cell(conf)
                .newline();
    }
}

From source file:edu.stanford.rsl.conrad.segmentation.GridFeatureExtractor.java

License:Open Source License

public Instances loadInstances() throws IOException {
    Instances data = null;// w  ww .j a  va2s.com
    if (Configuration.getGlobalConfiguration().getRegistryEntry(RegKeys.CLASSIFIER_DATA_LOCATION) != null) {
        BufferedReader reader = new BufferedReader(new FileReader(
                Configuration.getGlobalConfiguration().getRegistryEntry(RegKeys.CLASSIFIER_DATA_LOCATION)));
        data = new Instances(reader);
        reader.close();
    }
    return data;
}

From source file:edu.umbc.cs.maple.utils.WekaUtils.java

License:Open Source License

/** Take a certain number of a set of instances.
 * @param instances//from   w  ww.  j  a  v a  2s.com
 * @param numInstances the number of instances to keep
 * @return a reduced set of instances according to the given number to keep
 */
public static Instances trimInstances(Instances instances, int numInstances) {
    Instances trimmedInstances = new Instances(instances);
    for (int i = trimmedInstances.numInstances() - 1; i >= numInstances; i--) {
        trimmedInstances.delete(i);
    }
    return trimmedInstances;
}

From source file:edu.umbc.cs.maple.utils.WekaUtils.java

License:Open Source License

/** Extract a particular subset of the instances.
 * @param instances/* w  w  w  . j  a  v  a2s . co m*/
 * @param startIdx the start instance index
 * @param numInstancesToRetrieve the number of instances to retrieve
 * @return the specified subset of the instances.
 */
public static Instances subsetInstances(Instances instances, int startIdx, int numInstancesToRetrieve) {
    double possibleNumInstancesToRetrieve = instances.numInstances() - startIdx;
    if (numInstancesToRetrieve > possibleNumInstancesToRetrieve) {
        throw new IllegalArgumentException(
                "Cannot retrieve more than " + possibleNumInstancesToRetrieve + " instances.");
    }

    int endIdx = startIdx + numInstancesToRetrieve - 1;

    // delete all instance indices outside of [startIdx, endIdx]
    Instances subset = new Instances(instances);
    for (int i = subset.numInstances() - 1; i >= 0; i--) {
        if (i < startIdx || i > endIdx)
            subset.delete(i);
    }

    return subset;
}