Example usage for weka.core Instance deleteAttributeAt

List of usage examples for weka.core Instance deleteAttributeAt

Introduction

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

Prototype

public void deleteAttributeAt(int position);

Source Link

Document

Deletes an attribute at the given position (0 to numAttributes() - 1).

Usage

From source file:moa.streams.clustering.FileStream.java

License:Apache License

protected boolean readNextInstanceFromFile() {
    try {//ww  w . j a v  a  2 s  .  co  m

        if (this.instances.readInstance(this.fileReader)) {
            Instance rawInstance = this.instances.instance(0);

            //remove dataset from instance so we can delete attributes
            rawInstance.setDataset(null);
            for (int i = removeAttributes.length - 1; i >= 0; i--) {
                rawInstance.deleteAttributeAt(removeAttributes[i]);
            }
            //set adjusted dataset for instance
            rawInstance.setDataset(filteredDataset);

            if (normalizeOption.isSet() && valuesMinMaxDiff != null) {
                for (int i = 0; i < rawInstance.numAttributes(); i++) {
                    if (valuesMinMaxDiff.get(i)[2] != 1 && // Already normalized
                            valuesMinMaxDiff.get(i)[2] != 0 && // Max. value is 0 (unable to be normalized)
                            i != rawInstance.classIndex()) { // Class label is not subject to be normalized
                        double v = rawInstance.value(i);
                        v = (v - valuesMinMaxDiff.get(i)[0]) / valuesMinMaxDiff.get(i)[2];
                        rawInstance.setValue(i, v);
                    }
                }
            }

            this.lastInstanceRead = rawInstance;
            this.instances.delete(); // keep instances clean
            this.numInstancesRead++;
            return true;
        }
        if (this.fileReader != null) {
            this.fileReader.close();
            this.fileReader = null;
        }
        return false;
    } catch (IOException ioe) {
        throw new RuntimeException("ArffFileStream failed to read instance from stream.", ioe);
    }
}

From source file:mulan.transformations.BinaryRelevanceTransformation.java

License:Open Source License

/**
 * Remove all label attributes except labelToKeep
 * @param instance // ww w  .j  ava  2s  .  c  om
 * @param labelToKeep 
 * @return transformed Instance
 */
public Instance transformInstance(Instance instance, int labelToKeep) {
    Instance newInstance = DataUtils.createInstance(instance, instance.numAttributes());
    newInstance.setDataset(null);
    int numPredictors = instance.numAttributes() - numOfLabels;
    int skipLabel = 0;
    for (int labelIndex = 0; labelIndex < numOfLabels; labelIndex++) {
        if (labelIndex == labelToKeep) {
            skipLabel++;
            continue;
        }
        newInstance.deleteAttributeAt(numPredictors + skipLabel);
    }
    return newInstance;
}

From source file:nl.bioinf.roelen.thema11.classifier_tools.ClassifierUser.java

License:Open Source License

/**
 * use the classifier to test the sequences in a genbank or fasta file for boundaries
 * @param fileLocation the location of the genbank of fasta file
 * @param classifier the classifier to use
 * @return /* w w  w  .  j  a  va  2s .c  om*/
 */
public static ArrayList<ClassifiedNucleotide> getPossibleBoundaries(String fileLocation,
        Classifier classifier) {
    ArrayList<Gene> genesFromFile = new ArrayList<>();
    ArrayList<ClassifiedNucleotide> classifiedNucleotides = new ArrayList<>();
    //read from fasta
    if (fileLocation.toUpperCase().endsWith(".FASTA") || fileLocation.toUpperCase().endsWith(".FA")
            || fileLocation.toUpperCase().endsWith(".FAN")) {
        genesFromFile.addAll(readFasta(fileLocation));
    }
    //read from genbank
    else if (fileLocation.toUpperCase().endsWith(".GENBANK") || fileLocation.toUpperCase().endsWith(".GB")) {
        GenBankReader gbr = new GenBankReader();
        gbr.readFile(fileLocation);
        GenbankResult gbresult = gbr.getResult();
        genesFromFile = gbresult.getGenes();
    }
    //get the test data
    HashMap<String, ArrayList<IntronExonBoundaryTesterResult>> geneTestResults;
    geneTestResults = TestGenes.testForIntronExonBoundaries(genesFromFile, 1);
    ArrayList<InstanceToClassify> instanceNucs = new ArrayList<>();
    try {
        //write our results to a temporary file
        File tempArrf = File.createTempFile("realSet", ".arff");
        ArffWriter.write(tempArrf.getAbsolutePath(), geneTestResults, null);
        //get data
        ConverterUtils.DataSource source = new ConverterUtils.DataSource(tempArrf.getAbsolutePath());
        //SET DATA AND OPTIONS
        Instances data = source.getDataSet();
        for (int i = 0; i < data.numInstances(); i++) {
            Instance in = data.instance(i);
            //get the name of the gene or sequence tested
            String nameOfInstance = in.stringValue(in.numAttributes() - 3);
            //get the tested position
            int testedPosition = (int) in.value(in.numAttributes() - 2);
            //set the class as missing, because we want to find it
            in.setMissing((in.numAttributes() - 1));

            Instance instanceNoExtras = new Instance(in);

            //delete the name and position, they are irrelevant for classifying
            instanceNoExtras.deleteAttributeAt(instanceNoExtras.numAttributes() - 2);
            instanceNoExtras.deleteAttributeAt(instanceNoExtras.numAttributes() - 2);
            InstanceToClassify ic = new InstanceToClassify(instanceNoExtras, testedPosition, nameOfInstance);
            instanceNucs.add(ic);
        }
        for (InstanceToClassify ic : instanceNucs) {
            Instance in = ic.getInstance();
            in.setDataset(data);
            data.setClassIndex(data.numAttributes() - 1);
            //classify our instance
            classifier.classifyInstance(in);
            //save the likelyhood something is part of something
            double likelyhoodBoundary = classifier.distributionForInstance(in)[0];
            double likelyhoodNotBoundary = classifier.distributionForInstance(in)[1];

            //create a classified nucleotide and give it the added data
            ClassifiedNucleotide cn = new ClassifiedNucleotide(likelyhoodBoundary, likelyhoodNotBoundary,
                    ic.getName(), ic.getPosition());
            classifiedNucleotides.add(cn);
        }

    } catch (IOException ex) {
        Logger.getLogger(ClassifierUser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(ClassifierUser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return classifiedNucleotides;
}

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

License:Open Source License

private Instances toSingleInstanceDataset(Instances miData, Instances flatData) throws Exception {
    MultiInstanceToPropositional convertToProp = new MultiInstanceToPropositional();

    convertToProp.setInputFormat(miData);

    for (int i = 0; i < miData.numInstances(); i++) {
        convertToProp.input(miData.instance(i));
    }/* w ww. j  a v  a  2 s  .  c om*/
    convertToProp.batchFinished();

    if (flatData == null) {
        flatData = convertToProp.getOutputFormat();
        flatData.deleteAttributeAt(0); // remove the bag index attribute

    }

    Instance processed;
    while ((processed = convertToProp.output()) != null) {
        processed.setDataset(null);
        processed.deleteAttributeAt(0); // remove the bag index attribute
        flatData.add(processed);
    }

    // remove class attribute
    // flatData.setClassIndex(-1);
    // flatData.deleteAttributeAt(flatData.numAttributes() - 1);

    // set weights
    int instanceIdx = 0;
    for (Instance bag : miData) {
        for (Instance instance : bag.relationalValue(1)) {
            flatData.get(instanceIdx).setWeight(instance.weight());
            instanceIdx++;
        }
    }
    return flatData;
}

From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java

License:Open Source License

/**
 * generate instances for classifier classIdx
 *
 * @param p_Instances//from w  w w  .  j  a  va2 s.  c o  m
 *            input instances
 * @param p_ClassIndex
 *            class index
 * @param p_ID2Classes
 *            instance ids
 * @return new instances
 */
protected Instances genInstances(Instances p_Instances, double p_ClassIndex,
        Hashtable<String, ArrayList<Double>> p_ID2Classes) {
    Instances newInsts = new Instances(this.m_OutputFormat, 0);
    for (int i = 0; i < p_Instances.numInstances(); i++) {
        Instance inst = p_Instances.instance(i);
        Instance newInst = null;
        if (SparseInstance.class.isInstance(inst)) {
            newInst = new SparseInstance(inst);
        } else {
            newInst = new Instance(inst);
        }
        if (newInst.value(p_Instances.classIndex()) == p_ClassIndex) {
            newInst.setValue(inst.classIndex(), 1);
        } else {
            if (p_ID2Classes == null || !p_ID2Classes.get(inst.stringValue(this.m_IndexOfID))
                    .contains(new Double(p_ClassIndex))) {
                newInst.setValue(inst.classIndex(), 0);
            } else {
                continue;
            }
        }
        newInst.deleteAttributeAt(this.m_IndexOfID);
        newInst.setDataset(newInsts);
        newInsts.add(newInst);
    }
    return newInsts;
}

From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java

License:Open Source License

/**
 * filter instance//from  www.  j  ava  2  s.  c  o  m
 *
 * @param p_Instance
 *            input instance
 * @return filtered instance
 */
protected Instance filterInstance(Instance p_Instance) {
    Instance retVal = null;
    if (SparseInstance.class.isInstance(p_Instance)) {
        retVal = new SparseInstance(p_Instance);
    } else {
        retVal = new Instance(p_Instance);
    }
    if (this.m_IndexOfID >= 0) {
        retVal.deleteAttributeAt(this.m_IndexOfID);
    }
    return retVal;
}