Example usage for weka.core Instance value

List of usage examples for weka.core Instance value

Introduction

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

Prototype

public double value(Attribute att);

Source Link

Document

Returns an instance's attribute value in internal format.

Usage

From source file:newdtl.NewID3.java

/**
 * Classifies a given test instance using the decision tree.
 *
 * @param instance the instance to be classified
 * @return the classification//from  ww w.  j a v  a  2s. c o m
 * @throws NoSupportForMissingValuesException if instance has missing values
 */
@Override
public double classifyInstance(Instance instance) throws NoSupportForMissingValuesException {

    if (instance.hasMissingValue()) {
        throw new NoSupportForMissingValuesException("NewID3: Cannot handle missing values");
    }
    if (splitAttribute == null) {
        return label;
    } else {
        return children[(int) instance.value(splitAttribute)].classifyInstance(instance);
    }
}

From source file:newdtl.NewID3.java

/**
 * Computes class distribution for instance using decision tree.
 *
 * @param instance the instance for which distribution is to be computed
 * @return the class distribution for the given instance
 * @throws NoSupportForMissingValuesException if instance has missing values
 *///ww  w .java 2  s . co  m
@Override
public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException {

    if (instance.hasMissingValue()) {
        throw new NoSupportForMissingValuesException("NewID3: Cannot handle missing values");
    }
    if (splitAttribute == null) {
        return classDistributions;
    } else {
        return children[(int) instance.value(splitAttribute)].distributionForInstance(instance);
    }
}

From source file:newdtl.NewJ48.java

/**
 * Classifies a given test instance using the decision tree.
 *
 * @param instance the instance to be classified
 * @return the classification// w w w .j  a v a  2s . c o m
 */
@Override
public double classifyInstance(Instance instance) throws NoSupportForMissingValuesException {
    if (instance.hasMissingValue()) {
        throw new NoSupportForMissingValuesException("NewID3: Cannot handle missing values");
    }

    if (splitAttribute == null) {
        return label;
    } else {
        if (splitAttribute.isNumeric()) {
            if (Double.compare(instance.value(splitAttribute), splitThreshold) <= 0) {
                return children[0].classifyInstance(instance);
            } else {
                return children[1].classifyInstance(instance);
            }
        } else {
            return children[(int) instance.value(splitAttribute)].classifyInstance(instance);
        }
    }
}

From source file:newdtl.NewJ48.java

/**
 * Computes class distribution for instance using decision tree.
 *
 * @param instance the instance for which distribution is to be computed
 * @return the class distribution for the given instance
 *///from  w w  w .  ja  va2  s . com
@Override
public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException {
    if (instance.hasMissingValue()) {
        throw new NoSupportForMissingValuesException("NewID3: Cannot handle missing values");
    }

    if (splitAttribute == null) {
        return normalize(classDistributions);
    } else {
        if (splitAttribute.isNumeric()) {
            if (Double.compare(instance.value(splitAttribute), splitThreshold) <= 0) {
                return children[0].distributionForInstance(instance);
            } else {
                return children[1].distributionForInstance(instance);
            }
        } else {
            return children[(int) instance.value(splitAttribute)].distributionForInstance(instance);
        }
    }
}

From source file:newdtl.NewJ48.java

/**
 * search index of attribute that has most common value
 *
 * @param data the data for searching/*from  w ww . j av a2s  . co  m*/
 * @param att the attribute for searching
 * @return index of attribute that has most common value
 */
private int modusIndex(Instances data, Attribute att) {
    // cari modus
    int[] modus = new int[att.numValues()];
    Enumeration dataEnumeration = data.enumerateInstances();

    while (dataEnumeration.hasMoreElements()) {
        Instance inst = (Instance) dataEnumeration.nextElement();
        if (!inst.isMissing(att)) {
            modus[(int) inst.value(att)]++;
        }
    }

    // cari modus terbesar
    int indexMax = 0;
    for (int i = 1; i < modus.length; ++i) {
        if (modus[i] > modus[indexMax]) {
            indexMax = i;
        }
    }
    return indexMax;
}

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 //from   w  w  w.  ja v a 2  s.  c  o m
 */
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.deidentifier.arx.ARFF2ARX.java

License:Open Source License

/**
 *
 * @param instance//from  w  w w .  ja  v  a2  s  .co m
 * @return
 */
protected String[] convertRow(Instance instance) {
    String[] row = new String[instance.numAttributes()];
    for (int i = 0; i < instance.numAttributes(); i++) {
        if (instance.attribute(i).type() == Attribute.NOMINAL
                || instance.attribute(i).type() == Attribute.STRING) {
            row[i] = instance.stringValue(i);
        } else {
            row[i] = String.valueOf((int) instance.value(i));
        }
    }
    return row;
}

From source file:org.dkpro.similarity.ml.filters.LogFilter.java

License:Open Source License

@Override
protected Instance process(Instance inst) throws Exception {
    Instance newInst = new DenseInstance(inst.numAttributes());

    newInst.setValue(0, inst.value(0));

    for (int i = 1; i < inst.numAttributes() - 1; i++) {
        double newVal = Math.log(inst.value(i) + 1);
        // double newVal = inst.value(i);               // Passthrough

        newInst.setValue(i, newVal);//from w w w.j a v  a  2s.  co  m
    }

    newInst.setValue(inst.numAttributes() - 1, inst.value(inst.numAttributes() - 1));

    return newInst;
}

From source file:org.if4071.myann.TopologyModel.java

public void insertDataToInputNodes(Instance inputData) {
    for (int i = 0; i < inputData.numAttributes() - 1; i++) {
        nodes.get(i).setOutput(inputData.value(i));
    }/*  w ww  .ja  v a 2 s.  c om*/
}

From source file:org.iobserve.analysis.behavior.clustering.birch.model.ClusteringFeature.java

License:Apache License

/** Constructor for a clustering feature
 * representing an instance vector./*w  w w. j av  a  2s  .c om*/
 * @param instance the instance to represent.
 */
public ClusteringFeature(final Instance instance) {
    this.number = 1;
    this.linearSum = new double[instance.numAttributes()];
    this.squareSum = new double[instance.numAttributes()];

    for (int i = 0; i < instance.numAttributes(); i++) {
        this.linearSum[i] = instance.value(i);
        this.squareSum[i] = this.linearSum[i] * this.linearSum[i];
    }
}