Example usage for weka.core Instances relationName

List of usage examples for weka.core Instances relationName

Introduction

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

Prototype


publicString relationName() 

Source Link

Document

Returns the relation's name.

Usage

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

License:Open Source License

private static void writeDataset(final File root, final Instances x) {
    final File dataset_file = new File(root, x.relationName() + ".arff");
    final Saver saver = new ArffSaver();
    try {/*from   w w w .  j  a v a 2s . c om*/
        saver.setFile(dataset_file);
        saver.setInstances(x);
        saver.writeBatch();
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
}

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;
            }//w w w.jav  a  2s. c o 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.domains.yahtzee2.subtask.StraightMdp.java

License:Open Source License

public static void main(final String[] argv) throws FileNotFoundException {
    //      final StraightMdp M = new StraightMdp( false );
    //      final YahtzeeDiceState s = new YahtzeeDiceState( new Hand( new int[] { 0, 2, 2, 1, 0, 0 } ), 1 );
    //      final KeepAction a = new KeepAction( new int[] { 0, 1, 1, 0, 0, 0 } );
    ////from ww  w  .  j a  v a  2s  . c  o  m
    //      final Pair<ArrayList<YahtzeeDiceState>, ArrayList<Double>> P = M.sparseP( s, a );
    //
    //      for( int i = 0; i < P.first.size(); ++i ) {
    //         System.out.println( P.first.get( i ) + " (" + P.second.get( i ) + ")" );
    //      }

    final RandomGenerator rng = new MersenneTwister(42);
    final double discount = 1.0;

    final boolean small = false;
    final StraightMdp mdp = new StraightMdp(small);
    final int Nfeatures = Hand.Nfaces + 1; // +1 for rerolls
    final SparseValueIterationSolver<YahtzeeDiceState, YahtzeeAction> vi = new SparseValueIterationSolver<YahtzeeDiceState, YahtzeeAction>(
            mdp, discount, 1e-16);
    vi.run();

    final ArrayList<Attribute> attr = new ArrayList<Attribute>();
    attr.addAll(YahtzeeSubtaskStateSpace.attributes());
    attr.add(WekaUtil.createNominalAttribute("__label__", mdp.A().cardinality()));
    final Instances instances = WekaUtil
            .createEmptyInstances("yahtzee_straight_" + (small ? "small" : "large") + "_pistar", attr);
    final Policy<YahtzeeDiceState, YahtzeeAction> pistar = vi.pistar();
    final Generator<YahtzeeDiceState> g = mdp.S().generator();
    while (g.hasNext()) {
        final YahtzeeDiceState s = g.next();
        if (s.isTerminal()) {
            continue;
        }
        pistar.setState(s, 0L);
        final YahtzeeAction astar = pistar.getAction();
        System.out.println("" + s + " -> " + astar);
        final double[] phi = new double[Nfeatures + 1];
        int idx = 0;
        for (int i = 0; i < Hand.Nfaces; ++i) {
            phi[idx++] = s.hand.dice[i];
        }
        phi[idx++] = s.rerolls;
        phi[Nfeatures] = mdp.A().index(astar);
        WekaUtil.addInstance(instances, new DenseInstance(1.0, phi));
    }

    WekaUtil.writeDataset(new File("."), instances);

    final Csv.Writer csv = new Csv.Writer(
            new PrintStream(new FileOutputStream(new File(instances.relationName() + "_action-key.csv"))));
    for (final Map.Entry<ValueType<int[]>, Integer> e : YahtzeeSubtaskActionSpace.index_map.entrySet()) {
        csv.cell(e.getValue()).cell(new KeepAction(e.getKey().get())).newline();
    }

    //      final MeanVarianceAccumulator ret = new MeanVarianceAccumulator();
    //      final MeanVarianceAccumulator steps = new MeanVarianceAccumulator();
    //      final int Ngames = 100000;
    //      for( int i = 0; i < Ngames; ++i ) {
    //         final FuelWorldState s0;
    //         if( choices ) {
    //            s0 = FuelWorldState.createDefaultWithChoices( rng );
    //         }
    //         else {
    //            s0 = FuelWorldState.createDefault( rng );
    //         }
    //         final FuelWorldSimulator sim = new FuelWorldSimulator( s0 );
    //
    //         final Episode<FuelWorldState, FuelWorldAction> episode
    //            = new Episode<FuelWorldState, FuelWorldAction>( sim, JointPolicy.create( pistar ) );
    //         final RewardAccumulator<FuelWorldState, FuelWorldAction> racc
    //            = new RewardAccumulator<FuelWorldState, FuelWorldAction>( sim.nagents(), discount );
    //         episode.addListener( racc );
    //
    //         final long tstart = System.nanoTime();
    //         episode.run();
    //         final long tend = System.nanoTime();
    //         final double elapsed_ms = (tend - tstart) * 1e-6;
    //
    //         ret.add( racc.v()[0] );
    //         steps.add( racc.steps() );
    //      }
    //
    //      System.out.println( "****************************************" );
    //      System.out.println( "Average return: " + ret.mean() );
    //      System.out.println( "Return variance: " + ret.variance() );
    //      System.out.println( "Confidence: " + ret.confidence() );
    //      System.out.println( "Steps (mean): " + steps.mean() );
    //      System.out.println( "Steps (var): " + steps.variance() );
}

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

License:Open Source License

/** Converts the instances in the given dataset to binary, setting the specified labels to positive.
 * Note this method is destructive to data, directly modifying its contents.
 * @param data the multiclass dataset to be converted to binary.
 * @param positiveClassValue the class value to treat as positive.
 *///from w  ww. j  av a2s. c o m
public static void convertMulticlassToBinary(Instances data, String positiveClassValue) {

    // ensure that data is nominal
    if (!data.classAttribute().isNominal())
        throw new IllegalArgumentException("Instances must have a nominal class.");

    // create the new class attribute
    FastVector newClasses = new FastVector(2);
    newClasses.addElement("Y");
    newClasses.addElement("N");
    Attribute newClassAttribute = new Attribute("class", newClasses);

    // alter the class attribute to be binary
    int newClassAttIdx = data.classIndex();
    data.insertAttributeAt(newClassAttribute, newClassAttIdx);
    int classAttIdx = data.classIndex();

    // set the instances classes to be binary, with the labels [Y,N] (indices 0 and 1 respectively)
    int numInstances = data.numInstances();
    for (int instIdx = 0; instIdx < numInstances; instIdx++) {
        Instance inst = data.instance(instIdx);
        if (inst.stringValue(classAttIdx).equals(positiveClassValue)) {
            inst.setValue(newClassAttIdx, 0); // set it to the first class, which will be Y
        } else {
            inst.setValue(newClassAttIdx, 1); // set it to the second class, which will be 0
        }
    }

    // switch the class index to the new class and delete the old class
    data.setClassIndex(newClassAttIdx);
    data.deleteAttributeAt(classAttIdx);

    // alter the dataset name
    data.setRelationName(data.relationName() + "-" + positiveClassValue);
}

From source file:jjj.asap.sas.parser.job.ImportParserData.java

License:Open Source License

private void process(final String parent, int essaySet, Map<Double, List<String>> tags,
        Map<Double, List<String>> parseTrees, Map<Double, List<String>> depends) {

    // check if output exists
    boolean any = false;

    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-extra-stats.arff"))
        any = true;// ww  w .  j a  v a  2 s  .c om
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-pos-tags.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-parse-tree.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends0.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends1.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends2.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends3.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends4.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends5.arff"))
        any = true;
    if (!IOUtils.exists("work/datasets/" + parent + "/" + essaySet + "-depends6.arff"))
        any = true;

    if (!any) {
        Job.log("NOTE", "work/datasets/" + parent + "/" + essaySet
                + "-*.arff returns all required datasets - nothing to do");
        return;
    }

    // Load an existing dataset to use as a template.
    Instances dataset = Dataset.load("work/datasets/" + parent + "/" + essaySet + "-spell-checked.arff");

    // create the output datasets here. except for the extra statistics, 
    // the format is the same as 'dataset'.

    Instances tagsData = new Instances(dataset, 0);
    tagsData.setRelationName(essaySet + "-pos-tags.arff");
    Instances treeData = new Instances(dataset, 0);
    treeData.setRelationName(essaySet + "-parse-tree.arff");

    Instances dependsData[] = new Instances[7];
    for (int j = 0; j < 7; j++) {
        dependsData[j] = new Instances(dataset, 0);
        dependsData[j].setRelationName(essaySet + "-depends" + j + ".arff");
    }

    // extra stats
    DatasetBuilder builder = new DatasetBuilder();
    builder.addVariable("id");
    if (Contest.isMultiChoice(essaySet)) {
        builder.addNominalVariable("color", Contest.COLORS);
    }
    builder.addVariable("x_sent");
    builder.addVariable("x_para");
    builder.addVariable("x_length");
    builder.addVariable("x_words");
    builder.addVariable("x_unique_words");
    builder.addNominalVariable("score", Contest.getRubrics(essaySet));

    Instances extraStats = builder.getDataset(essaySet + "-extra-stats.arff");

    // now add rows for each instance

    for (int i = 0; i < dataset.numInstances(); i++) {

        // common variables
        Instance ob = dataset.instance(i);
        double id = ob.value(0);
        String y = ob.isMissing(dataset.numAttributes() - 1) ? null
                : ob.stringValue(dataset.numAttributes() - 1);
        String color = Contest.isMultiChoice(essaySet) ? ob.stringValue(dataset.attribute("color")) : null;
        String str = ob.stringValue(dataset.attribute("text"));

        //
        // Extra stats
        //

        int nSent = tags.containsKey(id) ? tags.get(id).size() : 0;
        int nPara = 0;
        for (int a = 0; a < str.length(); a++) {
            if (str.charAt(a) == '^')
                nPara++;
        }
        int nLength = str.length();
        int nWords = 0;
        int nUniqueWords = 0;
        String[] words = str.toLowerCase().split(" ");
        nWords = words.length;
        Set<String> u = new HashSet<String>();
        for (String w : words) {
            u.add(w);
        }
        nUniqueWords = u.size();

        extraStats.add(new DenseInstance(extraStats.numAttributes()));
        Instance extra = extraStats.lastInstance();
        extra.setValue(0, id);
        if (Contest.isMultiChoice(essaySet)) {
            extra.setValue(1, color);
        }

        extra.setValue(extraStats.attribute("x_sent"), nSent);
        extra.setValue(extraStats.attribute("x_para"), nPara);
        extra.setValue(extraStats.attribute("x_length"), nLength);
        extra.setValue(extraStats.attribute("x_words"), nWords);
        extra.setValue(extraStats.attribute("x_unique_words"), nUniqueWords);

        if (y == null)
            extra.setValue(extraStats.numAttributes() - 1, Utils.missingValue());
        else
            extra.setValue(extraStats.numAttributes() - 1, y);

        //
        // POS tags
        //

        String tagsText = "";
        List<String> tagsList = tags.get(id);
        if (tagsList == null || tagsList.isEmpty()) {
            Job.log("WARNING", "no tags for " + id);
            tagsText = "x";
        } else {
            for (String tagsItem : tagsList) {
                tagsText += tagsItem;
            }
        }

        tagsData.add(new DenseInstance(ob.numAttributes()));
        Instance tagsOb = tagsData.lastInstance();
        tagsOb.setValue(0, id);
        if (Contest.isMultiChoice(essaySet)) {
            tagsOb.setValue(1, color);
            tagsOb.setValue(2, tagsText.trim());
            if (y == null) {
                tagsOb.setValue(3, Utils.missingValue());
            } else {
                tagsOb.setValue(3, y);
            }
        } else {
            tagsOb.setValue(1, tagsText.trim());
            if (y == null) {
                tagsOb.setValue(2, Utils.missingValue());
            } else {
                tagsOb.setValue(2, y);
            }
        }

        //
        // Parse Tree
        //

        String treeText = "";
        List<String> treeList = parseTrees.get(id);
        if (treeList == null || treeList.isEmpty()) {
            Job.log("WARNING", "no parse tree for " + id);
            treeText = "x";
        } else {
            for (String treeItem : treeList) {
                treeText += treeItem;
            }
        }

        treeData.add(new DenseInstance(ob.numAttributes()));
        Instance treeOb = treeData.lastInstance();
        treeOb.setValue(0, id);
        if (Contest.isMultiChoice(essaySet)) {
            treeOb.setValue(1, color);
            treeOb.setValue(2, treeText.trim());
            if (y == null) {
                treeOb.setValue(3, Utils.missingValue());
            } else {
                treeOb.setValue(3, y);
            }
        } else {
            treeOb.setValue(1, treeText.trim());
            if (y == null) {
                treeOb.setValue(2, Utils.missingValue());
            } else {
                treeOb.setValue(2, y);
            }
        }

        //
        // Depends data
        //

        for (int j = 0; j < 7; j++) {

            String text = "";
            List<String> list = depends.get(id);
            if (list == null || list.isEmpty()) {
                Job.log("WARNING", "no depends for " + id);
                text = "x";
            } else {
                for (String item : list) {
                    String[] term = StringUtils.safeSplit(item, "/", 3);
                    switch (j) {
                    case 0:
                        text += item;
                        break;
                    case 1:
                        text += term[1] + "/" + term[2];
                        break;
                    case 2:
                        text += term[0] + "/" + term[2];
                        break;
                    case 3:
                        text += term[0] + "/" + term[1];
                        break;
                    case 4:
                        text += term[0];
                        break;
                    case 5:
                        text += term[1];
                        break;
                    case 6:
                        text += term[2];
                        break;
                    }
                    text += " ";
                }
            }

            dependsData[j].add(new DenseInstance(ob.numAttributes()));
            Instance dependsOb = dependsData[j].lastInstance();
            dependsOb.setValue(0, id);
            if (Contest.isMultiChoice(essaySet)) {
                dependsOb.setValue(1, color);
                dependsOb.setValue(2, text.trim());
                if (y == null) {
                    dependsOb.setValue(3, Utils.missingValue());
                } else {
                    dependsOb.setValue(3, y);
                }
            } else {
                dependsOb.setValue(1, text.trim());
                if (y == null) {
                    dependsOb.setValue(2, Utils.missingValue());
                } else {
                    dependsOb.setValue(2, y);
                }
            }

        } // j
    } // dataset

    // Now save the new datasets

    Dataset.save("work/datasets/" + parent + "/" + tagsData.relationName(), tagsData);
    Dataset.save("work/datasets/" + parent + "/" + treeData.relationName(), treeData);
    for (int j = 0; j < 7; j++) {
        Dataset.save("work/datasets/" + parent + "/" + dependsData[j].relationName(), dependsData[j]);
    }
    Dataset.save("work/datasets/" + parent + "/" + extraStats.relationName(), extraStats);

}

From source file:jjj.asap.sas.util.Bucket.java

License:Open Source License

/**
 * Adds a dataset to a bucket. The relName is the item name.
 *///  w ww .j  ava2 s  .  co  m
public static void add(final Instances dataset, final String bucketName) {

    try {

        PrintWriter writer = new PrintWriter("buckets/datasets/" + bucketName + "/" + dataset.relationName());
        writer.println(dataset.numAttributes());
        writer.flush();
        writer.close();

    } catch (IOException e) {
        throw new RuntimeException(dataset.relationName(), e);
    }
}

From source file:lu.lippmann.cdb.lab.mds.MDSViewBuilder.java

License:Open Source License

/**
 * //  ww w  .  j a  v  a  2 s.  com
 * @param args
 * @throws Exception
 */
public static final void main(String[] args) throws Exception {

    //final String fileName = "./samples/csv/salary.csv";
    //final String fileName = "./samples/csv/uci/mushroom.csv";
    final String fileName = "./samples/csv/uci/zoo.csv";
    //final String fileName = "./samples/csv/lines.csv";
    //final String fileName = WekaDataAccessUtil.DEFAULT_SAMPLE_DIR+"csv/zoo.csv";
    //final String fileName = "./samples/csv/speech-revisited.csv";
    //final String fileName = "./samples/arff/UCI/autos.arff";

    Instances instances = WekaDataAccessUtil.loadInstancesFromARFFOrCSVFile(new File(fileName));

    //Modify instance using SHIH Algorithm
    //Shih2010 shih = new Shih2010(instances);
    //instances = shih.getModifiedInstances();

    LookAndFeelUtil.init();

    final JFrame mainFrame = new JFrame();
    mainFrame.setBackground(Color.WHITE);
    LogoHelper.setLogo(mainFrame);
    mainFrame.setTitle("MDS for '" + instances.relationName() + "'");

    int maxResult = 1000;
    final MDSResult mdsResult = ClassicMDS.doMDS(instances, MDSDistancesEnum.EUCLIDEAN, 2, maxResult, true,
            false);

    mainFrame.getContentPane().setLayout(new BorderLayout());
    mainFrame.getContentPane()
            .add(buildMDSViewFromDataSet(instances, mdsResult, maxResult, new Listener<Instances>() {
                @Override
                public void onAction(final Instances parameter) {
                    //System.out.println(parameter);      
                }
            }));

    mainFrame.setSize(1200, 900);
    mainFrame.setVisible(true);

}

From source file:mao.datamining.RemoveUselessColumnsByMissingValues.java

License:Open Source License

/**
 * Signify that this batch of input to the filter is finished.
 *
 * @return true if there are instances pending output
 * @throws Exception if no input format defined
 *///w  w  w . ja v a  2s .c o m
public boolean batchFinished() throws Exception {

    if (getInputFormat() == null) {
        throw new IllegalStateException("No input instance format defined");
    }
    if (m_removeFilter == null) {

        // establish attributes to remove from first batch

        Instances toFilter = getInputFormat();
        int[] attsToDelete = new int[toFilter.numAttributes()];
        int numToDelete = 0;
        for (int i = 0; i < toFilter.numAttributes(); i++) {
            if (i == toFilter.classIndex())
                continue; // skip class
            AttributeStats stats = toFilter.attributeStats(i);

            //remove those attributes who has high ratio of missing values
            if ((stats.missingCount * 100) / stats.totalCount > m_maxMissingPercentage) {
                //            System.out.println("stats.missingPercentage: " + (stats.missingCount*100)/stats.totalCount+"%");            
                attsToDelete[numToDelete++] = i;
            }
            //remove those columns defined in the list by manual check
            if (this.column2DeleteSet.contains(toFilter.attribute(i).name())) {
                attsToDelete[numToDelete++] = i;
            }
        }

        int[] finalAttsToDelete = new int[numToDelete];
        System.arraycopy(attsToDelete, 0, finalAttsToDelete, 0, numToDelete);

        m_removeFilter = new Remove();
        m_removeFilter.setAttributeIndicesArray(finalAttsToDelete);
        m_removeFilter.setInvertSelection(false);
        m_removeFilter.setInputFormat(toFilter);

        for (int i = 0; i < toFilter.numInstances(); i++) {
            m_removeFilter.input(toFilter.instance(i));
        }
        m_removeFilter.batchFinished();

        Instance processed;
        Instances outputDataset = m_removeFilter.getOutputFormat();

        // restore old relation name to hide attribute filter stamp
        outputDataset.setRelationName(toFilter.relationName());

        setOutputFormat(outputDataset);
        while ((processed = m_removeFilter.output()) != null) {
            processed.setDataset(outputDataset);
            push(processed);
        }
    }
    flushInput();

    m_NewBatch = true;
    return (numPendingOutput() != 0);
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * GetDataSetOptions - Look for options in the 'relationName' in format 'dataset-name: options'
 * @return   The dataset options found//from   ww w.j a v  a2 s . co  m
 */
public static final String[] getDatasetOptions(Instances instances) {
    String name = instances.relationName();
    if (name.indexOf(':') > 0) {
        return name.substring(name.indexOf(':') + 1).split(" ");
    } else
        return new String[] {};
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * GetDataSetName - Look for name in the 'relationName' in format 'dataset-name: options'
 * @return   The dataset name//from w ww  .j a  v  a 2s  . com
 */
public static final String getDatasetName(Instances instances) {
    return getRelationName(instances.relationName());
}