Example usage for weka.core Instance setDataset

List of usage examples for weka.core Instance setDataset

Introduction

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

Prototype

public void setDataset(Instances instances);

Source Link

Document

Sets the reference to the dataset.

Usage

From source file:org.knime.knip.suise.node.boundarymodel.contourdata.WekaMIContourDataClassifier.java

License:Open Source License

/**
 * {@inheritDoc}/* www.  j av  a 2s. c  o m*/
 */
@Override
public double contourProbability(double[] inst) throws Exception {
    Instances bagData = new Instances(m_data.attribute(1).relation(), 1);
    Instance i = new DenseInstance(1, inst);
    i.setDataset(bagData);

    bagData.add(i);

    Instance bag = new DenseInstance(3);
    bag.setDataset(m_data);
    int val = bag.attribute(1).addRelation(bagData);
    bag.setValue(1, val);

    return m_classifier.distributionForInstance(bag)[1];

}

From source file:org.knime.knip.suise.ops.BuildTrainingData.java

License:Open Source License

/**
 * {@inheritDoc}//from  ww w . j av  a 2  s . c  om
 */
@Override
public Instances compute(RandomAccessibleInterval<LabelingType<L>> lab, Img<T> img, Instances r) {
    Random rand = new Random();

    double[] extent = new double[lab.numDimensions()];
    for (int d = 0; d < m_dimIndices.length; d++) {
        extent[m_dimIndices[d]] = lab.max(m_dimIndices[d]);
    }
    RectangleRegionOfInterest roi = new RectangleRegionOfInterest(new double[lab.numDimensions()], extent);

    Cursor<LabelingType<L>> labCur = roi.getIterableIntervalOverROI(lab).localizingCursor();
    OutOfBounds<T> imgRA = new OutOfBoundsBorder<T>(img);

    LabelRegions<L> regions = new LabelRegions<L>(lab);
    // get the class distributions
    Map<L, Double> classDistr = null;
    if (m_balanceInstancePerClass) {
        long sum = 0;
        long area;
        Collection<L> labels = regions.getExistingLabels();
        classDistr = new HashMap<L, Double>(labels.size());
        for (L label : labels) {
            area = regions.getLabelRegion(label).size();
            sum += area;
            classDistr.put(label, new Double(area));
        }
        // determine the new sampling rate for each class individually
        double instancesPerClass = (double) sum / (double) labels.size();
        for (L label : labels) {
            Double sampleRate = instancesPerClass / classDistr.get(label) * m_samplingRate;
            classDistr.put(label, sampleRate);
        }
    }

    long[] tmpPos = new long[imgRA.numDimensions()];
    while (labCur.hasNext()) {
        labCur.fwd();
        for (int d = 0; d < m_dimIndices.length; d++) {
            imgRA.setPosition(labCur.getLongPosition(m_dimIndices[d]), m_dimIndices[d]);
            if (imgRA.isOutOfBounds()) {
                imgRA.localize(tmpPos);
                NodeLogger.getLogger(getClass()).warn("Labeling reaches beyond the feature image. Position "
                        + Arrays.toString(tmpPos) + " skipped.");
                continue;
            }

        }
        if (!labCur.get().isEmpty()) {

            if (m_balanceInstancePerClass) {
                if (rand.nextDouble() >= classDistr.get(labCur.get().iterator().next())) {
                    continue;
                }
            } else {
                if (rand.nextDouble() >= m_samplingRate) {
                    continue;
                }
            }

            double[] featVec = new double[(int) img.dimension(m_featDim)];
            for (int f = 0; f < img.dimension(m_featDim); f++) {
                imgRA.setPosition(f, m_featDim);
                featVec[f] = imgRA.get().getRealDouble();
            }
            for (L classLabel : labCur.get()) {
                Instance instance = new DenseInstance(1.0, featVec);
                instance.insertAttributeAt(instance.numAttributes());
                instance.setDataset(r);
                instance.setClassValue(classLabel.toString());

                r.add(instance);

            }
        }
    }
    return r;
}

From source file:org.knime.knip.suise.ops.ClassifyPixels.java

License:Open Source License

/**
 * {@inheritDoc}/*  w  w w.  j av  a  2  s . c om*/
 */
@Override
public Img<RT>[] compute(Img<T> op, Img<RT>[] r) {

    RandomAccess<LabelingType<String>> labelingRA = null;
    if (m_createLabeling) {
        long[] dims = new long[m_dimIndices.length];
        for (int j = 0; j < dims.length; j++) {
            dims[j] = op.dimension(j);
        }

        m_labeling = KNIPGateway.ops().create().imgLabeling(new FinalInterval(dims));
        labelingRA = m_labeling.randomAccess();
    }

    long[] max = new long[m_dimIndices.length];
    for (int d = 0; d < m_dimIndices.length; d++) {
        max[d] = op.max(m_dimIndices[d]);
    }

    IntervalIterator ii = new IntervalIterator(new long[m_dimIndices.length], max);

    RandomAccess<RT>[] resRAs = new RandomAccess[r.length];
    for (int i = 0; i < resRAs.length; i++) {
        resRAs[i] = r[i].randomAccess();
    }
    RandomAccess<T> imgRA = op.randomAccess();

    double[] featVec = new double[(int) op.dimension(m_featDimIdx)];
    Instance instance = new DenseInstance(1.0, featVec);
    instance.setDataset(m_dataset);
    while (ii.hasNext()) {
        ii.fwd();
        for (int d = 0; d < m_dimIndices.length; d++) {
            imgRA.setPosition(ii.getLongPosition(d), m_dimIndices[d]);
        }

        for (int f = 0; f < op.dimension(m_featDimIdx); f++) {
            imgRA.setPosition(f, m_featDimIdx);
            featVec[f] = imgRA.get().getRealDouble();
        }

        double[] probs = null;
        try {
            probs = m_classifier.distributionForInstance(instance);

            if (m_createLabeling) {
                for (int d = 0; d < m_dimIndices.length; d++) {
                    labelingRA.setPosition(ii.getLongPosition(d), d);
                    labelingRA.get().clear();
                    labelingRA.get().add(m_dataset.classAttribute().value(Utils.maxIndex(probs)));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < probs.length; i++) {
            if (probs[i] > 0) {
                for (int d = 0; d < m_dimIndices.length; d++) {
                    resRAs[i].setPosition(ii.getLongPosition(d), d);
                    resRAs[i].get().setReal(probs[i] * (m_resType.getMaxValue() - m_resType.getMinValue())
                            + m_resType.getMinValue());
                }
            }
        }

    }

    return r;

}

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

License:Open Source License

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

    if (actorIt.hasNext()) {
        int count = 0;

        // create the Instances metadata object
        Instances meta = new Instances("AggregateLinks", new FastVector(), 1);
        FastVector attributeList = new FastVector();
        FastVector attributeItems = new FastVector();
        LinkByRelation linkQuery = (LinkByRelation) LinkQueryFactory.newInstance().create("LinkByRelation");
        linkQuery.buildQuery((String) parameter.get("Relation").get(), false);
        Iterator<Link> link = AlgorithmMacros.filterLink(parameter, g, linkQuery, null, null, null);
        Set<Actor> list = null;
        if ((Boolean) parameter.get("MakeSingleInstance").get()) {
            list = new TreeSet<Actor>();
            if (link != null) {
                while (link.hasNext()) {
                    if (((LinkEnd) parameter.get("ActorEnd").get()) == LinkEnd.ALL) {
                        Link l = link.next();
                        list.add(l.getSource());
                        list.add(l.getDestination());
                    } else if (((LinkEnd) parameter.get("ActorEnd").get()) == LinkEnd.SOURCE) {
                        list.add(link.next().getDestination());
                    } else {
                        list.add(link.next().getSource());
                    }/*from   w  w  w . ja  v a  2s  . com*/
                }
                for (Actor name : list) {
                    attributeItems.addElement(name.getID());
                }
                attributeItems.addElement("None");
                attributeList.addElement(new Attribute("ActorList", attributeItems));
                meta = new Instances("AggregateLinks", attributeList, 1);
            } else {
                Logger.getLogger(AggregateLinks.class.getName()).log(Level.WARNING,
                        "No links of type '" + (String) parameter.get("Relation").get() + "' are found");
            }
        } else {
            list = new TreeSet<Actor>();
            if (link != null) {
                while (link.hasNext()) {
                    if (((LinkEnd) parameter.get("ActorEnd").get()) == LinkEnd.ALL) {
                        Link l = link.next();
                        list.add(l.getSource());
                        list.add(l.getDestination());
                    } else if (((LinkEnd) parameter.get("ActorEnd").get()) == LinkEnd.SOURCE) {
                        list.add(link.next().getDestination());
                    } else {
                        list.add(link.next().getSource());
                    }
                }
                for (Actor name : list) {
                    attributeList.addElement(new Attribute(name.getID()));
                }
                meta = new Instances("AggregateLinks", attributeList, 1);
            } else {
                Logger.getLogger(AggregateLinks.class.getName()).log(Level.WARNING,
                        "No links of type '" + (String) parameter.get("Relation").get() + "' are found");
            }
        }
        while (actorIt.hasNext()) {
            Actor actor = actorIt.next();
            double[] values = new double[meta.numAttributes()];
            TreeSet<Actor> total = new TreeSet<Actor>();
            HashMap<Actor, Double> links = getMap(actor, total, g);
            Instance instance = null;
            if ((Boolean) parameter.get("MakeSingleInstance").get()) {
                if (links.size() > 0) {
                    String name = links.keySet().iterator().next().getID();
                    values[0] = meta.attribute("ActorList").indexOfValue(name);
                } else {
                    values[0] = Double.NaN;
                }
                instance = new Instance(values.length, values);
                instance.setDataset(meta);
            } else {
                Iterator<Actor> it = list.iterator();
                for (int j = 0; j < list.size(); ++j) {
                    Actor artist = it.next();
                    if (links.containsKey(artist)) {
                        values[j] = links.get(artist);
                    } else {
                        values[j] = Double.NaN;
                    }
                }
                instance = new Instance(values.length, values);
                instance.setDataset(meta);
            }

            Property aggregator = PropertyFactory.newInstance()
                    .create("BasicProperty",
                            AlgorithmMacros.getDestID(parameter, g,
                                    (String) parameter.get("DestinationProperty").get()),
                            weka.core.Instance.class);
            try {
                aggregator.add(instance);
            } catch (InvalidObjectTypeException ex) {
                Logger.getLogger(AggregateLinks.class.getName()).log(Level.SEVERE, null, ex);
            }
            actor.add(aggregator);
        }

    } else {
        Logger.getLogger(AggregateLinks.class.getName()).log(Level.WARNING,
                "No tags of mode '" + (String) parameter.get("Mode").get() + "' are present");
    }
}

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);//w ww  .  ja va 2 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  ww  . ja  va 2s. c om
    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.MultiInstanceSVM.java

License:Open Source License

protected void evaluateClassifier(Classifier classifier, Instances dataSet, Graph g, Actor toBePredicted)
        throws Exception {
    if (user != null) {
        for (int i = 0; i < user.length; ++i) {
            int total = 0;
            int setTrue = 0;
            // evaluate all propositionalized instances and evalulate the results
            Link[] interests = g.getLinkBySource((String) parameter[4].getValue(), user[i]);
            if (i % 100 == 0) {
                Logger.getLogger(MultiInstanceSVM.class.getName()).log(Level.FINER, "Evaluating for user " + i);
            }//w  w w . j  a va 2s.  c  om
            if (interests != null) {
                for (int j = 0; j < interests.length; ++j) {
                    Link[] music = g.getLink((String) parameter[5].getValue(), user[i],
                            interests[j].getDestination());
                    if (music != null) {
                        Link[] given = g.getLinkBySource((String) parameter[3].getValue(),
                                interests[j].getDestination());
                        if (given != null) {
                            Instances evaluateData = dataSet.stringFreeStructure();
                            double[] values = new double[artists.length + 3];
                            java.util.Arrays.fill(values, 0.0);
                            values[0] = interests[j].getStrength();
                            values[1] = music[0].getStrength();
                            for (int k = 0; k < given.length; ++k) {
                                values[java.util.Arrays.binarySearch(artists, given[k].getDestination())
                                        + 2] = 1.0;
                            }
                            Instance instance = new SparseInstance(artists.length + 3, values);
                            instance.setDataset(evaluateData);
                            double result = classifier.classifyInstance(instance);
                            if (result == 1.0) {
                                setTrue++;
                            }
                            total++;
                        }
                    }
                }
            }
            boolean evaluate = evaluateResult(setTrue, total);
            if (evaluate) {
                Properties props = new Properties();
                props.setProperty("LinkType", (String) parameter[9].getValue());
                Link derived = LinkFactory.newInstance().create(props);
                derived.set(user[i], 1.0, toBePredicted);
                g.add(derived);
            }
            //                if ((g.getLink((String) parameter[3].getValue(), user[i], toBePredicted) != null) && (evaluate)) {
            //                    correctlyClassified[i]++;
            //                }
            //                if (evaluate) {
            //                    totalClassified[i]++;
            //                }
        }
    }
}

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

License:Open Source License

protected void addInstances(Graph g, Instances dataSet, Actor artist, int skipCount, int positiveSkipCount) {
    int skipCounter = 0;
    int positiveSkipCounter = 0;
    for (int i = 0; i < user.length; ++i) {
        String result = "false";
        if (g.getLink((String) parameter[3].getValue(), user[i], artist) != null) {
            result = "true";
        }/*from w w  w .  j  a  va2  s . co m*/
        Link[] interests = g.getLinkBySource((String) parameter[4].getValue(), user[i]);
        if (interests != null) {
            for (int j = 0; j < interests.length; ++j) {
                Link[] music = g.getLink((String) parameter[5].getValue(), user[i],
                        interests[j].getDestination());
                Link[] given = g.getLinkBySource((String) parameter[3].getValue(),
                        interests[j].getDestination());
                if ((given != null) && (music != null)) {
                    if (((result.contentEquals("true")) && (positiveSkipCounter % positiveSkipCount == 0))
                            || ((result.contentEquals("false")) && (skipCounter % skipCount == 0))) {
                        double[] values = new double[artists.length + 3];
                        java.util.Arrays.fill(values, 0.0);
                        values[0] = interests[j].getStrength();
                        values[1] = music[0].getStrength();
                        for (int k = 0; k < given.length; ++k) {
                            values[java.util.Arrays.binarySearch(artists, given[k].getDestination()) + 2] = 1.0;
                        }
                        if (result.compareTo("true") == 0) {
                            values[values.length - 1] = 1.0;
                        }
                        Instance instance = new SparseInstance(3 + artists.length, values);
                        instance.setDataset(dataSet);
                        instance.setClassValue(result);
                        dataSet.add(instance);
                        //                            System.out.println("Adding instance for user "+i);
                        if (result.contentEquals("false")) {
                            skipCounter++;
                        } else {
                            positiveSkipCounter++;
                        }
                    } else if (result.contentEquals("false")) {
                        skipCounter++;
                    } else {
                        positiveSkipCounter++;
                    }
                }
            }
        }
    }
}

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

License:Open Source License

protected void addInstances(Graph g, Instances dataSet, Actor artist, int skipCount, int positiveSkipCount) {
    int skipCounter = 0;
    int positiveSkipCounter = 0;
    for (int i = 0; i < user.length; ++i) {
        String result = "false";
        if (g.getLink((String) parameter[3].getValue(), user[i], artist) != null) {
            result = "true";
        }/*from   w  w  w  .j ava2 s  . c  o  m*/
        Link[] given = g.getLinkBySource((String) parameter[3].getValue(), user[i]);
        if (given != null) {
            if (((result.contentEquals("true")) && (positiveSkipCounter % positiveSkipCount == 0))
                    || ((result.contentEquals("false")) && (skipCounter % skipCount == 0))) {
                double[] values = new double[artists.length + 1];
                java.util.Arrays.fill(values, 0.0);
                for (int k = 0; k < given.length; ++k) {
                    if (given[k].getDestination() == artist) {
                        values[java.util.Arrays.binarySearch(artists, given[k].getDestination())] = Double.NaN;
                    } else {
                        values[java.util.Arrays.binarySearch(artists, given[k].getDestination())] = 1.0;
                    }
                }
                if (result.compareTo("true") == 0) {
                    values[values.length - 1] = 1.0;
                }
                Instance instance = new SparseInstance(1 + artists.length, values);
                instance.setDataset(dataSet);
                instance.setClassValue(result);
                dataSet.add(instance);
                //                            System.out.println("Adding instance for user "+i);
                if (result.contentEquals("false")) {
                    skipCounter++;
                } else {
                    positiveSkipCounter++;
                }
            } else if (result.contentEquals("false")) {
                skipCounter++;
            } else {
                positiveSkipCounter++;
            }
        }
    }
}

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

License:Open Source License

protected void evaluateClassifier(Classifier classifier, Instances dataSet, Graph g, Actor toBePredicted)
        throws Exception {
    if (user != null) {
        for (int i = 0; i < user.length; ++i) {
            // evaluate all propositionalized instances and evalulate the results
            if (i % 100 == 0) {
                //                   System.out.println("Evaluating for user " + i);
            }/*from   w w w  .j  a v  a  2  s.  c om*/
            Link[] given = g.getLinkBySource((String) parameter[3].getValue(), user[i]);
            if (given != null) {
                Instances evaluateData = dataSet.stringFreeStructure();
                double[] values = new double[artists.length + 3];
                java.util.Arrays.fill(values, 0.0);
                for (int k = 0; k < given.length; ++k) {
                    if (given[k].getDestination().equals(toBePredicted)) {
                        values[java.util.Arrays.binarySearch(artists, given[k].getDestination())] = Double.NaN;
                    } else {
                        values[java.util.Arrays.binarySearch(artists, given[k].getDestination())] = 1.0;
                    }
                }
                Instance instance = new SparseInstance(artists.length + 3, values);
                instance.setDataset(evaluateData);
                double result = classifier.classifyInstance(instance);
                if (result == 1.0) {
                    Properties props = new Properties();
                    props.setProperty("LinkType", (String) parameter[9].getValue());
                    Link derived = LinkFactory.newInstance().create(props);
                    derived.set(user[i], 1.0, toBePredicted);
                    g.add(derived);
                }
            }
        }
    }
}