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:core.me.Context.java

License:Open Source License

public double[] getVirtualRelationship(Context p) throws Exception {

    Classifiers cc = Classifiers.get();//from  www . ja v a2s  . c o m
    Instances dataStruc = cc.getDataStructRC();

    double H = 0, D = 0, DX = 0.;
    int parentClass = 1;

    H = this.getH(p);
    D = this.getD(p);
    DX = this.getDX(p);
    parentClass = p.getSymbolClass();

    double[] values = new double[6];
    values[0] = H;
    values[1] = D;
    values[2] = DX;
    values[3] = dataStruc.attribute(3).indexOfValue("" + this.theClass.get());
    values[4] = dataStruc.attribute(4).indexOfValue("" + parentClass);
    values[5] = dataStruc.attribute(5).indexOfValue("0");

    Instance inst = new Instance(1.0, values);
    inst.setDataset(dataStruc);
    inst.setClassMissing();

    return cc.getVirtualRelationship(inst);
}

From source file:core.TextDirectoryLoader.java

License:Open Source License

/**
 * Process input directories/files incrementally.
 *
 * @param structure ignored//from  w  w  w  .ja  v a2  s  .c o  m
 * @return never returns without throwing an exception
 * @throws IOException if a problem occurs
 */
@Override
public Instance getNextInstance(Instances structure) throws IOException {
    // throw new
    // IOException("TextDirectoryLoader can't read data sets incrementally.");

    String directoryPath = getDirectory().getAbsolutePath();
    Attribute classAtt = structure.classAttribute();
    if (m_filesByClass == null) {
        m_filesByClass = new ArrayList<LinkedList<String>>();
        for (int i = 0; i < classAtt.numValues(); i++) {
            File classDir = new File(directoryPath + File.separator + classAtt.value(i));
            String[] files = classDir.list();
            LinkedList<String> classDocs = new LinkedList<String>();
            for (String cd : files) {
                File txt = new File(directoryPath + File.separator + classAtt.value(i) + File.separator + cd);
                if (txt.isFile()) {
                    classDocs.add(cd);
                }
            }
            m_filesByClass.add(classDocs);
        }
    }

    // cycle through the classes
    int count = 0;
    LinkedList<String> classContents = m_filesByClass.get(m_lastClassDir);
    boolean found = (classContents.size() > 0);
    while (classContents.size() == 0) {
        m_lastClassDir++;
        count++;
        if (m_lastClassDir == structure.classAttribute().numValues()) {
            m_lastClassDir = 0;
        }
        classContents = m_filesByClass.get(m_lastClassDir);
        if (classContents.size() > 0) {
            found = true; // we have an instance we can create
            break;
        }
        if (count == structure.classAttribute().numValues()) {
            break; // must be finished
        }
    }

    if (found) {
        String nextDoc = classContents.poll();
        File txt = new File(
                directoryPath + File.separator + classAtt.value(m_lastClassDir) + File.separator + nextDoc);

        BufferedReader is;
        if (m_charSet == null || m_charSet.length() == 0) {
            is = new BufferedReader(new InputStreamReader(new FileInputStream(txt)));
        } else {
            is = new BufferedReader(new InputStreamReader(new FileInputStream(txt), m_charSet));
        }
        StringBuffer txtStr = new StringBuffer();
        int c;
        while ((c = is.read()) != -1) {
            txtStr.append((char) c);
        }

        double[] newInst = null;
        if (m_OutputFilename) {
            newInst = new double[3];
        } else {
            newInst = new double[2];
        }

        newInst[0] = 0;
        structure.attribute(0).setStringValue(txtStr.toString());

        if (m_OutputFilename) {
            newInst[1] = 0;
            structure.attribute(1).setStringValue(txt.getAbsolutePath());
        }
        newInst[structure.classIndex()] = m_lastClassDir;
        Instance inst = new DenseInstance(1.0, newInst);
        inst.setDataset(structure);
        is.close();

        m_lastClassDir++;
        if (m_lastClassDir == structure.classAttribute().numValues()) {
            m_lastClassDir = 0;
        }

        return inst;
    } else {
        return null; // done!
    }
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * Evaluates the classifier on a single instance and records the
 * prediction (if the class is nominal).
 *
 * @param classifier machine learning classifier
 * @param instance the test instance to be classified
 * @return the prediction made by the clasifier
 * @throws Exception if model could not be evaluated 
 * successfully or the data contains string attributes
 *//*www  . ja  v  a2s  .  c  o  m*/
public double evaluateModelOnceAndRecordPrediction(Classifier classifier, Instance instance) throws Exception {

    Instance classMissing = (Instance) instance.copy();
    double pred = 0;
    classMissing.setDataset(instance.dataset());
    classMissing.setClassMissing();
    if (m_ClassIsNominal) {
        if (m_Predictions == null) {
            m_Predictions = new FastVector();
        }
        double[] dist = classifier.distributionForInstance(classMissing);
        pred = Utils.maxIndex(dist);
        if (dist[(int) pred] <= 0) {
            pred = Instance.missingValue();
        }
        updateStatsForClassifier(dist, instance);
        m_Predictions.addElement(new NominalPrediction(instance.classValue(), dist, instance.weight()));
    } else {
        pred = classifier.classifyInstance(classMissing);
        updateStatsForPredictor(pred, instance);
    }
    return pred;
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * store the prediction made by the classifier as a string
 * /*from   w  ww.  j ava  2s. co  m*/
 * @param classifier      the classifier to use
 * @param inst      the instance to generate text from
 * @param instNum      the index in the dataset
 * @param attributesToOutput   the indices of the attributes to output
 * @param printDistribution   prints the complete distribution for nominal 
 *             classes, not just the predicted value
 * @return                    the prediction as a String
 * @throws Exception      if something goes wrong
 * @see         #printClassifications(Classifier, Instances, String, int, Range, boolean)
 */
protected static String predictionText(Classifier classifier, Instance inst, int instNum,
        Range attributesToOutput, boolean printDistribution)

        throws Exception {

    StringBuffer result = new StringBuffer();
    int width = 10;
    int prec = 3;

    Instance withMissing = (Instance) inst.copy();
    withMissing.setDataset(inst.dataset());
    withMissing.setMissing(withMissing.classIndex());
    double predValue = classifier.classifyInstance(withMissing);

    // index
    result.append(Utils.padLeft("" + (instNum + 1), 6));

    if (inst.dataset().classAttribute().isNumeric()) {
        // actual
        if (inst.classIsMissing())
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(inst.classValue(), width, prec));
        // predicted
        if (Instance.isMissingValue(predValue))
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(predValue, width, prec));
        // error
        if (Instance.isMissingValue(predValue) || inst.classIsMissing())
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
    } else {
        // actual
        result.append(" "
                + Utils.padLeft(((int) inst.classValue() + 1) + ":" + inst.toString(inst.classIndex()), width));
        // predicted
        if (Instance.isMissingValue(predValue))
            result.append(" " + Utils.padLeft("?", width));
        else
            result.append(" " + Utils.padLeft(
                    ((int) predValue + 1) + ":" + inst.dataset().classAttribute().value((int) predValue),
                    width));
        // error?
        if (!Instance.isMissingValue(predValue) && !inst.classIsMissing()
                && ((int) predValue + 1 != (int) inst.classValue() + 1))
            result.append(" " + "  +  ");
        else
            result.append(" " + "     ");
        // prediction/distribution
        if (printDistribution) {
            if (Instance.isMissingValue(predValue)) {
                result.append(" " + "?");
            } else {
                result.append(" ");
                double[] dist = classifier.distributionForInstance(withMissing);
                for (int n = 0; n < dist.length; n++) {
                    if (n > 0)
                        result.append(",");
                    if (n == (int) predValue)
                        result.append("*");
                    result.append(Utils.doubleToString(dist[n], prec));
                }
            }
        } else {
            if (Instance.isMissingValue(predValue))
                result.append(" " + "?");
            else
                result.append(" " + Utils.doubleToString(
                        classifier.distributionForInstance(withMissing)[(int) predValue], prec));
        }
    }

    // attributes
    result.append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n");

    return result.toString();
}

From source file:crystalball.quant.QuantWekaSMOreg.java

License:Open Source License

private void makeDataSet(BarData barData, int startBarId, int endBarId) {
    ArrayList<Attribute> attrs = new ArrayList<Attribute>();
    attrs.add(id);/*ww  w.j av a  2s.  c o  m*/
    attrs.add(close);
    instances = new Instances("instanceName", attrs, endBarId - startBarId + 1);

    for (int i = startBarId; i <= endBarId; i++) {
        // Create empty instance with three attribute values 
        Instance inst = new DenseInstance(2);

        inst.setValue(instances.attribute("id"), i);
        inst.setValue(instances.attribute("close"), barData.close[i]);
        inst.setDataset(instances);
        instances.add(inst);
    }
}

From source file:dataHandlers.DataClusterHandler.java

private Instance toInstance(User user, Instances dataSet) {
    Instance tempInstance = new Instance(userPoints.numAttributes());
    tempInstance.setDataset(userPoints);
    String userDataString = user.getUserID() + user.getTasteString(LastFMDataHandler.getTagCount());
    String[] dataArray = userDataString.split(",");
    for (int index = 0; index < dataArray.length; index++) {
        tempInstance.setValue(index, Integer.parseInt(dataArray[index]));
    }//from  ww w.j  a  v a 2s  .co  m
    return tempInstance;
}

From source file:de.uniheidelberg.cl.swp.mlprocess.MLProcess.java

License:Apache License

/**
 * Creates the classifications of the test-{@link CoreferencePair}s by using the classifier
 * trained on the test-{@link CoreferencePair}s.
 * /*  ww w . j  a v a2 s  .c  om*/
 * @param testCorefs {@link CoreferencePair}s extraced from the test corpus by the ACR-Systems.
 * @return {@link CoreferencePair} which are predicted by our classifier to be correct.
 */
private List<CoreferencePair> createPrediction(Map<String, List<CoreferencePair>> testCorefs) throws Exception {
    List<CoreferencePair> predictions = new ArrayList<CoreferencePair>();
    for (String s : testCorefs.keySet()) {
        for (final CoreferencePair cp : testCorefs.get(s)) {
            Instance ini = ic.addCorefInstance(cp, s);
            ini.setDataset(ic.getInstances());

            /* use the classifier to select a label */
            if (wr.labelUnknownInstance(ini) == 0.0) {
                cp.setAcrSystem(ini.stringValue(ini.numAttributes() - 2));
                predictions.add(cp);
            }
        }
    }
    predictions = removeDuplicates(predictions);
    return predictions;
}

From source file:de.uni_koeln.phil_fak.iv.tm.p4.classification.WekaAdapter.java

License:Open Source License

private Instance instance(Document document, String label) {
    List<Float> values = document.getVector(corpus).getValues();
    /* Die Instanz enthlt alle Merkmale plus die Klasse: */
    double[] vals = new double[values.size() + 1];
    for (int i = 0; i < values.size(); i++) {
        vals[i + 1] = values.get(i);//  w ww.  j  a va  2s .  c  om
    }
    Instance instance = new Instance(1, vals);
    /*
     * Und muss erfahren, was die Werte bedeuten, was wir fr unser
     * Trainingsset beschrieben hatten:
     */
    instance.setDataset(trainingSet);
    /*
     * Beim Training haben wir Instanzen mit vorhandenem Klassenlabel, bei
     * der Klassifikation ist die Klasse unbekannt:
     */
    if (label == null) {
        instance.setClassMissing(); // during classification
    } else
        instance.setClassValue(label); // during training
    return instance;
}

From source file:de.uni_koeln.spinfo.classification.zoneAnalysis.classifier.WekaClassifier.java

License:Open Source License

private Instance instance(ClassifyUnit cu, Instances trainingSet) {
    double[] values = cu.getFeatureVector();
    String classID = ((ZoneClassifyUnit) cu).getActualClassID() + "";
    Instance instance = new SparseInstance(1, values);
    /*/*from   w ww.ja va2  s  .  c  om*/
     * Weka muss 'erklrt' bekommen, was die Werte bedeuten - dies ist im Trainingsset beschrieben:
     */
    instance.setDataset(trainingSet);
    /*
     * Beim Training geben wir den Instanzen ein Klassenlabel, bei der Klassifikation ist die Klasse unbekannt:
     */
    if (classID == "0") {
        instance.setClassMissing(); // bei Klassifikation
    } else
        instance.setClassValue(classID); // beim Training
    return instance;
}

From source file:de.upb.timok.oneclassclassifier.WekaSvmClassifier.java

License:Open Source License

@Override
public boolean isOutlier(double[] testSample) {
    Instance wekaInstance;
    if (testSet == null) {
        final ArrayList<double[]> temp = new ArrayList<>();
        temp.add(testSample);/*www .j a v  a 2  s.  c  o  m*/
        testSet = DatasetTransformationUtils.testSetToInstances(temp);
        wekaInstance = testSet.get(0);
    } else {
        wekaInstance = new DenseInstance(1, testSample);
        testSet.add(wekaInstance);
        wekaInstance.setDataset(testSet);
    }
    try {
        if (filter != null) {
            testSet = Filter.useFilter(testSet, filter);
            wekaInstance = testSet.lastInstance();
        }
        double result;
        result = wekaSvm.classifyInstance(wekaInstance);
        if (Double.isNaN(result)) {
            return true;
        } else {
            return false;
        }
    } catch (final Exception e) {
        logger.error("Unexpected exception", e);
    }
    return false;
}