Example usage for weka.core Instance numAttributes

List of usage examples for weka.core Instance numAttributes

Introduction

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

Prototype

public int numAttributes();

Source Link

Document

Returns the number of attributes.

Usage

From source file:org.mcennis.graphrat.algorithm.clustering.WekaClassifierClusterer.java

License:Open Source License

@Override
public void execute(Graph g) {

    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    try {//  w w w  .  ja  v a  2  s . c  o m

        Clusterer clusterer = (Clusterer) ((Class) parameter.get("Clusterer").get()).newInstance();
        String[] options = ((String) parameter.get("Options").get()).split("\\s+");

        ((OptionHandler) clusterer).setOptions(options);

        Iterator<Actor> actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        Instances dataSet = null;
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));

            if (!property.getValue().isEmpty()) {

                Instance value = (Instance) property.getValue().get(0);

                if ((dataSet == null) && (value.dataset() != null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.dataset().numAttributes(); ++i) {
                        attributes.addElement(value.dataset().attribute(i));
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                } else if ((dataSet == null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.numAttributes(); ++i) {
                        Attribute element = new Attribute(Integer.toString(i));
                        attributes.addElement(element);
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                }
                dataSet.add(value);
            }

        }
        clusterer.buildClusterer(dataSet);
        actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        HashMap<Integer, Graph> clusters = new HashMap<Integer, Graph>();
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (!property.getValue().isEmpty()) {

                Instance instance = (Instance) property.getValue().get(0);
                int cluster = -1;

                try {

                    cluster = clusterer.clusterInstance(instance);
                    if (!clusters.containsKey(cluster)) {
                        Graph graph = GraphFactory.newInstance().create(AlgorithmMacros.getDestID(parameter, g,
                                (String) parameter.get("GraphID").get() + cluster), parameter);
                        clusters.put(cluster, graph);
                    }
                    clusters.get(cluster).add(a);
                } catch (Exception ex) {

                    Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE,
                            "ClusterInstance on clusterer failed", ex);

                }

                Property clusterProperty = PropertyFactory.newInstance().create("BasicProperty", AlgorithmMacros
                        .getDestID(parameter, g, (String) parameter.get("DestinationProperty").get()),
                        Integer.class);

                clusterProperty.add(new Integer(cluster));

                a.add(clusterProperty);

            }
        }

        Iterator<Graph> graphIt = clusters.values().iterator();
        while (graphIt.hasNext()) {
            LinkQuery query = (LinkQuery) parameter.get("LinkQuery").get();
            Graph graph = graphIt.next();
            Iterator<Link> link = query.executeIterator(g, graph.getActor(), graph.getActor(), null);
            while (link.hasNext()) {
                graph.add(link.next());
            }
            if ((Boolean) parameter.get("AddContext").get()) {
                TreeSet<Actor> actorSet = new TreeSet<Actor>();
                actorSet.addAll(graph.getActor());
                link = query.executeIterator(g, actorSet, null, null);
                while (link.hasNext()) {
                    Link l = link.next();
                    Actor d = l.getDestination();
                    if (graph.getActor(d.getMode(), d.getID()) == null) {
                        graph.add(d);
                    }
                    if (graph.getLink(l.getRelation(), l.getSource(), l.getDestination()) == null) {
                        graph.add(l);
                    }
                }

                link = query.executeIterator(g, null, actorSet, null);
                while (link.hasNext()) {
                    Link l = link.next();
                    Actor d = l.getSource();
                    if (graph.getActor(d.getMode(), d.getID()) == null) {
                        graph.add(d);
                    }
                    if (graph.getLink(l.getRelation(), l.getSource(), l.getDestination()) == null) {
                        graph.add(l);
                    }
                }
            }
        }

    } catch (InstantiationException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (IllegalAccessException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (Exception ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    }

}

From source file:org.mcennis.graphrat.algorithm.clustering.WekaProbablisticClusterer.java

License:Open Source License

@Override

public void execute(Graph g) {
    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    try {//from w  w w  . j av  a2 s .co m

        Clusterer clusterer = (Clusterer) ((Class) parameter.get("Clusterer").get()).newInstance();
        String[] options = ((String) parameter.get("Options").get()).split("\\s+");

        ((OptionHandler) clusterer).setOptions(options);

        Iterator<Actor> actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        Instances dataSet = null;
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));

            if (!property.getValue().isEmpty()) {

                Instance value = (Instance) property.getValue().get(0);

                if ((dataSet == null) && (value.dataset() != null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.dataset().numAttributes(); ++i) {
                        attributes.addElement(value.dataset().attribute(i));
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                } else if ((dataSet == null)) {
                    FastVector attributes = new FastVector();
                    for (int i = 0; i < value.numAttributes(); ++i) {
                        Attribute element = new Attribute(Integer.toString(i));
                        attributes.addElement(element);
                    }
                    dataSet = new Instances("Clustering", attributes, 1000);
                }
                dataSet.add(value);
            }

        }
        clusterer.buildClusterer(dataSet);
        actor = AlgorithmMacros.filterActor(parameter, g, mode, null, null);
        HashMap<Integer, Graph> clusters = new HashMap<Integer, Graph>();
        while (actor.hasNext()) {
            Actor a = actor.next();
            Property property = a.getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (!property.getValue().isEmpty()) {

                Instance instance = (Instance) property.getValue().get(0);
                double[] cluster = new double[] {};

                try {

                    cluster = clusterer.distributionForInstance(instance);
                } catch (Exception ex) {

                    Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE,
                            "ClusterInstance on clusterer failed", ex);

                }

                Property clusterProperty = PropertyFactory.newInstance().create("BasicProperty",
                        AlgorithmMacros.getDestID(parameter, g,
                                (String) parameter.get("DestinationProperty").get()),
                        (new double[] {}).getClass());

                clusterProperty.add(cluster);

                a.add(clusterProperty);

            }
        }

    } catch (InstantiationException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (IllegalAccessException ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    } catch (Exception ex) {

        Logger.getLogger(WekaClassifierClusterer.class.getName()).log(Level.SEVERE, null, ex);

    }

}

From source file:org.mcennis.graphrat.algorithm.machinelearning.BuildClassifierPerActor.java

License:Open Source License

public void execute(Graph g) {
    // construct the queries to be used

    ActorByMode groundMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    groundMode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    ActorByMode targetMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    targetMode.buildQuery((String) parameter.get("TargetMode").get(), ".*", false);

    LinkByRelation groundTruth = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
    groundTruth.buildQuery((String) parameter.get("Relation").get(), false);

    // build a list of new artists
    TreeSet<Actor> artists = new TreeSet<Actor>();
    artists.addAll(AlgorithmMacros.filterActor(parameter, g, targetMode.execute(g, artists, null)));

    // collect the instance variables from the properties to be the 

    for (Actor i : artists) {
        TreeSet<Actor> artist = new TreeSet<Actor>();
        artist.add(i);//from   w ww  . j a va2 s. c  om
        Classifier classifier = createClassifier();
        Iterator<Actor> users = AlgorithmMacros.filterActor(parameter, g, groundMode, null, null);
        Instances dataSet = null;
        boolean firstRun = true;
        while (users.hasNext()) {
            TreeSet<Actor> user = new TreeSet<Actor>();
            user.add(users.next());
            Property property = user.first().getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (property.getPropertyClass().getName().contentEquals(Instance.class.getName())) {
                List values = property.getValue();
                if (!values.isEmpty()) {
                    // get the existing instance
                    Instance object = (Instance) values.get(0);
                    if (firstRun == true) {
                        firstRun = false;
                        Instances current = object.dataset();
                        FastVector attributes = new FastVector();
                        for (int j = 0; j < current.numAttributes(); ++j) {
                            attributes.addElement(current.attribute(j));
                        }
                        Attribute classValue = new Attribute(i.getID());
                        attributes.addElement(classValue);
                        dataSet = new Instances(i.getID(), attributes, 1000);
                        dataSet.setClassIndex(dataSet.numAttributes() - 1);
                    }

                    // for every artist, create a temporary artist classifer
                    double[] content = new double[object.numAttributes() + 1];
                    for (int j = 0; j < object.numAttributes() + 1; ++j) {
                        content[j] = object.value(j);
                    }

                    Iterator<Link> link = null;
                    if ((LinkEnd) parameter.get("LinkEnd").get() == LinkEnd.SOURCE) {
                        link = AlgorithmMacros.filterLink(parameter, g, groundTruth, user, artist, null);
                    } else {
                        link = AlgorithmMacros.filterLink(parameter, g, groundTruth, artist, user, null);
                    }
                    if (link.hasNext()) {
                        content[content.length - 1] = link.next().getStrength();
                    } else if ((Boolean) parameter.get("AbsenceIsMissing").get()) {
                        content[content.length - 1] = Double.NaN;
                    } else {
                        content[content.length - 1] = 0.0;
                    }
                    Instance base = new Instance(1.0, content);
                    base.setDataset(dataSet);
                    dataSet.add(base);
                }
            }
        }
        try {
            classifier.buildClassifier(dataSet);
            Property classifierProperty = PropertyFactory.newInstance().create(
                    AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("ClassifierProperty").get()),
                    (String) parameter.get("ClassifierProperty").getType(), weka.classifiers.Classifier.class);
            classifierProperty.add(classifier);
            i.add(classifierProperty);

            Property instancesProperty = PropertyFactory.newInstance().create(
                    AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("InstancesProperty").get()),
                    (String) parameter.get("InstancesProperty").getType(), weka.core.Instances.class);
            instancesProperty.add(classifier);
            i.add(instancesProperty);
        } catch (Exception ex) {
            Logger.getLogger(BuildClassifierPerActor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.mcennis.graphrat.algorithm.machinelearning.BuildClassifierSingleAttribute.java

License:Open Source License

public void execute(Graph g) {
    // construct the queries to be used

    ActorByMode groundMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    groundMode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    ActorByMode targetMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    targetMode.buildQuery((String) parameter.get("TargetMode").get(), ".*", false);

    LinkByRelation groundTruth = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
    groundTruth.buildQuery((String) parameter.get("Relation").get(), false);

    // build a list of new artists
    TreeSet<Actor> artists = new TreeSet<Actor>();
    artists.addAll(AlgorithmMacros.filterActor(parameter, g, targetMode.execute(g, artists, null)));

    // collect the instance variables from the properties to be the 

    Classifier classifier = createClassifier();
    Iterator<Actor> users = AlgorithmMacros.filterActor(parameter, g, groundMode, null, null);
    Instances dataSet = null;//from w w  w  .j  a  v  a 2s. c  o  m
    boolean firstEntry = true;
    while (users.hasNext()) {
        TreeSet<Actor> user = new TreeSet<Actor>();
        user.add(users.next());
        Property property = user.first().getProperty(
                AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
        if (property.getPropertyClass().getName().contentEquals(Instance.class.getName())) {
            List values = property.getValue();
            if (!values.isEmpty()) {
                // get the existing instance
                Instance object = (Instance) values.get(0);
                if (firstEntry) {
                    firstEntry = false;
                    Instances current = object.dataset();
                    FastVector attributes = new FastVector();
                    for (int j = 0; j < current.numAttributes(); ++j) {
                        attributes.addElement(current.attribute(j));
                    }
                    FastVector targetNames = new FastVector();
                    Iterator<Actor> artistIt = targetMode.executeIterator(g, null, null);
                    while (artistIt.hasNext()) {
                        targetNames.addElement(artistIt.next().getID());
                    }
                    Attribute classValue = new Attribute("TargetID", targetNames);
                    attributes.addElement(classValue);
                    dataSet = new Instances("Training", attributes, 1000);
                    dataSet.setClassIndex(dataSet.numAttributes() - 1);
                }

                // for every artist, create a temporary artist classifer
                double[] content = new double[object.numAttributes() + 1];
                for (int j = 0; j < object.numAttributes() + 1; ++j) {
                    content[j] = object.value(j);
                }

                Iterator<Link> link = null;
                if ((LinkEnd) parameter.get("LinkEnd").get() == LinkEnd.SOURCE) {
                    link = AlgorithmMacros.filterLink(parameter, g, groundTruth, user, null, null);
                } else {
                    link = AlgorithmMacros.filterLink(parameter, g, groundTruth, null, user, null);
                }
                if (link.hasNext()) {
                    double strength = Double.NEGATIVE_INFINITY;
                    Actor target = null;
                    while (link.hasNext()) {
                        Link l = link.next();
                        if (l.getStrength() > strength) {
                            strength = l.getStrength();
                            if ((LinkEnd) parameter.get("LinkEnd").get() == LinkEnd.SOURCE) {
                                target = l.getDestination();
                            } else {
                                target = l.getSource();
                            }
                        }
                    }
                    content[content.length - 1] = dataSet.attribute(dataSet.numAttributes() - 1)
                            .indexOfValue(target.getID());
                } else {
                    content[content.length - 1] = Double.NaN;
                }
                Instance base = new Instance(1.0, content);
                base.setDataset(dataSet);
                dataSet.add(base);
            }
        }
    }
    try {
        classifier.buildClassifier(dataSet);
        Property classifierProperty = PropertyFactory.newInstance().create("BasicProperty",
                AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("ClassifierProperty").get()),
                weka.classifiers.Classifier.class);
        classifierProperty.add(classifier);
        g.add(classifierProperty);

        Property instancesProperty = PropertyFactory.newInstance().create("BasicProperty",
                AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("InstancesProperty").get()),
                weka.core.Instances.class);
        instancesProperty.add(classifier);
        g.add(instancesProperty);
    } catch (Exception ex) {
        Logger.getLogger(BuildClassifierSingleAttribute.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.mcennis.graphrat.algorithm.machinelearning.ClassifyPerActor.java

License:Open Source License

public void execute(Graph g) {
    // construct the queries to be used

    ActorByMode groundMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    groundMode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    ActorByMode targetMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    targetMode.buildQuery((String) parameter.get("TargetMode").get(), ".*", false);

    // build a list of new artists
    TreeSet<Actor> artists = new TreeSet<Actor>();
    artists.addAll(AlgorithmMacros.filterActor(parameter, g, targetMode.execute(g, artists, null)));

    // collect the instance variables from the properties to be the 

    for (Actor i : artists) {
        Property classifierProperty = i.getProperty(
                AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("ClassifierProperty").get()));
        if (!classifierProperty.getValue().isEmpty()) {
            Classifier classifier = (Classifier) classifierProperty.getValue().get(0);
            TreeSet<Actor> artist = new TreeSet<Actor>();
            artist.add(i);// ww  w  .  jav  a 2 s  .  co  m

            Iterator<Actor> users = AlgorithmMacros.filterActor(parameter, g, groundMode, null, null);
            Instances dataSet = null;
            boolean firstRun = true;
            while (users.hasNext()) {
                TreeSet<Actor> user = new TreeSet<Actor>();
                user.add(users.next());
                Property property = user.first().getProperty(AlgorithmMacros.getSourceID(parameter, g,
                        (String) parameter.get("SourceProperty").get()));
                if (property.getPropertyClass().getName().contentEquals(Instance.class.getName())) {
                    List values = property.getValue();
                    if (!values.isEmpty()) {
                        // get the existing instance
                        Instance object = (Instance) values.get(0);
                        if (firstRun) {
                            firstRun = false;
                            Instances current = object.dataset();
                            FastVector attributes = new FastVector();
                            for (int j = 0; j < current.numAttributes(); ++j) {
                                attributes.addElement(current.attribute(j));
                            }
                            Attribute classValue = new Attribute(i.getID());
                            attributes.addElement(classValue);
                            dataSet = new Instances(i.getID(), attributes, 1000);
                            dataSet.setClassIndex(dataSet.numAttributes() - 1);
                        }

                        // for every artist, create a temporary artist classifer
                        double[] content = new double[object.numAttributes() + 1];
                        for (int j = 0; j < object.numAttributes() + 1; ++j) {
                            content[j] = object.value(j);
                        }

                        Instance base = new Instance(1.0, content);
                        try {
                            double strength = classifier.classifyInstance(base);
                            if ((!Double.isNaN(strength)) && (strength != 0.0)) {
                                Link link = LinkFactory.newInstance()
                                        .create((String) parameter.get("Relation").get());
                                if ((LinkEnd) parameter.get("LinkEnd").get() == LinkEnd.SOURCE) {
                                    link.set(user.first(), strength, artist.first());
                                } else {
                                    link.set(artist.first(), strength, user.first());
                                }
                                g.add(link);
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(ClassifyPerActor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.mcennis.graphrat.algorithm.machinelearning.ClassifySingleAttribute.java

License:Open Source License

public void execute(Graph g) {
    // construct the queries to be used

    ActorByMode groundMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    groundMode.buildQuery((String) parameter.get("GroundMode").get(), ".*", false);

    ActorByMode targetMode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    targetMode.buildQuery((String) parameter.get("TargetMode").get(), ".*", false);

    LinkByRelation groundTruth = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
    groundTruth.buildQuery((String) parameter.get("Relation").get(), false);

    // build a list of new artists
    TreeSet<Actor> artists = new TreeSet<Actor>();
    artists.addAll(AlgorithmMacros.filterActor(parameter, g, targetMode.execute(g, artists, null)));

    // collect the instance variables from the properties to be the 

    Property classifierProperty = g.getProperty(
            AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("ClassifierProperty").get()));
    if (!classifierProperty.getValue().isEmpty()) {
        Classifier classifier = (Classifier) classifierProperty.getValue().get(0);
        Iterator<Actor> users = AlgorithmMacros.filterActor(parameter, g, groundMode, null, null);
        Instances dataSet = null;/*from  w  w w  . ja  va 2  s.c o  m*/
        boolean firstEntry = true;
        while (users.hasNext()) {
            TreeSet<Actor> user = new TreeSet<Actor>();
            user.add(users.next());
            Property property = user.first().getProperty(
                    AlgorithmMacros.getSourceID(parameter, g, (String) parameter.get("SourceProperty").get()));
            if (property.getPropertyClass().getName().contentEquals(Instance.class.getName())) {
                List values = property.getValue();
                if (!values.isEmpty()) {
                    // get the existing instance
                    Instance object = (Instance) values.get(0);
                    if (firstEntry) {
                        firstEntry = false;
                        Instances current = object.dataset();
                        FastVector attributes = new FastVector();
                        for (int j = 0; j < current.numAttributes(); ++j) {
                            attributes.addElement(current.attribute(j));
                        }
                        FastVector targetNames = new FastVector();
                        Iterator<Actor> artistIt = targetMode.executeIterator(g, null, null);
                        while (artistIt.hasNext()) {
                            targetNames.addElement(artistIt.next().getID());
                        }
                        Attribute classValue = new Attribute("TargetID", targetNames);
                        attributes.addElement(classValue);
                        dataSet = new Instances("Training", attributes, 1000);
                        dataSet.setClassIndex(dataSet.numAttributes() - 1);
                    }

                    // for every artist, create a temporary artist classifer
                    double[] content = new double[object.numAttributes() + 1];
                    for (int j = 0; j < object.numAttributes() + 1; ++j) {
                        content[j] = object.value(j);
                    }

                    Instance base = new Instance(1.0, content);
                    try {
                        double strength = classifier.classifyInstance(base);
                        if (!Double.isNaN(strength)) {
                            String id = dataSet.classAttribute().value((int) strength);
                            Actor target = g.getActor((String) parameter.get("TargetMode").get(), id);
                            Link link = LinkFactory.newInstance()
                                    .create((String) parameter.get("Relation").get());
                            if ((LinkEnd) parameter.get("LinkEnd").get() == LinkEnd.SOURCE) {
                                link.set(user.first(), strength, target);
                            } else {
                                link.set(target, strength, user.first());
                            }
                            g.add(link);
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(ClassifyPerActor.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
        }
    }
}

From source file:org.mcennis.graphrat.algorithm.reusablecores.InstanceManipulation.java

License:Open Source License

/**
 * Takes the contents of the Instance array and creates a new Instance object
 * whose attributes are the attributes of the Instance objects in sequence
 * backed by a new Dataset relfecting the new set of attributes.
 *   //from   w ww.j a  v a2  s  . c o  m
 * If there is any conflict of attribute names between the Instance objects,
 * they are duplicated in the Instance object (which may cause difficulties
 * for some machine learning algorithms.)  If this a problem, utilize 
 * normalizeFieldNames instead of concatenation.
 * 
 * If either array is null or if the length of the data and meta array do not
 * match, a new Instance object without attributes is created and returned.
 * 
 * @param data array of Instance objects 
 * @param meta array of Instances backing the data array
 * @return new Instance containing all the given data
 */
static public Instance instanceConcatenation(Instance[] data, Instances[] meta) {
    FastVector attributeVector = new FastVector();
    LinkedList<Double> values = new LinkedList<Double>();
    Instance ret = new Instance(0);
    ret.setDataset(new Instances("Default", new FastVector(), 0));
    if ((data != null) && (meta != null) && (data.length == meta.length) && (data.length > 0)) {
        for (int i = 0; i < data.length; ++i) {
            for (int j = 0; j < meta[i].numAttributes(); ++j) {
                attributeVector.addElement(meta[i].attribute(j));
                values.add(data[i].value(j));
            }
        }
        ret = new Instance(values.size());
        Iterator<Double> it = values.iterator();
        for (int i = 0; i < ret.numAttributes(); ++i) {
            ret.setValue(i, it.next());
        }
        ret.setDataset(new Instances(meta[0].relationName() + " Concatenated", attributeVector, 1));
    }
    return ret;
}

From source file:org.opentox.jaqpot3.qsar.predictor.FastRbfNnPredictor.java

License:Open Source License

private static double squaredNormDifference(Instance a, Instance b) {
    int numAttributes = a.numAttributes();
    if (numAttributes != b.numAttributes()) {
        throw new IllegalArgumentException("Provided instances of different length! "
                + "Squared Norm of the difference cannot be calculated!");
    }// w  w  w.  j  ava 2 s . c om
    double sum = 0;
    for (int i = 0; i < numAttributes; i++) {
        sum += Math.pow(a.value(i) - b.value(i), 2);
    }
    return sum;
}

From source file:org.ssase.debt.classification.OnlineMultilayerPerceptron.java

License:Open Source License

public Instances getInstances(Instance inst) {
    Instances insts;/* ww  w  . j  a v a 2  s  .  com*/
    FastVector atts = new FastVector();
    for (int i = 0; i < inst.numAttributes(); i++) {
        atts.addElement(inst.attribute(i));
    }
    insts = new Instances("CurrentTrain", atts, 0);
    insts.add(inst);
    insts.setClassIndex(inst.numAttributes() - 1);
    return insts;
}

From source file:pk.lums.edu.sma.processing.ml.DBSCAN.EuclideanDataObject.java

License:Open Source License

/**
 * Compares two DataObjects in respect to their attribute-values
 * //from ww w  .  j a v a 2 s .com
 * @param dataObject
 *            The DataObject, that is compared with this.dataObject; now
 *            assumed to be of the same type and with the same structure
 * @return Returns true, if the DataObjects correspond in each value, else
 *         returns false
 */
public boolean equals(DataObject dataObject) {
    if (this == dataObject)
        return true;

    Instance firstInstance = getInstance();
    Instance secondInstance = dataObject.getInstance();
    int firstNumValues = firstInstance.numValues();
    int secondNumValues = secondInstance.numValues();
    int numAttributes = firstInstance.numAttributes();

    int firstI, secondI;
    for (int p1 = 0, p2 = 0; p1 < firstNumValues || p2 < secondNumValues;) {
        if (p1 >= firstNumValues) {
            firstI = numAttributes;
        } else {
            firstI = firstInstance.index(p1);
        }

        if (p2 >= secondNumValues) {
            secondI = numAttributes;
        } else {
            secondI = secondInstance.index(p2);
        }

        if (firstI == secondI) {
            if (firstInstance.valueSparse(p1) != secondInstance.valueSparse(p2)) {
                return false;
            }
            p1++;
            p2++;
        } else if (firstI > secondI) {
            if (0 != secondInstance.valueSparse(p2)) {
                return false;
            }
            p2++;
        } else {
            if (0 != firstInstance.valueSparse(p1)) {
                return false;
            }
            p1++;
        }
    }
    return true;
}