Example usage for weka.core Instance setClassValue

List of usage examples for weka.core Instance setClassValue

Introduction

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

Prototype

public void setClassValue(String value);

Source Link

Document

Sets the class value of an instance to the given value.

Usage

From source file:tr.gov.ulakbim.jDenetX.streams.ConceptDriftRealStream.java

License:Open Source License

public Instance nextInstance() {
    numberInstanceStream++;/*ww  w.j  a  v  a  2s  . co  m*/
    double numclass = 0.0;
    double x = -4.0 * (double) (numberInstanceStream - this.positionOption.getValue())
            / (double) this.widthOption.getValue();
    double probabilityDrift = 1.0 / (1.0 + Math.exp(x));
    if (this.random.nextDouble() > probabilityDrift) {
        if (!this.inputStream.hasMoreInstances()) {
            this.inputStream.restart();
        }
        this.inputInstance = this.inputStream.nextInstance();
        numclass = this.inputInstance.classValue();
    } else {
        if (!this.driftStream.hasMoreInstances()) {
            this.driftStream.restart();
        }
        this.driftInstance = this.driftStream.nextInstance();
        numclass = this.driftInstance.classValue();
    }
    int m = 0;
    double[] newVals = new double[this.inputInstance.numAttributes() + this.driftInstance.numAttributes() - 1];
    for (int j = 0; j < this.inputInstance.numAttributes() - 1; j++, m++) {
        newVals[m] = this.inputInstance.value(j);
    }
    for (int j = 0; j < this.driftInstance.numAttributes() - 1; j++, m++) {
        newVals[m] = this.driftInstance.value(j);
    }
    newVals[m] = numclass;
    //return new Instance(1.0, newVals);
    Instance inst = new DenseInstance(1.0, newVals);
    inst.setDataset(this.getHeader());
    inst.setClassValue(numclass);
    return inst;

}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.AgrawalGenerator.java

License:Open Source License

public Instance nextInstance() {
    double salary = 0, commission = 0, hvalue = 0, loan = 0;
    int age = 0, elevel = 0, car = 0, zipcode = 0, hyears = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        salary = 20000.0 + 130000.0 * this.instanceRandom.nextDouble();
        commission = (salary >= 75000.0) ? 0 : (10000.0 + 65000.0 * this.instanceRandom.nextDouble());
        // true to c implementation:
        // if (instanceRandom.nextDouble() < 0.5 && salary < 75000.0)
        // commission = 10000.0 + 65000.0 * instanceRandom.nextDouble();
        age = 20 + this.instanceRandom.nextInt(61);
        elevel = this.instanceRandom.nextInt(5);
        car = this.instanceRandom.nextInt(20);
        zipcode = this.instanceRandom.nextInt(9);
        hvalue = (9.0 - zipcode) * 100000.0 * (0.5 + this.instanceRandom.nextDouble());
        hyears = 1 + this.instanceRandom.nextInt(30);
        loan = this.instanceRandom.nextDouble() * 500000.0;
        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(salary, commission,
                age, elevel, car, zipcode, hvalue, hyears, loan);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;//from   w  ww.ja  va  2s  . c  o m
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }
    // perturb values
    if (this.peturbFractionOption.getValue() > 0.0) {
        salary = perturbValue(salary, 20000, 150000);
        if (commission > 0) {
            commission = perturbValue(commission, 10000, 75000);
        }
        age = (int) Math.round(perturbValue(age, 20, 80));
        hvalue = perturbValue(hvalue, (9.0 - zipcode) * 100000.0, 0, 135000);
        hyears = (int) Math.round(perturbValue(hyears, 1, 30));
        loan = perturbValue(loan, 0, 500000);
    }
    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, salary);
    inst.setValue(1, commission);
    inst.setValue(2, age);
    inst.setValue(3, elevel);
    inst.setValue(4, car);
    inst.setValue(5, zipcode);
    inst.setValue(6, hvalue);
    inst.setValue(7, hyears);
    inst.setValue(8, loan);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.HyperplaneGenerator.java

License:Open Source License

public Instance nextInstance() {

    int numAtts = this.numAttsOption.getValue();
    double[] attVals = new double[numAtts + 1];
    double sum = 0.0;
    double sumWeights = 0.0;
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = this.instanceRandom.nextDouble();
        sum += this.weights[i] * attVals[i];
        sumWeights += this.weights[i];
    }/*from w ww  .  j  a v  a  2 s .  c  o  m*/
    int classLabel;
    if (sum >= sumWeights * 0.5) {
        classLabel = 1;
    } else {
        classLabel = 0;
    }
    //Add Noise
    if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
        classLabel = (classLabel == 0 ? 1 : 0);
    }

    Instance inst = new DenseInstance(1.0, attVals);
    inst.setDataset(getHeader());
    inst.setClassValue(classLabel);
    addDrift();
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.LEDGenerator.java

License:Open Source License

public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);// www . ja  va2 s.c o  m
    int selected = this.instanceRandom.nextInt(10);
    for (int i = 0; i < 7; i++) {
        if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
            inst.setValue(i, originalInstances[selected][i] == 0 ? 1 : 0);
        } else {
            inst.setValue(i, originalInstances[selected][i]);
        }
    }
    if (!this.suppressIrrelevantAttributesOption.isSet()) {
        for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) {
            inst.setValue(i + 7, this.instanceRandom.nextInt(2));
        }
    }
    inst.setClassValue(selected);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.RandomRBFGenerator.java

License:Open Source License

public Instance nextInstance() {
    Centroid centroid = this.centroids[MiscUtils.chooseRandomIndexBasedOnWeights(this.centroidWeights,
            this.instanceRandom)];
    int numAtts = this.numAttsOption.getValue();
    double[] attVals = new double[numAtts + 1];
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = (this.instanceRandom.nextDouble() * 2.0) - 1.0;
    }//from  w ww.j a  va2s  . co  m
    double magnitude = 0.0;
    for (int i = 0; i < numAtts; i++) {
        magnitude += attVals[i] * attVals[i];
    }
    magnitude = Math.sqrt(magnitude);
    double desiredMag = this.instanceRandom.nextGaussian() * centroid.stdDev;
    double scale = desiredMag / magnitude;
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = centroid.centre[i] + attVals[i] * scale;
    }
    Instance inst = new DenseInstance(1.0, attVals);
    inst.setDataset(getHeader());
    inst.setClassValue(centroid.classLabel);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.RandomTreeGenerator.java

License:Open Source License

public Instance nextInstance() {
    double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()];
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    for (int i = 0; i < attVals.length; i++) {
        attVals[i] = i < this.numNominalsOption.getValue()
                ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue())
                : this.instanceRandom.nextDouble();
        inst.setValue(i, attVals[i]);//from   www.  j a  va  2s  . com
    }
    inst.setDataset(header);
    inst.setClassValue(classifyInstance(this.treeRoot, attVals));
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.SEAGenerator.java

License:Open Source License

public Instance nextInstance() {
    double attrib1 = 0, attrib2 = 0, attrib3 = 0;
    int group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        attrib1 = 10 * this.instanceRandom.nextDouble();
        attrib2 = 10 * this.instanceRandom.nextDouble();
        attrib3 = 10 * this.instanceRandom.nextDouble();

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(attrib1, attrib2,
                attrib3);//from  w  w  w  . j av a 2s .  c  o  m
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }
    //Add Noise
    if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
        group = (group == 0 ? 1 : 0);
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, attrib1);
    inst.setValue(1, attrib2);
    inst.setValue(2, attrib3);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.STAGGERGenerator.java

License:Open Source License

public Instance nextInstance() {

    int size = 0, color = 0, shape = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        size = this.instanceRandom.nextInt(3);
        color = this.instanceRandom.nextInt(3);
        shape = this.instanceRandom.nextInt(3);

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;/*  w w w  .j a va2 s  .  c  o  m*/
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, size);
    inst.setValue(1, color);
    inst.setValue(2, shape);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.WaveformGenerator.java

License:Open Source License

public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);/* w  ww .ja v  a2s. c o  m*/
    int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
    int choiceA = 0, choiceB = 0;
    switch (waveform) {
    case 0:
        choiceA = 0;
        choiceB = 1;
        break;
    case 1:
        choiceA = 0;
        choiceB = 2;
        break;
    case 2:
        choiceA = 1;
        choiceB = 2;
        break;
    default:
        break;
    }
    double multiplierA = this.instanceRandom.nextDouble();
    double multiplierB = 1.0 - multiplierA;
    for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
        inst.setValue(i, (multiplierA * hFunctions[choiceA][i]) + (multiplierB * hFunctions[choiceB][i])
                + this.instanceRandom.nextGaussian());
    }
    if (this.addNoiseOption.isSet()) {
        for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
            inst.setValue(i, this.instanceRandom.nextGaussian());
        }
    }
    inst.setClassValue(waveform);
    return inst;
}

From source file:tubes1.Main.java

/**
 * @param args the command line arguments
 *///from  w  w  w .ja v a 2s.  c o  m
public static void main(String[] args) throws IOException, Exception {
    // TODO code application logic here
    String filename = "weather";

    //Masih belum mengerti tipe .csv yang dapat dibaca seperti apa
    //CsvToArff convert = new CsvToArff(filename+".csv");

    //LOAD FILE
    BufferedReader datafile = readDataFile("src/" + filename + ".arff");
    Instances data = new Instances(datafile);
    data.setClassIndex(data.numAttributes() - 1);
    //END OF LOAD FILE

    CustomFilter fil = new CustomFilter();

    //REMOVE USELESS ATTRIBUTE
    data = fil.removeAttribute(data);
    System.out.println(data);

    Instances[] allData = new Instances[4];
    //data for Id3
    allData[0] = fil.resampling(fil.convertNumericToNominal(data));
    //data for J48
    allData[1] = fil.convertNumericToNominal(fil.resampling(data));
    //data for myId3
    allData[2] = allData[0];
    //data for myC4.5
    allData[3] = fil.resampling(fil.convertNumericToNominal(fil.convertNumericRange(data)));

    data = fil.convertNumericToNominal(data);
    // BUILD CLASSIFIERS
    Classifier[] models = { new Id3(), //C4.5
            new J48(), new myID3(), new myC45() };

    for (int j = 0; j < models.length; j++) {
        FastVector predictions = new FastVector();
        //FOR TEN-FOLD CROSS VALIDATION
        Instances[][] split = crossValidationSplit(allData[j], 10);
        // Separate split into training and testing arrays
        Instances[] trainingSplits = split[0];
        Instances[] testingSplits = split[1];
        System.out.println("\n---------------------------------");
        for (int i = 0; i < trainingSplits.length; i++) {
            try {
                //                    System.out.println("Building for training Split : " + i);
                Evaluation validation = classify(models[j], trainingSplits[i], testingSplits[i]);

                predictions.appendElements(validation.predictions());

                // Uncomment to see the summary for each training-testing pair.
                //                    System.out.println(models[j].toString());
            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            // Calculate overall accuracy of current classifier on all splits
            double accuracy = calculateAccuracy(predictions);

            // Print current classifier's name and accuracy in a complicated,
            // but nice-looking way.
            System.out.println(String.format("%.2f%%", accuracy));
        }
        models[j].buildClassifier(allData[j]);
        Model.save(models[j], models[j].getClass().getSimpleName());
    }

    //test instance
    Instances trainingSet = new Instances("Rel", getFvWekaAttributes(data), 10);
    trainingSet.setClassIndex(data.numAttributes() - 1);

    Instance testInstance = new Instance(data.numAttributes());
    for (int i = 0; i < data.numAttributes() - 1; i++) {
        System.out.print("Masukkan " + data.attribute(i).name() + " : ");
        Scanner in = new Scanner(System.in);
        String att = in.nextLine();
        if (isNumeric(att)) {
            att = fil.convertToFit(att, data, i);
        }
        testInstance.setValue(data.attribute(i), att);
    }

    //        System.out.println(testInstance);
    //        System.out.println(testInstance.classAttribute().index());

    trainingSet.add(testInstance);

    Classifier Id3 = Model.load("Id3");
    Classifier J48 = Model.load("J48");
    Classifier myID3 = Model.load("myID3");
    Classifier MyC45 = Model.load("myC45");
    //        Classifier MyId3 = Model.load("myID3");

    Instance A = trainingSet.instance(0);
    Instance B = trainingSet.instance(0);
    Instance C = trainingSet.instance(0);
    Instance D = trainingSet.instance(0);

    //test with ID3 WEKA
    A.setClassValue(Id3.classifyInstance(trainingSet.instance(0)));
    System.out.println("Id3 Weka : " + A);

    //test with C4.5 WEKA
    B.setClassValue(J48.classifyInstance(trainingSet.instance(0)));
    System.out.println("C4.5 Weka : " + B);

    //test with my C4.5
    C.setClassValue(MyC45.classifyInstance(trainingSet.instance(0)));
    System.out.println("My C4.5 : " + C);

    //test with my ID3
    D.setClassValue(myID3.classifyInstance(trainingSet.instance(0)));
    System.out.println("My ID3 : " + D);
}