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:srl.recognition.handwriting.StrokePixelator.java

License:BSD License

/**
 * Returns a Weka instance of a sketch (using the pixelize function))
 * //  w w w .  j a v a  2s .  c  o m
 * @param sk
 * @param heightWidth
 * @return
 */
public static Instance getInstance(Sketch sk, int heightWidth, Instances dataSet) {

    int[][] pixels = pixelizeSketch(sk, heightWidth);

    Instance inst = new Instance(dataSet.numAttributes());

    inst.setDataset(dataSet);

    int count = 0;

    for (int i = 0; i < pixels.length; i++) {
        String holder = "";
        for (int j = 0; j < pixels.length; j++) {
            if (pixels[j][i] == 1)
                inst.setValue(dataSet.attribute("Pixel" + count), 1);
            else
                inst.setValue(dataSet.attribute("Pixel" + count), -1);
            holder += String.valueOf(pixels[j][i]);
            count++;
        }
        log.debug(holder);
    }
    return inst;
}

From source file:srl.recognition.handwriting.StrokePixelator.java

License:BSD License

/**
 * Returns a Weka instance of a sketch for CivilSketch attributes
 * //from   w  w w  . ja v a 2s.  co m
 * @param sk
 * @param heightWidth
 * @return
 */
public static Instance getInstanceCivilSketch(Sketch sk, Instances dataSet) {

    Instance inst = new Instance(8);
    inst.setDataset(dataSet);

    return inst;
}

From source file:statechum.analysis.learning.experiments.PairSelection.WekaDataCollector.java

License:Open Source License

/**  Constructs a Weka {@link Instance} for a pair of interest.
 * //from  w  ww . ja  va 2s .c  om
 * @param comparisonResults metrics related to the considered pair.
 * @param classification whether this is a correct pair
 * @return an instance of a test or a training sample. 
 */
Instance constructInstance(int[] comparisonResults, boolean classification) {
    if (comparisonResults.length != instanceLength)
        throw new IllegalArgumentException("results' length does not match the number of comparators");

    double[] instanceValues = new double[instanceLength + 1];
    for (int i = 0; i < instanceLength; ++i)
        instanceValues[i] = convertAssessmentResultToString(comparisonResults[i], attributesOfAnInstance[i]);

    instanceValues[instanceLength] = trainingData.classAttribute()
            .indexOfValue(Boolean.toString(classification));
    Instance outcome = new Instance(1, instanceValues);
    outcome.setDataset(trainingData);
    return outcome;
}

From source file:Statistics.WekaFunctions.java

public Instance selectTestInstance() {
    //Select last instance from loaded set of Test Instances

    //Create test instance
    //Instance testInstance = new Instance(newDataTest.firstInstance());
    //Instance testInstance = new Instance(newDataTest.instance(0));
    Instance testInstance = new Instance(RF_testInstances.lastInstance());
    //System.out.println("-selecting last instance in test set RF_testInstances, done");

    // Specify that the instance belong to the training set 
    // in order to inherit from the set description                                
    testInstance.setDataset(trainParameters);
    System.out.println("-selected last instance in test set: " + testInstance.toString());

    return testInstance;
}

From source file:SupervisedMetablocking.AbstractSupervisedMetablocking.java

License:Open Source License

protected Instance getFeatures(int match, List<Integer> commonBlockIndices, Comparison comparison) {
    double[] instanceValues = new double[noOfAttributes];

    int entityId2 = comparison.getEntityId2() + entityIndex.getDatasetLimit();

    double ibf1 = Math.log(noOfBlocks / entityIndex.getNoOfEntityBlocks(comparison.getEntityId1(), 0));
    double ibf2 = Math.log(noOfBlocks / entityIndex.getNoOfEntityBlocks(comparison.getEntityId2(), 1));
    instanceValues[0] = commonBlockIndices.size() * ibf1 * ibf2;

    double raccb = 0;
    for (Integer index : commonBlockIndices) {
        raccb += 1.0 / comparisonsPerBlock[index];
    }//from w w  w .j  a v a2s  .c  o m
    if (raccb < 1.0E-6) {
        raccb = 1.0E-6;
    }
    instanceValues[1] = raccb;

    instanceValues[2] = commonBlockIndices.size()
            / (redundantCPE[comparison.getEntityId1()] + redundantCPE[entityId2] - commonBlockIndices.size());
    instanceValues[3] = nonRedundantCPE[comparison.getEntityId1()];
    instanceValues[4] = nonRedundantCPE[entityId2];
    instanceValues[5] = match;

    Instance newInstance = new DenseInstance(1.0, instanceValues);
    newInstance.setDataset(trainingInstances);
    return newInstance;
}

From source file:tclass.clusteralg.EMClusterer.java

License:Open Source License

public float prob(Instance inst, int cluster) {
    try {/*from  w  ww  . ja va  2s  . c o m*/
        inst.setDataset(stuff);
        float retval = (float) em.probInstanceInCluster(inst, cluster);
        return retval;
    } catch (Exception e) {
        System.err.println("AAAARGH!!! Could not get distribution f for instance!");
        e.printStackTrace();
    }
    return 0;
}

From source file:tclass.clusteralg.EMClusterer.java

License:Open Source License

public ClusterMem findBestLabel(EventI ev) {
    // System.out.println("Trying to find best label for " + ev); 

    Instance inst = WekaBridge.makeInstance(ev, edi, ignoreTime);
    inst.setDataset(stuff);
    try {/*w  ww  . j a v  a  2s  .  com*/
        int cluster = em.clusterInstance(inst);
        float conf = (float) em.probInstanceInCluster(inst, cluster);
        // if(conf > 1){
        //    System.out.println("WTF?? Label is " + (new ClusterMem(elAt(cluster), conf)).toString() );               //  }
        return new ClusterMem(elAt(cluster), conf);

    } catch (Exception e) {
        System.out.println("Label finding failed. AAAAAARGH!!!");
        e.printStackTrace();
        return null;
    }
}

From source file:tclass.WekaBridge.java

License:Open Source License

/** 
 *  Makes an instances objects. /*from ww w .j  a  v a2  s . c o m*/
 */

public static Instances makeInstances(ClassStreamEventsVecI csevi, ClassDescVecI cdvi, EventDescVecI edvi,
        int evPos, boolean ignoreClasses, boolean ignoreTime) throws Exception {
    // System.out.println("Asked to ignore classes."); 

    Instances retval;
    // Ok, first convert attributes. 
    StreamEventsVecI sevi = csevi.getStreamEventsVec();
    ClassificationVecI cvi = csevi.getClassVec();
    // System.out.println("Event Desc Vec = " + edvi); 
    int numAtts = edvi.elAt(evPos).numParams();
    if (ignoreTime) {
        numAtts = numAtts - 1;
    }
    FastVector atts = makeAttVector(edvi.elAt(evPos), ignoreTime);
    if (!ignoreClasses) {
        int size = cdvi.size();
        FastVector classes = new FastVector(size);
        for (int i = 0; i < size; i++) {
            classes.addElement(cdvi.getClassLabel(i));
        }
        atts.addElement(new Attribute("class", classes));
    }
    int size = csevi.size();
    retval = new Instances(edvi.elName(evPos), atts, size);
    if (!ignoreClasses) {
        retval.setClassIndex(numAtts);
    } else {
        retval.setClassIndex(-1);
    }
    for (int i = 0; i < size; i++) {
        // Get events of this type: 
        EventVecI evi = sevi.elAt(i).getEvents(evPos);
        int numEvents = evi.size();
        for (int j = 0; j < numEvents; j++) {
            // System.out.println("Adding event " + j + " of stream " + i); 
            Instance thisInst = new DenseInstance(atts.size());
            thisInst.setDataset(retval);
            EventI thisEvent = evi.elAt(j);

            for (int k = (ignoreTime ? 1 : 0); k < edvi.elAt(evPos).numParams(); k++) {
                thisInst.setValue(k - (ignoreTime ? 1 : 0), thisEvent.valOf(k));
            }
            if (!ignoreClasses) {
                thisInst.setValue(numAtts, cdvi.getClassLabel(cvi.elAt(i).getRealClass()));
            }
            retval.add(thisInst);
        }
    }
    return retval;
}

From source file:tclass.WekaBridge.java

License:Open Source License

public static Instances makeInstances(ClassStreamAttValVecI csavvi, String name) throws Exception {
    StreamAttValVecI origData = csavvi.getStreamAttValVec();
    AttDescVecI format = origData.getDescription();
    ClassificationVecI classes = csavvi.getClassVec();
    ClassDescVecI classInfo = classes.getClassDescVec();
    FastVector instanceDesc = makeAttVector(format, classInfo);
    int numInstances = origData.size();
    int numAtts = format.size();
    Instances retval = new Instances(name, instanceDesc, numInstances);
    retval.setClassIndex(numAtts); // Set class to last attribute. 

    for (int i = 0; i < numInstances; i++) {
        Instance thisInst = new DenseInstance(numAtts + 1); // To include the class.  
        thisInst.setDataset(retval);
        StreamAttValI thisStream = origData.elAt(i);
        for (int j = 0; j < numAtts; j++) {
            thisInst.setValue(j, thisStream.getAtt(j));
        }/*from  w w  w.j  a va2  s  . c o m*/
        thisInst.setValue(numAtts, classInfo.getClassLabel(classes.elAt(i).getRealClass()));
        retval.add(thisInst);
    }
    return retval;
}

From source file:tr.gov.ulakbim.jDenetX.streams.clustering.RandomRBFGeneratorEvents.java

License:Open Source License

public Instance nextInstance() {
    numGeneratedInstances++;// ww w .jav a2s.co m
    eventScheduler();

    //make room for thge classlabel
    double[] values_new = new double[numAttsOption.getValue() + 1];
    double[] values = null;
    int clusterChoice = -1;

    if (instanceRandom.nextDouble() > noiseLevelOption.getValue()) {
        clusterChoice = chooseWeightedElement();
        values = kernels.get(clusterChoice).generator.sample(instanceRandom).toDoubleArray();
    } else {
        //get ranodm noise point
        values = getNewSample();
    }

    if (Double.isNaN(values[0])) {
        System.out.println("Instance corrupted:" + numGeneratedInstances);
    }
    System.arraycopy(values, 0, values_new, 0, values.length);

    Instance inst = new DenseInstance(1.0, values_new);
    inst.setDataset(getHeader());
    if (clusterChoice == -1) {
        inst.setClassValue(-1);
    } else {
        inst.setClassValue(kernels.get(clusterChoice).generator.getId());
        //Do we need micro cluster representation if have overlapping clusters?
        //if(!overlappingOption.isSet())
        kernels.get(clusterChoice).addInstance(inst);
    }
    //        System.out.println(numGeneratedInstances+": Overlap is"+updateOverlaps());

    return inst;
}