Example usage for weka.core Instance numAttributes

List of usage examples for weka.core Instance numAttributes

Introduction

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

Prototype

public int numAttributes();

Source Link

Document

Returns the number of attributes.

Usage

From source file:myid3andc45classifier.Model.MyC45.java

@Override
public double classifyInstance(Instance instance) {
    int i = 0;//from   w  ww  .  java  2  s .c  om
    if (attribute == null) {
        return label;
    } else {
        boolean numeric = false;
        for (int j = 0; j < instance.numAttributes(); j++) {
            if (instance.attribute(j).isNumeric()) {
                if (instance.attribute(j).name().equalsIgnoreCase(attribute.name().split(" ")[0])) {
                    numeric = true;
                    break;
                }
                i++;
            }
        }
        if (numeric) {
            double threshold = Double.parseDouble(attribute.name().split(" ")[1]);
            //System.out.println("WOWW!!! " + attribute.name() + " threshold is " + threshold);
            double val = (double) instance.value(i);
            if (val <= threshold) {
                return successors[(int) attribute.indexOfValue("<=" + threshold)].classifyInstance(instance);
                //instance.setValue(attribute, "<="+threshold);
            } else {
                return successors[(int) attribute.indexOfValue(">" + threshold)].classifyInstance(instance);
                //instance.setValue(attribute, ">"+threshold);
            }
        }

        return successors[(int) instance.value(attribute)].classifyInstance(instance);
    }

}

From source file:NaiveBayes.NaiveBayes.java

@Override
public double classifyInstance(Instance last) {
    double prob[] = new double[last.classAttribute().numValues()];
    for (int classIndex = 0; classIndex < last.attribute(last.classIndex()).numValues(); classIndex++) {//classifikasi
        double temp = 1;
        int i = 0;

        for (Atribut attr : getList()) {
            if (i == last.classIndex())
                i++;/*from   ww  w  .  j a v  a 2s  .c  om*/
            //System.out.println(attr.getName()+"="+last.attribute(i).name());
            temp *= attr.getFrekuensiNilai(last.attribute(last.classIndex()).value(classIndex),
                    last.toString(i), last.value(i), last.attribute(i).isNumeric()) / numEachClass[classIndex];
            i++;
        }
        double res;
        res = numEachClass[classIndex] / last.numAttributes() * temp;
        prob[classIndex] = res;
    }
    return maxIndex(prob);
}

From source file:NaiveBayesPckge.mushClass.java

@Override
public double classifyInstance(Instance instance) throws java.lang.Exception {
    double classify = 0;
    // banyaknya kesimpulan. Misal T dan F berati ada 2
    int numClasses = instance.numClasses();
    double[] out = new double[numClasses];
    //banyaknya kelas yang diuji
    int class_index = instance.classIndex();
    //banyaknya atribut
    int num_attributes = instance.numAttributes();
    double inputs[] = new double[num_attributes];

    for (int i = 0; i < numClasses; i++) {
        out[i] = probabConclusion[i];/*from w w  w  .  j  a  va 2s.c  o  m*/
        for (int j = 0; j < num_attributes - 1; j++) {
            int indexLabel = searchIndexLabel(j, instance.stringValue(j));
            out[i] *= (double) atribNom[j].getAttribObjectType(indexLabel, i);
        }
    }

    classify = getIndexBiggestProbability(out);

    return classify;
}

From source file:NaiveBayesPckge.mushClass.java

@Override
public double[] distributionForInstance(Instance instance) throws Exception {

    // banyaknya kesimpulan. Misal T dan F berati ada 2
    int numClasses = instance.numClasses();

    double[] out = new double[numClasses];
    //banyaknya kelas yang diuji
    int class_index = instance.classIndex();
    //banyaknya atribut
    int num_attributes = instance.numAttributes();
    double inputs[] = new double[num_attributes];

    for (int i = 0; i < numClasses; i++) {

        out[i] = probabConclusion[i];//from   w w  w  . j  av a 2 s.c om

        //            System.out.print("\n" + maxIterasi +". out["+i+"] = ");
        for (int j = 1; j < num_attributes; j++) {
            int indexLabel = searchIndexLabel(j, instance.stringValue(j));

            out[i] *= (double) atribNom[j].getAttribObjectType(indexLabel, i);

            //                System.out.print(atribNom[j].getAttribObjectType(indexLabel, i) + "*");
        }

        //            System.out.println("\nout["+i+"] = "+out[i]);
        //            System.out.println(instance.toString());
    }

    //        maxIterasi++;
    return out;
}

From source file:NaiveBayesPckge.NaiveBayesCode.java

@Override
public double[] distributionForInstance(Instance instance) throws Exception {

    // banyaknya kesimpulan. Misal T dan F berati ada 2
    int numClasses = instance.numClasses();
    double[] out = new double[numClasses];
    //banyaknya kelas yang diuji
    int class_index = instance.classIndex();
    //banyaknya atribut
    int num_attributes = instance.numAttributes();
    double inputs[] = new double[num_attributes];

    for (int i = 0; i < numClasses; i++) {

        out[i] = probabConclusion[i];//from www. j  a  va  2s  .  c om
        //            System.out.print("\n" + maxIterasi +". out["+i+"] = ");
        for (int j = 0; j < num_attributes - 1; j++) {
            int indexLabel = searchIndexLabel(j, instance.stringValue(j));
            out[i] *= (double) atribNom[j].getAttribObjectType(indexLabel, i);
            //                System.out.print(atribNom[j].getAttribObjectType(indexLabel, i) + "*");
        }
        //            System.out.println("\nout["+i+"] = "+out[i]);
        //            System.out.println(instance.toString());
    }
    //        maxIterasi++;
    return out;
}

From source file:net.paudan.evosvm.LibLINEAR.java

License:Open Source License

/**
* returns an instance into a sparse liblinear array
*
* @param instance the instance to work on
* @return the liblinear array//from w  w w  .  java2  s.  c  om
* @throws Exception if setup of array fails
*/
protected FeatureNode[] instanceToArray(Instance instance) throws Exception {
    // determine number of non-zero attributes
    int count = 0;

    for (int i = 0; i < instance.numValues(); i++) {
        if (instance.index(i) == instance.classIndex())
            continue;
        if (instance.valueSparse(i) != 0)
            count++;
    }

    if (m_Bias >= 0) {
        count++;
    }

    // fill array
    FeatureNode[] nodes = new FeatureNode[count];
    int index = 0;
    for (int i = 0; i < instance.numValues(); i++) {

        int idx = instance.index(i);
        double val = instance.valueSparse(i);

        if (idx == instance.classIndex())
            continue;
        if (val == 0)
            continue;

        nodes[index] = new FeatureNode(idx + 1, val);
        index++;
    }

    // add bias term
    if (m_Bias >= 0) {
        nodes[index] = new FeatureNode(instance.numAttributes() + 1, m_Bias);
    }

    return nodes;
}

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 ww  .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 . j  a  v a2  s.  c o  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));/* ww w .j av  a  2 s  .c  om*/

    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);
    }

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

    return newInst;
}

From source file:org.hypknowsys.wumprep.WUMprepWrapper.java

License:Open Source License

/**
 * Creates a dummy dataset from the input format, sends it to the script and
 * reads the script output's ARFF information that in turn is used to set
 * <code>this</code>' output format.
 * //from   w  w  w  .  ja  va 2s .  c om
 * This mechanism allows a WUMprep script to alter the recordset layout as
 * long as this change is documented by the output ARFF header. For example,
 * the <tt>dnsLookup.pl</tt> script changes the <code>host_ip</code> field
 * to <code>host_dns</code> when performing IP lookups.
 * 
 * @param instanceInfo
 *          The input format.
 * @return Object containing the output instance structure.
 */
public Instances getScriptOutputFormat(Instances instanceInfo) {
    Instances outputFormat = instanceInfo;
    Instances testData = new Instances(instanceInfo);
    Instance testInstance = new Instance(testData.numAttributes());

    testData.delete();
    testInstance.setDataset(testData);

    // Initialize the testInstance's attribute values
    for (int i = 0; i < testInstance.numAttributes(); i++) {
        String aName = testInstance.attribute(i).name();
        if (aName.equals("host_ip"))
            testInstance.setValue(i, "127.0.0.1");
        else if (aName.equals("ts_day"))
            testInstance.setValue(i, "01");
        else if (aName.equals("ts_month"))
            testInstance.setValue(i, "Jan");
        else if (aName.equals("ts_year"))
            testInstance.setValue(i, "2005");
        else if (aName.equals("ts_hour"))
            testInstance.setValue(i, "11");
        else if (aName.equals("ts_minutes"))
            testInstance.setValue(i, "55");
        else if (aName.equals("ts_seconds"))
            testInstance.setValue(i, "00");
        else if (aName.equals("tz"))
            testInstance.setValue(i, "+0200");
        else
            testInstance.setValue(i, aName + "-dummy");
    }

    testData.add(testInstance);

    WUMprepWrapper testWrapper = new WUMprepWrapper(m_scriptName, m_args);
    testWrapper.start();
    testWrapper.push(testData.toString());
    testWrapper.push((Instance) null);

    class ErrorReader extends Thread implements Serializable {
        /**  */
        private static final long serialVersionUID = -488779846603045891L;
        PipedReader m_input = null;

        /**
         * Helper class for reading stderr output from the WUMprep script
         * 
         * @param input The script's wrapper's stderr pipe reader
         */
        ErrorReader(PipedReader input) {
            m_input = input;
            this.start();
        }

        public void run() {
            try {
                while (m_input.read() >= 0)
                    ;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // read the stderr output
    new ErrorReader(testWrapper.getErrorPipe());

    try {
        // ignore the stderr output
        outputFormat = new org.hypknowsys.wumprep4weka.core.Instances(testWrapper.getOutputPipe());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return outputFormat;
}