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:org.lappsgrid.eval.metric.BinaryGoldStandardAttribute.java

License:Apache License

public String getLabel(Instance instance) {
    return gs_att.value((int) instance.value(gs_att));
}

From source file:org.mcennis.graphrat.algorithm.aggregators.PropertyToLink.java

License:Open Source License

@Override
public void execute(Graph g) {
    ActorByMode mode = (ActorByMode) ActorQueryFactory.newInstance().create("ActorByMode");
    mode.buildQuery((String) parameter.get("SourceMode").get(), ".*", false);
    Iterator<Actor> actorIt = AlgorithmMacros.filterActor(parameter, g, mode, null, null);

    if (actorIt.hasNext()) {
        while (actorIt.hasNext()) {
            Actor actor = actorIt.next();
            Property linkSource = actor.getProperty(
                    AlgorithmMacros.getDestID(parameter, g, (String) parameter.get("Property").get()));
            if (linkSource != null) {
                List values = linkSource.getValue();
                if (values.size() > 0) {
                    Instance data = (Instance) values.get(0);
                    Instances meta = data.dataset();
                    TreeSet<Actor> base = new TreeSet<Actor>();
                    for (int j = 0; j < meta.numAttributes(); ++j) {
                        if (!Double.isNaN(data.value(j))) {
                            Actor destination = g.getActor((String) parameter.get("DestinationMode").get(),
                                    meta.attribute(j).name());
                            if (destination != null) {
                                base.add(destination);
                                Collection<Actor> ret = AlgorithmMacros.filterActor(parameter, g, base);
                                if (ret.size() > 0) {
                                    Link link = LinkFactory.newInstance()
                                            .create((String) parameter.get("Relation").get());
                                    link.set(actor, data.value(j), ret.iterator().next());
                                    g.add(link);
                                }// ww  w  .  j  a v  a  2s .c o  m
                                base.clear();
                            }
                        }
                    }
                } else {
                    Logger.getLogger(PropertyToLink.class.getName()).log(Level.WARNING, "Property '"
                            + linkSource.getType() + "' on actor '" + actor.getID() + "' has no value");
                }
            } else {
                Logger.getLogger(PropertyToLink.class.getName()).log(Level.WARNING,
                        "Property '" + (String) parameter.get("Property").get() + "' on actor '" + actor.getID()
                                + "' does not exist");
            }
        }
    } else {
        Logger.getLogger(PropertyToLink.class.getName()).log(Level.WARNING,
                "No actors of mode '" + (String) parameter.get("SourceMode").get() + "' were found");
    }
}

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  ww w  .j  a v  a 2 s.  c o m*/
        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;/* w  w  w. jav a2  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);
                }

                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);//from w w w.java 2 s. c o 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;//  www.ja v a2s .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.machinelearning.WekaClassifierMultiAttribute.java

License:Open Source License

@Override
public void execute(Graph g) {
    Actor[] source = g.getActor((String) parameter[1].getValue());
    if (source != null) {

        // create the atributes for each artist
        FastVector sourceTypes = new FastVector();
        Actor[] dest = g.getActor((String) parameter[3].getValue());
        if (dest != null) {
            // create the Instances set backing this object
            Instances masterSet = null;/*from   w  w w  .j a  v a  2s . co m*/
            Instance[] trainingData = new Instance[source.length];
            for (int i = 0; i < source.length; ++i) {
                // First, acquire the instance objects for each actor
                Property p = null;
                if ((Boolean) parameter[10].getValue()) {
                    p = source[i].getProperty((String) parameter[2].getValue() + g.getID());
                } else {
                    p = source[i].getProperty((String) parameter[2].getValue());
                }
                if (p != null) {
                    Object[] values = p.getValue();
                    if (values.length > 0) {
                        sourceTypes.addElement(source[i].getID());
                        trainingData[i] = (Instance) ((Instance) values[0]).copy();
                        // assume that this Instance has a backing dataset 
                        // that contains all Instance objects to be tested
                        if (masterSet == null) {
                            masterSet = new Instances(trainingData[i].dataset(), source.length);
                        }
                        masterSet.add(trainingData[i]);
                        sourceTypes.addElement(source[i].getID());
                    } else {
                        trainingData[i] = null;
                        Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                                "Actor " + source[i].getType() + ":" + source[i].getID()
                                        + " does not have an Instance value of property ID " + p.getType());
                    }
                } else {
                    trainingData[i] = null;
                    Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                            "Actor " + source[i].getType() + ":" + source[i].getID()
                                    + " does not have a property of ID " + p.getType());
                }
            }

            Vector<Attribute> destVector = new Vector<Attribute>();
            for (int i = 0; i < dest.length; ++i) {
                FastVector type = new FastVector();
                type.addElement("false");
                type.addElement("true");
                Attribute tmp = new Attribute(dest[i].getID(), type);
                destVector.add(tmp);
                masterSet.insertAttributeAt(tmp, masterSet.numAttributes());
            }
            Attribute sourceID = new Attribute("sourceID", sourceTypes);
            masterSet.insertAttributeAt(sourceID, masterSet.numAttributes());

            //set ground truth for evaluation
            for (int i = 0; i < masterSet.numInstances(); ++i) {
                Instance inst = masterSet.instance(i);
                Actor user = g.getActor((String) parameter[i].getValue(),
                        sourceID.value((int) inst.value(sourceID)));
                if (user != null) {
                    for (int j = 0; j < dest.length; ++j) {
                        if (g.getLink((String) parameter[4].getValue(), user, dest[j]) != null) {
                            inst.setValue(sourceID, "true");
                        } else {
                            if ((Boolean) parameter[9].getValue()) {
                                inst.setValue(sourceID, "false");
                            } else {
                                inst.setValue(sourceID, Double.NaN);
                            }
                        }
                    }
                } else {
                    Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE,
                            "Actor " + sourceID.value((int) inst.value(sourceID)) + " does not exist in graph");
                }
            }

            // perform cross fold evaluation of each classifier in turn
            String[] opts = ((String) parameter[9].getValue()).split("\\s+");
            Properties props = new Properties();
            if ((Boolean) parameter[11].getValue()) {
                props.setProperty("LinkType", (String) parameter[5].getValue() + g.getID());
            } else {
                props.setProperty("LinkType", (String) parameter[5].getValue());
            }
            props.setProperty("LinkClass", "Basic");
            try {
                for (int destCount = 0; destCount < dest.length; ++destCount) {
                    masterSet.setClass(destVector.get(destCount));
                    for (int i = 0; i < (Integer) parameter[8].getValue(); ++i) {
                        Instances test = masterSet.testCV((Integer) parameter[8].getValue(), i);
                        Instances train = masterSet.testCV((Integer) parameter[8].getValue(), i);
                        Classifier classifier = (Classifier) ((Class) parameter[7].getValue()).newInstance();
                        classifier.setOptions(opts);
                        classifier.buildClassifier(train);
                        for (int j = 0; j < test.numInstances(); ++j) {
                            String sourceName = sourceID.value((int) test.instance(j).value(sourceID));
                            double result = classifier.classifyInstance(test.instance(j));
                            String predicted = masterSet.classAttribute().value((int) result);
                            Link derived = LinkFactory.newInstance().create(props);
                            derived.set(g.getActor((String) parameter[2].getValue(), sourceName), 1.0,
                                    g.getActor((String) parameter[3].getValue(), predicted));
                            g.add(derived);
                        }
                    }
                }
            } catch (InstantiationException ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else { // dest==null
            Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                    "Ground truth mode '" + (String) parameter[3].getValue() + "' has no actors");
        }
    } else { // source==null
        Logger.getLogger(WekaClassifierMultiAttribute.class.getName()).log(Level.WARNING,
                "Source mode '" + (String) parameter[2].getValue() + "' has no actors");
    }
}

From source file:org.mcennis.graphrat.property.database.InstanceDB.java

License:Open Source License

public int put(Instance object) {
    int ret = -1;
    ResultSet rs = null;//ww w  . j  a  v  a2s  . c  om
    try {
        put.clearParameters();
        getID.clearParameters();
        put.setDouble(1, object.weight());
        getID.setDouble(1, object.weight());
        put.executeUpdate();
        rs = getID.executeQuery();
        if (rs.next()) {
            ret = rs.getInt("id");
            for (int i = 0; i < object.numValues(); ++i) {
                putValue.clearParameters();
                putValue.setInt(1, ret);
                putValue.setInt(2, i);
                putValue.setDouble(3, object.value(i));
                putValue.executeUpdate();
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(URLDB.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
            }
            rs = null;
        }
    }
    return ret;
}

From source file:org.openml.webapplication.algorithm.InstancesHelper.java

License:Open Source License

public static double[] predictionToConfidences(Instances dataset, Instance prediction,
        int[] att_prediction_confidence, int att_prediction) throws Exception {
    double[] confidences = new double[dataset.numClasses()];
    boolean nonNullValue = false;
    for (int i = 0; i < dataset.numClasses(); i++) {
        if (Utils.isMissingValue(prediction.value(att_prediction_confidence[i]))) {
            throw new Exception("Prediction file contains missing values for important attribute ("
                    + prediction.attribute(att_prediction_confidence[i]).name() + "). ");
        }//w ww .  j a v  a2 s .co  m
        confidences[i] = prediction.value(att_prediction_confidence[i]);
        if (confidences[i] > 0) {
            nonNullValue = true;
        }
    }

    if (nonNullValue == false) {
        confidences[(int) prediction.value(att_prediction)] = 1;
    }

    return confidences;
}

From source file:org.openml.webapplication.evaluate.EvaluateBatchPredictions.java

License:Open Source License

private void doEvaluation() throws Exception {
    // set global evaluation
    Evaluation[] e = new Evaluation[bootstrap ? 2 : 1];

    for (int i = 0; i < e.length; ++i) {
        e[i] = new Evaluation(dataset);
        if (cost_matrix != null) {
            // TODO test
            e[i] = new Evaluation(dataset, InstancesHelper.doubleToCostMatrix(cost_matrix));
        } else {//from ww w  .j  a va 2s .  c  o  m
            e[i] = new Evaluation(dataset);
        }
    }

    // set local evaluations
    for (int i = 0; i < sampleEvaluation.length; ++i) {
        for (int j = 0; j < sampleEvaluation[i].length; ++j) {
            for (int k = 0; k < sampleEvaluation[i][j].length; ++k) {
                for (int m = 0; m < (bootstrap ? 2 : 1); ++m) {
                    if (cost_matrix != null) {
                        // TODO test
                        sampleEvaluation[i][j][k][m] = new Evaluation(dataset,
                                InstancesHelper.doubleToCostMatrix(cost_matrix));
                    } else {
                        sampleEvaluation[i][j][k][m] = new Evaluation(dataset);
                    }
                }
            }
        }
    }

    for (int i = 0; i < predictions.numInstances(); i++) {
        Instance prediction = predictions.instance(i);
        int repeat = ATT_PREDICTION_REPEAT < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_REPEAT);
        int fold = ATT_PREDICTION_FOLD < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_FOLD);
        int sample = ATT_PREDICTION_SAMPLE < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_SAMPLE);
        int rowid = (int) prediction.value(ATT_PREDICTION_ROWID);

        predictionCounter.addPrediction(repeat, fold, sample, rowid);
        if (dataset.numInstances() <= rowid) {
            throw new RuntimeException("Making a prediction for row_id" + rowid
                    + " (0-based) while dataset has only " + dataset.numInstances() + " instances. ");
        }

        int bootstrap = 0;
        boolean measureGlobalScore = true;
        if (taskType == TaskType.LEARNINGCURVE && sample != predictionCounter.getSamples() - 1) {
            // for learning curves, we want the score of the last sample at global score
            measureGlobalScore = false;
        }

        if (taskType == TaskType.REGRESSION) {
            if (measureGlobalScore) {
                e[bootstrap].evaluateModelOnce(prediction.value(ATT_PREDICTION_PREDICTION),
                        dataset.instance(rowid));
            }
            sampleEvaluation[repeat][fold][sample][bootstrap]
                    .evaluateModelOnce(prediction.value(ATT_PREDICTION_PREDICTION), dataset.instance(rowid));
        } else {
            // TODO: catch error when no prob distribution is provided
            double[] confidences = InstancesHelper.predictionToConfidences(dataset, prediction,
                    ATT_PREDICTION_CONFIDENCE, ATT_PREDICTION_PREDICTION);

            if (measureGlobalScore) {
                e[bootstrap].evaluateModelOnceAndRecordPrediction(confidences, dataset.instance(rowid));
            }
            sampleEvaluation[repeat][fold][sample][bootstrap].evaluateModelOnceAndRecordPrediction(confidences,
                    dataset.instance(rowid));
        }
    }

    if (predictionCounter.check() == false) {
        throw new RuntimeException("Prediction count does not match: " + predictionCounter.getErrorMessage());
    }

    List<EvaluationScore> evaluationMeasuresList = new ArrayList<EvaluationScore>();
    Map<Metric, MetricScore> globalMeasures = Output.evaluatorToMap(e, nrOfClasses, taskType, bootstrap);
    for (Metric m : globalMeasures.keySet()) {
        MetricScore score = globalMeasures.get(m);
        DecimalFormat dm = MathHelper.defaultDecimalFormat;
        EvaluationScore em = new EvaluationScore(m.implementation, m.name,
                score.getScore() == null ? null : dm.format(score.getScore()), null,
                score.getArrayAsString(dm));
        evaluationMeasuresList.add(em);
    }
    for (int i = 0; i < sampleEvaluation.length; ++i) {
        for (int j = 0; j < sampleEvaluation[i].length; ++j) {
            for (int k = 0; k < sampleEvaluation[i][j].length; ++k) {
                Map<Metric, MetricScore> currentMeasures = Output.evaluatorToMap(sampleEvaluation[i][j][k],
                        nrOfClasses, taskType, bootstrap);
                for (Metric m : currentMeasures.keySet()) {
                    MetricScore score = currentMeasures.get(m);
                    DecimalFormat dm = MathHelper.defaultDecimalFormat;
                    EvaluationScore currentMeasure;
                    if (taskType == TaskType.LEARNINGCURVE) {
                        currentMeasure = new EvaluationScore(m.implementation, m.name,
                                score.getScore() == null ? null : dm.format(score.getScore()),
                                score.getArrayAsString(dm), i, j, k,
                                predictionCounter.getShadowTypeSize(i, j, k));
                    } else {
                        currentMeasure = new EvaluationScore(m.implementation, m.name,
                                score.getScore() == null ? null : dm.format(score.getScore()),
                                score.getArrayAsString(dm), i, j);
                    }
                    evaluationMeasuresList.add(currentMeasure);
                }
            }
        }
    }
    evaluationScores = evaluationMeasuresList.toArray(new EvaluationScore[evaluationMeasuresList.size()]);
}