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:com.yahoo.research.scoring.classifier.NutchOnlineClassifier.java

License:Apache License

/**
 * Converts an {@link AnthURL} into an {@link Instance} which can be handled
 * by the {@link Classifier}.//  w  ww. j ava2 s .co  m
 * 
 * @param url
 *            the {@link AnthURL} which should be transformed/converted.
 * @return the resulting {@link Instance}.
 */
private static Instance convert(AnthURL url) {
    if (url != null) {

        Instance inst = new SparseInstance(dimension);
        inst.replaceMissingValues(replaceMissingValues);

        inst.setDataset(instances);
        inst.setValue(attributesIndex.get("class"), (url.sem ? "sem" : "nonsem"));
        inst.setValue(attributesIndex.get("sempar"), (url.semFather ? 1 : 0));
        inst.setValue(attributesIndex.get("nonsempar"), (url.nonSemFather ? 1 : 0));
        inst.setValue(attributesIndex.get("semsib"), (url.semSibling ? 1 : 0));
        inst.setValue(attributesIndex.get("nonsempar"), (url.nonSemFather ? 1 : 0));
        inst.setValue(attributesIndex.get("domain"), url.uri.getHost());
        Set<String> tokens = new HashSet<String>();

        tokens.addAll(tokenizer(url.uri.getPath()));
        tokens.addAll(tokenizer(url.uri.getQuery()));
        tokens.addAll(tokenizer(url.uri.getFragment()));
        for (String tok : tokens) {
            inst.setValue(attributesIndex.get(getAttributeNameOfHash(getHash(tok, hashTrickSize))), 1);
        }
        return inst;

    } else {
        System.out.println("Input AnthURL for convertion into instance was null.");
        return null;
    }
}

From source file:com.zazhu.BlueHub.BlueHub.java

License:Apache License

/**
 * receives the last reads from the sensors and creates the features we use
 * only the acc x,y,z (either from internal or external sensor)
 * /* w ww  .j  a  v  a2s  . c  o m*/
 * @param sensorQueue
 * @throws Exception 
 */
private Instance processingSenseData(Queue<String> sensorQueue, char whatSensor) throws Exception {

    BufferedReader reader;
    Instances format;
    Instance newInstance = null;

    Log.d(TAG, "Queue size = " + mQueueSize);

    if (sensorQueue.size() <= 0)
        throw new Exception("Queue empty");

    // create the arrays that will contain the accelerometer data
    // s.x s.y s.z
    double[] sx = new double[sensorQueue.size()];
    double[] sy = new double[sensorQueue.size()];
    double[] sz = new double[sensorQueue.size()];

    String rawReading;
    StringTokenizer st;

    int index;

    if (D)
        Log.e(TAG, "+++ COMPUTING FEATURES +++");

    // 1. collect raw data. what kind of sensing data? external vs. internal
    switch (whatSensor) {
    case EXTERNAL:
        index = 0;
        while ((rawReading = sensorQueue.poll()) != null) {
            // FORMAT:
            // "Time_SensorName_SensorNumber_Counter_Xacc_Yacc_Zacc_Xgyro_Ygyro_checksum"
            // position of the values needed: s.x = 4, s.y = 5, s.z = 6
            st = new StringTokenizer(rawReading, FIELD_SEP);
            // not needed data
            for (int i = 0; i < 4; i++)
                st.nextToken();
            // s.x, s.y, s.z
            sx[index] = Double.valueOf(st.nextToken());
            sy[index] = Double.valueOf(st.nextToken());
            sz[index] = Double.valueOf(st.nextToken());

            index += 1;
        }

        // 2. process raw data
        // 2.1 read the input format for the instance (TODO must be changed to
        // use weka classes)
        reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.format_extern)));

        try {
            format = new Instances(reader);

            if (format.classIndex() == -1)
                format.setClassIndex(format.numAttributes() - 1);

            // 2.2 create a new instance
            newInstance = new DenseInstance(7);
            newInstance.setDataset(format);
            // set attributes
            newInstance.setValue(format.attribute(0), Feature.getStd(sx));
            newInstance.setValue(format.attribute(1), Feature.getStd(sy));
            newInstance.setValue(format.attribute(2), Feature.getStd(sz));
            newInstance.setValue(format.attribute(3), Feature.getMean(sx));
            newInstance.setValue(format.attribute(4), Feature.getMean(sy));
            newInstance.setValue(format.attribute(5), Feature.getMean(sz));
            // set unknown class
            newInstance.setMissing(format.attribute(6));

        } catch (IOException e) {
            e.printStackTrace();
        }

        break;
    case INTERNAL:

        index = 0;
        while ((rawReading = sensorQueue.poll()) != null) {

            // FORMAT "Xacc_Yacc_Zacc"
            // position of the values needed: s.x = 0, s.y = 1, s.z = 2
            st = new StringTokenizer(rawReading, FIELD_SEP);

            // s.x, s.y, s.z
            sx[index] = Double.valueOf(st.nextToken());
            sy[index] = Double.valueOf(st.nextToken());
            sz[index] = Double.valueOf(st.nextToken());

            index += 1;
        }

        // 2. process raw data
        // 2.1 read the input format for the instance (TODO must be changed to
        // use weka classes)
        reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.format_intern)));

        try {
            format = new Instances(reader);

            if (format.classIndex() == -1)
                format.setClassIndex(format.numAttributes() - 1);

            // 2.2 create a new instance
            newInstance = new DenseInstance(7);
            newInstance.setDataset(format);
            // set attributes
            newInstance.setValue(format.attribute(0), Feature.getStd(sx));
            newInstance.setValue(format.attribute(1), Feature.getStd(sy));
            newInstance.setValue(format.attribute(2), Feature.getStd(sz));
            newInstance.setValue(format.attribute(3), Feature.getMean(sx));
            newInstance.setValue(format.attribute(4), Feature.getMean(sy));
            newInstance.setValue(format.attribute(5), Feature.getMean(sz));
            // set unknown class
            newInstance.setMissing(format.attribute(6));

        } catch (IOException e) {
            e.printStackTrace();
        }

        break;
    default:
        if (D)
            Log.e(TAG, "+++ COMPUTING FEATURES: NO VALUE FOR THE SENSOR READING +++");
        break;
    }

    return newInstance;

}

From source file:core.classification.Classifiers.java

License:Open Source License

private Instance buildYNCInstance(Context p, Context c) {
    double RAREA = 0, H = 0, D = 0, V = 0.;
    int parentClass = 1, childClass = 1;

    RAREA = ((double) c.area()) / p.area();
    H = c.getH(p);// w  w w .  j a  v  a  2s .c  om
    D = c.getD(p);
    V = c.getDX(p);
    parentClass = (p.getSymbolClass() > 0) ? p.getSymbolClass() : 1;
    childClass = (c.getSymbolClass() > 0) ? c.getSymbolClass() : 1;

    double[] values = new double[7];
    values[0] = dataStructYC.attribute(0).indexOfValue("" + parentClass);
    values[1] = dataStructYC.attribute(1).indexOfValue("" + childClass);
    values[2] = RAREA;
    values[3] = H;
    values[4] = D;
    values[5] = V;
    values[6] = dataStructYC.attribute(6).indexOfValue("N");

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

    return inst;
}

From source file:core.Core.java

public String run() throws Exception {
    ConverterUtils.DataSource source = new ConverterUtils.DataSource("src/files/powerpuffgirls.arff");

    HashMap<String, Classifier> hash = new HashMap<>();

    hash.put("J48", new J48());
    hash.put("NaiveBayes", new NaiveBayes());
    hash.put("IBk=1", new IBk(1));
    hash.put("IBk=3", new IBk(3));
    hash.put("MultilayerPerceptron", new MultilayerPerceptron());

    LibSVM svm = new LibSVM();
    hash.put("LibSVM", svm);
    Instances ins = source.getDataSet();

    ins.setClassIndex(4);//from w  ww  .ja v a 2  s . co m

    StringBuilder sb = new StringBuilder();

    int blossom = 0;
    int bubbles = 0;

    Instance test = null;

    for (Map.Entry<String, Classifier> entry : hash.entrySet()) {
        Classifier c = entry.getValue();
        c.buildClassifier(ins);

        test = new Instance(5);

        float[] array = classifyImage();

        test.setDataset(ins);
        test.setValue(0, array[0]);
        test.setValue(1, array[1]);
        test.setValue(2, array[2]);
        test.setValue(3, array[3]);

        double prob[] = c.distributionForInstance(test);

        sb.append("<em>");
        sb.append(entry.getKey());
        sb.append(":</em>");
        sb.append("<br/>");

        for (int i = 0; i < prob.length; i++) {
            String value = test.classAttribute().value(i);

            if (getRoundedValue(prob[i]) >= CUT_NOTE) {
                if (getClassValue(value))
                    blossom++;
                else
                    bubbles++;
            }

            sb.append(getClassName(value));
            sb.append(": ");
            sb.append("<strong>");
            sb.append(getRoundedValue(prob[i]) < CUT_NOTE ? "Rejeitado!" : getValueFormatted(prob[i]));
            sb.append("</strong>");
            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }

        sb.append("<br/>");

        System.out.println("blossom: " + blossom);
        System.out.println("bubbles: " + bubbles);
        System.out.println("=================\n");
    }

    sb.append(blossom > bubbles ? "<h3> a Florzinha!</h3>" : "<h3> a Lindinha!</h3>");

    blossom = 0;
    bubbles = 0;

    return sb.toString();
}

From source file:core.me.Context.java

License:Open Source License

public Instance buildSCAInstance() {
    Instances dataStruc = Classifiers.getInst(false).getDataStructSCA();

    double[] values = new double[2];
    values[0] = this.theSymbol.getRatio();
    values[1] = dataStruc.attribute(1).indexOfValue("0");

    Instance inst = new Instance(1.0, values);
    inst.setDataset(dataStruc);
    inst.setClassMissing();/*from   ww w .j av  a  2 s.com*/
    return inst;
}

From source file:core.me.Context.java

License:Open Source License

public Instance buildSCBInstance() {
    Instances dataStruc = Classifiers.getInst(false).getDataStructSCB();

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

    if (this.getParent() != null) {
        H = this.getH();
        D = this.getD();
        DX = this.getDX();
        parentClass = this.getParentSymbol().getSymbolClass();
    }/* ww  w. jav a  2  s .c o  m*/

    double[] values = new double[5];
    values[0] = H;
    values[1] = D;
    values[2] = DX;
    values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(parentClass));
    values[4] = dataStruc.attribute(4).indexOfValue("0");

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

    return inst;
}

From source file:core.me.Context.java

License:Open Source License

public Instance buildSCC1Instance() {
    Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.INLINE);

    double LH, LD, LDX;
    int LCLASS;//from   w  w  w . j a v a  2  s .  co  m
    double height = (double) this.theSymbol.getHeight();

    LH = height / ((double) this.horSymbol.getHeight());
    LD = (this.theSymbol.getCenter() - this.horSymbol.getCenter()) / height;
    LDX = this.getHor().getDX();
    LCLASS = this.horSymbol.getSymbolClass();

    double[] values = new double[5];
    values[0] = LH;
    values[1] = LD;
    values[2] = LDX;
    values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(LCLASS));
    values[4] = dataStruc.attribute(4).indexOfValue("0");

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

}

From source file:core.me.Context.java

License:Open Source License

public Instance buildSCC2Instance() {
    Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.SUPERSCRIPT);

    double EH, ED, EDX;
    int ECLASS;//from w  w w  . ja  va  2 s.com
    double height = (double) this.theSymbol.getHeight();

    EH = height / ((double) this.supSymbol.getHeight());
    ED = (this.theSymbol.getCenter() - this.supSymbol.getCenter()) / height;
    EDX = this.getSup().getDX();
    ECLASS = this.supSymbol.getSymbolClass();

    double[] values = new double[5];
    values[0] = EH;
    values[1] = ED;
    values[2] = EDX;
    values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(ECLASS));
    values[4] = dataStruc.attribute(4).indexOfValue("0");

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

}

From source file:core.me.Context.java

License:Open Source License

public Instance buildSCC3Instance() {
    Instances dataStruc = Classifiers.getInst(false).getDataStructSCC(Relationship.SUBSCRIPT);

    double SH, SD, SDX;
    int SCLASS;//from  w  w  w.  j a v a2s .  c o m
    double height = (double) this.theSymbol.getHeight();

    SH = height / ((double) this.subSymbol.getHeight());
    SD = (this.theSymbol.getCenter() - this.subSymbol.getCenter()) / height;
    SDX = this.getSub().getDX();
    SCLASS = this.subSymbol.getSymbolClass();

    double[] values = new double[5];
    values[0] = SH;
    values[1] = SD;
    values[2] = SDX;
    values[3] = dataStruc.attribute(3).indexOfValue(Integer.toString(SCLASS));
    values[4] = dataStruc.attribute(4).indexOfValue("0");

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

}

From source file:core.me.Context.java

License:Open Source License

public Instance buildRCInstance() {

    Instances dataStruc = Classifiers.getInst(false).getDataStructRC();

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

    if (this.getParent() != null) {
        H = ((double) this.getParentSymbol().getHeight()) / this.theSymbol.getHeight();
        D = (this.getParentSymbol().getCenter() - this.theSymbol.getCenter())
                / this.getParentSymbol().getHeight();
        DX = this.getDX();
        parentClass = this.getParentSymbol().getSymbolClass();
    }//from  w  w  w  .  j  av a2  s  .c o  m

    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 inst;

}