Example usage for weka.core Instance enumerateAttributes

List of usage examples for weka.core Instance enumerateAttributes

Introduction

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

Prototype

public Enumeration<Attribute> enumerateAttributes();

Source Link

Document

Returns an enumeration of all the attributes.

Usage

From source file:cn.edu.xjtu.dbmine.source.NaiveBayes.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test 
 * instance.//from w  w w.j a v a2s .  com
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 * @exception Exception if there is a problem generating the prediction
 */
public double[] distributionForInstance(Instance instance) throws Exception {

    if (m_UseDiscretization) {
        m_Disc.input(instance);
        instance = m_Disc.output();
    }
    double[] probs = new double[m_NumClasses];
    for (int j = 0; j < m_NumClasses; j++) {
        probs[j] = m_ClassDistribution.getProbability(j);
    }
    Enumeration enumAtts = instance.enumerateAttributes();
    int attIndex = 0;
    while (enumAtts.hasMoreElements()) {
        Attribute attribute = (Attribute) enumAtts.nextElement();
        if (!instance.isMissing(attribute)) {
            double temp, max = 0;
            for (int j = 0; j < m_NumClasses; j++) {
                temp = Math.max(1e-75,
                        Math.pow(m_Distributions[attIndex][j].getProbability(instance.value(attribute)),
                                m_Instances.attribute(attIndex).weight()));
                probs[j] *= temp;
                if (probs[j] > max) {
                    max = probs[j];
                }
                if (Double.isNaN(probs[j])) {
                    throw new Exception("NaN returned from estimator for attribute " + attribute.name() + ":\n"
                            + m_Distributions[attIndex][j].toString());
                }
            }
            if ((max > 0) && (max < 1e-75)) { // Danger of probability underflow
                for (int j = 0; j < m_NumClasses; j++) {
                    probs[j] *= 1e75;
                }
            }
        }
        attIndex++;
    }

    // Display probabilities
    Utils.normalize(probs);
    return probs;
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static Map<Attribute, Double> instanceToMap(Instance ins) {
    HashMap<Attribute, Double> retval = new HashMap<Attribute, Double>();
    Enumeration<Attribute> enu = ins.enumerateAttributes();
    while (enu.hasMoreElements()) {
        Attribute temp = enu.nextElement();
        retval.put(temp, ins.value(temp));
    }/* ww w . j a v a  2 s .co m*/
    return retval;
}

From source file:faster_pca.faster_pca.java

License:Open Source License

/**
 * Modified version of PrincipalComponents.fillCorrelation()
 * @throws Exception //from   w w  w . ja  va 2s . c o m
 */
protected void fillCorrelation() throws Exception {
    int i;
    int j;
    int k;
    double[] att1;
    double[] att2;
    double corr;

    m_Correlation = new double[m_NumAttribs][m_NumAttribs];
    att1 = new double[m_NumInstances];
    att2 = new double[m_NumInstances];

    double trainInstancesCopy[][] = new double[m_NumAttribs][m_NumInstances];

    for (i = 0; i < m_NumInstances; i++) {
        Instance in = m_TrainInstances.instance(i);
        Enumeration enumer = in.enumerateAttributes();

        for (j = 0; j < m_NumAttribs; j++) {
            trainInstancesCopy[j][i] = in.value(j);
        }
    }

    for (i = 0; i < m_NumAttribs; i++) {
        for (j = 0; j <= i; j++) {
            /*for (k = 0; k < m_NumInstances; k++) {
              //att1[k] = m_TrainInstances.instance(k).value(i);
              att1[k] = trainInstancesCopy[i][k];
              //att2[k] = m_TrainInstances.instance(k).value(j);
              att2[k] = trainInstancesCopy[j][k];
            }*/
            if (i == j) {
                m_Correlation[i][j] = 1.0;
            } else {
                corr = Utils.correlation(trainInstancesCopy[i], trainInstancesCopy[j], m_NumInstances);
                m_Correlation[i][j] = corr;
                m_Correlation[j][i] = corr;
            }
        }
    }

    // now standardize the input data
    /*m_standardizeFilter = new Standardize();
    m_standardizeFilter.setInputFormat(m_TrainInstances);
    m_TrainInstances = Filter.useFilter(m_TrainInstances, m_standardizeFilter);*/ //todo: see if this line actually needs called
    double mins[] = new double[m_NumAttribs];
    double maxs[] = new double[m_NumAttribs];

    for (j = 0; j < m_NumAttribs; j++) {
        mins[j] = Double.MAX_VALUE;
        maxs[j] = Double.MIN_VALUE;
        for (i = 0; i < m_NumInstances; i++) {
            double val = trainInstancesCopy[j][i];
            if (val < mins[j])
                mins[j] = val;
            if (val > maxs[j])
                maxs[j] = val;
        }

    }

    f_norm = new fast_normalize(mins, maxs);

}

From source file:main.NaiveBayes.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test instance.
 * /*from  w  w w. j  a va 2s  .co  m*/
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 * @exception Exception if there is a problem generating the prediction
 */
@Override
public double[] distributionForInstance(Instance instance) throws Exception {

    if (m_UseDiscretization) {
        m_Disc.input(instance);
        instance = m_Disc.output();
    }
    double[] probs = new double[m_NumClasses];
    for (int j = 0; j < m_NumClasses; j++) {
        probs[j] = m_ClassDistribution.getProbability(j);
    }
    Enumeration<Attribute> enumAtts = instance.enumerateAttributes();
    int attIndex = 0;
    while (enumAtts.hasMoreElements()) {
        Attribute attribute = enumAtts.nextElement();
        if (!instance.isMissing(attribute)) {
            double temp, max = 0;
            for (int j = 0; j < m_NumClasses; j++) {
                temp = Math.max(1e-75,
                        Math.pow(m_Distributions[attIndex][j].getProbability(instance.value(attribute)),
                                m_Instances.attribute(attIndex).weight()));
                probs[j] *= temp;
                if (probs[j] > max) {
                    max = probs[j];
                }
                if (Double.isNaN(probs[j])) {
                    throw new Exception("NaN returned from estimator for attribute " + attribute.name() + ":\n"
                            + m_Distributions[attIndex][j].toString());
                }
            }
            if ((max > 0) && (max < 1e-75)) { // Danger of probability underflow
                for (int j = 0; j < m_NumClasses; j++) {
                    probs[j] *= 1e75;
                }
            }
        }
        attIndex++;
    }

    // Display probabilities
    Utils.normalize(probs);
    return probs;
}

From source file:NaiveBayes.NaiveBayes13514004.java

@Override
public double[] distributionForInstance(Instance instnc) throws Exception {
    Attribute ins;//  w  ww.j a v a 2 s .  c o m
    double[] result = new double[numClass];

    //Algoritma
    //Menghitung probabilitas
    for (int j = 0; j < numClass; j++) {
        result[j] = kelasprob[j];
        Enumeration<Attribute> e2 = instnc.enumerateAttributes();
        int i = 0;
        while (e2.hasMoreElements()) {
            ins = e2.nextElement();
            result[j] = result[j] * prob[i][j][(int) instnc.value(ins)];
            i++;
        }
    }

    return result;
}

From source file:naivebayes.NBTubesAI.java

@Override
public double classifyInstance(Instance instance) throws Exception {
    int jumlahKelas = instance.classAttribute().numValues();
    double[] classifyResult = new double[jumlahKelas];

    //iterasi menghitung probabilitas untuk seluruh kelas
    for (int i = 0; i < jumlahKelas; i++) {

        //Rumus probabilitas Naive Bayes here

        classifyResult[i] = (double) classCount.get(i + 0.0) / numInstance;

        Enumeration<Attribute> enumAttr = instance.enumerateAttributes();

        while (enumAttr.hasMoreElements()) {
            Attribute temp = enumAttr.nextElement();

            if (!instance.isMissing(temp)) {

                try {
                    classifyResult[i] = classifyResult[i]
                            * distribution.get(temp.name()).get(instance.stringValue(temp)).get(i + 0.0);

                } catch (NullPointerException e) {
                    classifyResult[i] = 0;
                }/*from   w ww  .ja  va2s  .co m*/

            }

        }

    }
    double maxValue = 0;
    int currentIndex = 0;
    for (int i = 0; i < jumlahKelas; i++) {
        if (maxValue < classifyResult[i]) {
            currentIndex = i;
            maxValue = classifyResult[i];
        }
    }
    return currentIndex;

}

From source file:naivebayes.NBTubesAI.java

@Override
public double[] distributionForInstance(Instance instance) throws Exception {
    int jumlahKelas = instance.classAttribute().numValues();

    double[] classifyResult = new double[jumlahKelas];

    //iterasi menghitung probabilitas untuk seluruh kelas
    for (int i = 0; i < jumlahKelas; i++) {

        //Rumus probabilitas Naive Bayes here

        classifyResult[i] = (double) classCount.get(i + 0.0) / numInstance;

        Enumeration<Attribute> enumAttr = instance.enumerateAttributes();

        while (enumAttr.hasMoreElements()) {
            Attribute temp = enumAttr.nextElement();

            if (!instance.isMissing(temp)) {

                try {
                    classifyResult[i] = classifyResult[i]
                            * distribution.get(temp.name()).get(instance.stringValue(temp)).get(i + 0.0);

                } catch (NullPointerException e) {

                }//from   w  w  w .  j a  v  a2 s .c  o  m

            }

        }

    }

    return classifyResult;
}

From source file:org.opentox.toxotis.factory.DatasetFactory.java

License:Open Source License

/**
 * Create a {@link DataEntry data entry} from a single instance.
 * @param instance/*www  .jav a 2  s . c  o  m*/
 * @return
 *      A Data Entry that corresponds to the provided instance.
 * @throws ToxOtisException
 */
public DataEntry createDataEntry(Instance instance) throws ToxOtisException {
    Enumeration attributes = instance.enumerateAttributes();
    DataEntry de = new DataEntry();
    try {
        while (attributes.hasMoreElements()) {
            Attribute attribute = (Attribute) attributes.nextElement();
            if (attribute.name().equals(Dataset.COMPOUND_URI) || attribute.name().equals("URI")) {
                de.setConformer(new Compound(new VRI(instance.stringValue(attribute))));
            } else {
                FeatureValue fv = new FeatureValue();
                Feature feature = new Feature(new VRI(attribute.name()));

                LiteralValue value = null;
                if (attribute.isNumeric()) {
                    value = new LiteralValue<Double>(instance.value(attribute), XSDDatatype.XSDdouble);
                    feature.getOntologicalClasses().add(OTClasses.numericFeature());
                } else if (attribute.isString() || attribute.isDate()) {
                    value = new LiteralValue<String>(instance.stringValue(attribute), XSDDatatype.XSDstring);
                    feature.getOntologicalClasses().add(OTClasses.stringFeature());
                } else if (attribute.isNominal()) {
                    value = new LiteralValue<String>(instance.stringValue(attribute), XSDDatatype.XSDstring);
                    Enumeration nominalValues = attribute.enumerateValues();
                    feature.getOntologicalClasses().add(OTClasses.nominalFeature());
                    while (nominalValues.hasMoreElements()) {
                        String nomValue = (String) nominalValues.nextElement();
                        feature.getAdmissibleValues()
                                .add(new LiteralValue<String>(nomValue, XSDDatatype.XSDstring));
                    }
                }
                fv.setFeature(feature);
                fv.setValue(value);
                de.addFeatureValue(fv);
            }
        }
    } catch (URISyntaxException ex) {
        throw new ToxOtisException(ex);
    }
    return de;
}

From source file:sirius.nnsearcher.main.Constraints.java

License:Open Source License

@SuppressWarnings("unchecked")
private void findIndex(weka.core.Instance instance) {
    for (Enumeration<Attribute> e = instance.enumerateAttributes(); e.hasMoreElements();) {
        Attribute attr = e.nextElement();
        if (attr.name().compareTo(this.attributeName.name()) == 0) {
            this.index = attr.index();
            break;
        }/*www .  j  a  va2s.c o  m*/
    }
}