Example usage for weka.core Instances numAttributes

List of usage examples for weka.core Instances numAttributes

Introduction

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

Prototype


publicint numAttributes() 

Source Link

Document

Returns the number of attributes.

Usage

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

License:Open Source License

/**
 * Returns a list of all Attributes, *including* the class attribute if
 * it is set. Note that using Instance.enumerateAttributes() will *skip*
 * the class attribute./*  w w  w  .jav  a2  s.c om*/
 * @param instances
 * @return
 */
public static ArrayList<Attribute> extractAttributes(final Instances instances) {
    final ArrayList<Attribute> attributes = new ArrayList<Attribute>(instances.numAttributes());
    for (int i = 0; i < instances.numAttributes(); ++i) {
        attributes.add(instances.attribute(i));
    }
    return attributes;
}

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

License:Open Source License

/**
 * Returns a list of all Attributes, *excluding* the class attribute if
 * it is set.//from w  w  w .j  a  va2s.c o m
 * @param instances
 * @return
 */
public static ArrayList<Attribute> extractUnlabeledAttributes(final Instances instances) {
    final ArrayList<Attribute> attributes = new ArrayList<Attribute>(instances.numAttributes());
    for (int i = 0; i < instances.numAttributes(); ++i) {
        if (i == instances.classIndex()) {
            continue;
        }
        attributes.add(instances.attribute(i));
    }
    return attributes;
}

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

License:Open Source License

/**
 * Load an ARFF dataset./*from   ww  w . java2  s  . co  m*/
 *
 * Adapted from:
 * http://weka.wikispaces.com/Use+WEKA+in+your+Java+code
 * @param file
 * @return
 */
public static Instances readLabeledDataset(final File file) {
    try {
        final DataSource source = new DataSource(file.getPath());
        final Instances data = source.getDataSet();
        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }
        return data;
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

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

License:Open Source License

public static Instance labeledInstanceFromUnlabeledFeatures(final Instances headers, final double[] x) {
    assert (x.length == headers.numAttributes() - 1);
    final double[] labeled = new double[x.length + 1];
    Fn.memcpy(labeled, x, x.length);/*  w w w .  ja v  a 2  s  . co m*/
    labeled[labeled.length - 1] = Double.NaN;
    final DenseInstance inst = new DenseInstance(1.0, labeled);
    return inst;
}

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

License:Open Source License

/**
 * Adds a dummy label and converts to an Instance
 * @param headers//from  w w w  .  j  a v a2  s . com
 * @param x
 * @return
 */
public static Instance labeledInstanceFromUnlabeledFeatures(final Instances headers, final RealVector x) {
    assert (x.getDimension() == headers.numAttributes() - 1);
    final double[] labeled = new double[x.getDimension() + 1];
    for (int i = 0; i < x.getDimension(); ++i) {
        labeled[i] = x.getEntry(i);
    }
    labeled[labeled.length - 1] = Double.NaN;
    final DenseInstance inst = new DenseInstance(1.0, labeled);
    return inst;
}

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);
            }/*from  w  w w.j  a  va2  s.c om*/
        }
        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.  ja  v a2s .com*/
    }
    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;
        }/*w  ww.jav  a  2  s  . c  om*/
        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.ml.WekaGlue.java

License:Open Source License

public static SequentialProjectionHashLearner createSequentialProjectionHashLearner(final RandomGenerator rng,
        final Instances labeled, final Instances unlabeled, final int K, final double eta, final double alpha) {
    assert (labeled.classIndex() >= 0);
    final int Nfeatures = labeled.numAttributes() - 1;

    final RealMatrix X = new Array2DRowRealMatrix(Nfeatures, labeled.size() + unlabeled.size());
    final RealMatrix XL = new Array2DRowRealMatrix(Nfeatures, labeled.size() * 2);
    final RealMatrix S = new Array2DRowRealMatrix(XL.getColumnDimension(), XL.getColumnDimension());

    for (int j = 0; j < labeled.size(); ++j) {
        final Instance inst = labeled.get(j);
        for (int i = 0; i < XL.getRowDimension(); ++i) {
            X.setEntry(i, j, inst.value(i));
            XL.setEntry(i, j, inst.value(i));
        }/*from  w  ww. j a  v  a2  s  .  com*/

        int sj = -1;
        Instance s = null;
        do {
            sj = rng.nextInt(labeled.size());
            s = labeled.get(sj);
        } while (s == inst || s.classValue() != inst.classValue());
        S.setEntry(j, sj, 1);

        int dj = -1;
        Instance d = null;
        do {
            dj = rng.nextInt(labeled.size());
            d = labeled.get(dj);
        } while (d == inst || d.classValue() == inst.classValue());
        S.setEntry(j, dj, -1);
    }

    for (int j = 0; j < unlabeled.size(); ++j) {
        final Instance inst = unlabeled.get(j);
        for (int i = 0; i < X.getRowDimension(); ++i) {
            X.setEntry(i, labeled.size() + j, inst.value(i));
        }
    }

    return new SequentialProjectionHashLearner(X, XL, S, K, eta, alpha);
}

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

License:Open Source License

public void saveInstances(String s) throws IOException {
    if (Configuration.getGlobalConfiguration().getRegistryEntry(RegKeys.CLASSIFIER_DATA_LOCATION) != null) {
        BufferedWriter bw = new BufferedWriter(new FileWriter(
                Configuration.getGlobalConfiguration().getRegistryEntry(RegKeys.CLASSIFIER_DATA_LOCATION) + "_"
                        + s));//from   w  ww .j  av  a2 s.co  m
        System.out.println("Saving: " + s);

        //bw.write(getInstances().toString());

        Instances inst = getInstances();
        StringBuffer text = new StringBuffer();

        text.append("@relation").append(" ").append(Utils.quote("testing")).append("\n\n");
        for (int i = 0; i < inst.numAttributes(); i++) {
            text.append(inst.attribute(i)).append("\n");
        }
        text.append("\n").append("@data").append("\n");
        bw.write(text.toString());

        for (int i = 0; i < inst.numInstances(); i++) {
            text = new StringBuffer();
            text.append(inst.instance(i));
            if (i < inst.numInstances() - 1) {
                text.append('\n');
            }
            bw.write(text.toString());
        }
        bw.flush();
        bw.close();
        System.out.println("Done.");
    }
}