Example usage for weka.core Instance attribute

List of usage examples for weka.core Instance attribute

Introduction

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

Prototype

public Attribute attribute(int index);

Source Link

Document

Returns the attribute with the given index.

Usage

From source file:machinelearning.KnnClassifier.java

public double classifyInstance(Instance instance, int k) {
    int size = trainingData.numInstances();
    int attributes = trainingData.numAttributes() - 1;
    float dist;/* www.j a v a 2  s . c o  m*/
    Map<Float, Instance> neighbors = new TreeMap<>();

    Instance test;

    for (int i = 0; i < size; i++) {
        dist = 0;
        test = trainingData.instance(i);

        for (int j = 0; j < attributes; j++) {
            if (test.attribute(j).isNominal()) {
                if (instance.attribute(j).equals(test.attribute(j)))
                    dist++;
            } else
                dist += Math.abs(test.value(test.attribute(j)) - instance.value(test.attribute(j)));
        }

        neighbors.put(dist, test);
    }

    return findMostCommon(neighbors, k);
}

From source file:machinelearninglabs.OENaiveBayesClassifier.java

@Override
public double[] distributionForInstance(Instance instance) throws Exception {
    // return an array with a size of the number of classes
    double[] jointProbabilities = new double[instance.attribute(instance.classIndex()).numValues()];
    double[] result = new double[instance.attribute(instance.classIndex()).numValues()];

    // calculate un-normaalized probs
    for (int cls = 0; cls < jointProbabilities.length; cls++) {
        double p = classProbs[cls];
        for (int att = 0; att < instance.numAttributes() - 1; att++) {
            int value = (int) instance.value(att);
            p *= conditionalProbabilities[att][cls][value];
        }//from   ww w . j  ava 2s  .c  o m
        jointProbabilities[cls] = p;
    }

    // Find normalized probabilities
    for (int i = 0; i < jointProbabilities.length; i++) {
        double denominator = 0;
        for (int j = 0; j < jointProbabilities.length; j++) {
            denominator += jointProbabilities[j];
        }
        result[i] = jointProbabilities[i] / denominator;
    }
    return result;
}

From source file:machinelearningproject.DecisionTree.java

@Override
public double classifyInstance(Instance instance) throws Exception {
    String classification = mainTree.traverseTree(instance);
    double result = 0.0;
    for (int i = 0; i < instance.numClasses(); i++) {
        if (classification.equals(instance.attribute(instance.classIndex()).value(i))) {
            result = (double) i;
        }//from w  ww .j  a v  a 2 s  .c o  m
    }
    return result;
}

From source file:machinelearningproject.RandomForest.java

@Override
public double classifyInstance(Instance instance) throws Exception {
    HashMap<String, Integer> classMap = new HashMap<>();

    for (int i = 0; i < dtrees.size(); i++) {
        String key = dtrees.get(i).traverseTree(instance);
        if (classMap.isEmpty() || !classMap.containsKey(key)) {
            classMap.put(key, 1);//from   w  w  w  .ja va 2  s .  c  om
        } else {
            if (classMap.containsKey(key)) {
                classMap.put(key, classMap.get(key) + 1);
            }
        }
    }
    Iterator<String> keySetIterator = classMap.keySet().iterator();
    String modeClass = "";
    int count = 0;
    while (keySetIterator.hasNext()) {
        String key = keySetIterator.next();
        if (count < classMap.get(key)) {
            modeClass = key;
            count = classMap.get(key);
        }
    }

    double result = 0.0;
    for (int i = 0; i < instance.numClasses(); i++) {
        if (modeClass.equals(instance.attribute(instance.classIndex()).value(i))) {
            result = (double) i;
        }
    }

    return result;
}

From source file:machinelearningproject.Tree.java

public String traverseTree(Instance instance) {
    String attrValue = "";
    Tree buffTree = this;
    while (!buffTree.isLeaf()) {
        //get attribute value of an instance
        for (int i = 0; i < instance.numAttributes(); i++) {
            if (instance.attribute(i).name().equals(buffTree.attributeName)) {
                attrValue = instance.stringValue(i);
                break;
            }//from www .j a  v a  2  s .c o  m
        }

        //compare attribute with node value
        for (int i = 0; i < buffTree.nodes.size(); i++) {
            if (attrValue.equals(buffTree.nodes.get(i).value)) {
                buffTree = buffTree.nodes.get(i).subTree;
                break;
            }
        }
    }

    //isLeaf
    attrValue = buffTree.attributeName;

    return attrValue;
}

From source file:meka.experiment.statisticsexporters.WekaFilter.java

License:Open Source License

/**
 * Converts the Instances back into statistics.
 *
 * @param data          the data to convert
 * @return              the generated statistics
 *//*from   w ww .j  a  v a  2s  .c o  m*/
protected List<EvaluationStatistics> fromInstances(Instances data) {
    List<EvaluationStatistics> result;
    EvaluationStatistics stat;
    MultiLabelClassifier cls;
    String rel;
    int i;
    int n;
    Instance inst;

    result = new ArrayList<>();

    if (data.attribute(EvaluationStatistics.KEY_CLASSIFIER) == null) {
        log("Failed to locate attribute: " + EvaluationStatistics.KEY_CLASSIFIER);
        return result;
    }
    if (data.attribute(EvaluationStatistics.KEY_RELATION) == null) {
        log("Failed to locate attribute: " + EvaluationStatistics.KEY_RELATION);
        return result;
    }

    for (i = 0; i < data.numInstances(); i++) {
        inst = data.instance(i);
        try {
            cls = OptionUtils.fromCommandLine(MultiLabelClassifier.class,
                    inst.stringValue(data.attribute(EvaluationStatistics.KEY_CLASSIFIER)));
            rel = inst.stringValue(data.attribute(EvaluationStatistics.KEY_RELATION));
            stat = new EvaluationStatistics(cls, rel, null);
            for (n = 0; n < inst.numAttributes(); n++) {
                if (inst.attribute(n).isNumeric() && !inst.isMissing(n)) {
                    stat.put(inst.attribute(n).name(), inst.value(n));
                }
            }
            result.add(stat);
        } catch (Exception e) {
            handleException("Failed to process instance: " + inst, e);
        }
    }

    return result;
}

From source file:meka.gui.dataviewer.DataTableModel.java

License:Open Source License

/**
 * sets the value in the cell at columnIndex and rowIndex to aValue. but only
 * the value and the value can be changed
 *
 * @param aValue the new value//from  w  w w.  j a v  a  2  s .  c  o m
 * @param rowIndex the row index
 * @param columnIndex the column index
 * @param notify whether to notify the listeners
 */
public void setValueAt(Object aValue, int rowIndex, int columnIndex, boolean notify) {
    int type;
    int index;
    String tmp;
    Instance inst;
    Attribute att;
    Object oldValue;

    if (!m_IgnoreChanges) {
        addUndoPoint();
    }

    oldValue = getValueAt(rowIndex, columnIndex);
    type = getType(rowIndex, columnIndex);
    index = columnIndex - 1;
    inst = m_Data.instance(rowIndex);
    att = inst.attribute(index);

    // missing?
    if (aValue == null) {
        inst.setValue(index, Utils.missingValue());
    } else {
        tmp = aValue.toString();

        switch (type) {
        case Attribute.DATE:
            try {
                att.parseDate(tmp);
                inst.setValue(index, att.parseDate(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.NOMINAL:
            if (att.indexOfValue(tmp) > -1) {
                inst.setValue(index, att.indexOfValue(tmp));
            }
            break;

        case Attribute.STRING:
            inst.setValue(index, tmp);
            break;

        case Attribute.NUMERIC:
            try {
                Double.parseDouble(tmp);
                inst.setValue(index, Double.parseDouble(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.RELATIONAL:
            try {
                inst.setValue(index, inst.attribute(index).addRelation((Instances) aValue));
            } catch (Exception e) {
                // ignore
            }
            break;

        default:
            throw new IllegalArgumentException("Unsupported Attribute type: " + type + "!");
        }
    }

    // notify only if the value has changed!
    if (notify && (!("" + oldValue).equals("" + aValue))) {
        notifyListener(new TableModelEvent(this, rowIndex, columnIndex));
    }
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Calculates the distance between two instances
 *
 * @param first the first instance/*w  w w .  jav  a2s .c om*/
 * @param second the second instance
 * @return the distance between the two given instances
 */
private double distance(Instance first, double[] mean, double[] var, int pos) {

    double diff, distance = 0;
    int j = 0;
    for (int i = 0; i < first.numAttributes(); i++) {
        // Skipp nominal attributes (incl. class & ID)
        if ((i == m_ClassIndex) || (i == m_IdIndex))
            continue;

        // If attribute is numeric
        if (first.attribute(i).isNumeric()) {
            if (!first.isMissing(i)) {
                diff = first.value(i) - mean[j];
                if (Utils.gr(var[j], m_ZERO))
                    distance += m_Change[pos][j] * var[j] * diff * diff;
                else
                    distance += m_Change[pos][j] * diff * diff;
            } else {
                if (Utils.gr(var[j], m_ZERO))
                    distance += m_Change[pos][j] * var[j];
                else
                    distance += m_Change[pos][j] * 1.0;
            }
        }
        j++;
    }

    return distance;
}

From source file:ml.dataprocess.CorrelationAttributeEval.java

License:Open Source License

/**
 * Initializes an information gain attribute evaluator. Replaces missing
 * values with means/modes; Deletes instances with missing class values.
 * //from  w  w w .  jav  a2  s. co m
 * @param data set of instances serving as training data
 * @throws Exception if the evaluator has not been generated successfully
 */
@Override
public void buildEvaluator(Instances data) throws Exception {
    data = new Instances(data);
    data.deleteWithMissingClass();

    ReplaceMissingValues rmv = new ReplaceMissingValues();
    rmv.setInputFormat(data);
    data = Filter.useFilter(data, rmv);

    int numClasses = data.classAttribute().numValues();
    int classIndex = data.classIndex();
    int numInstances = data.numInstances();
    m_correlations = new double[data.numAttributes()];
    /*
     * boolean hasNominals = false; boolean hasNumerics = false;
     */
    List<Integer> numericIndexes = new ArrayList<Integer>();
    List<Integer> nominalIndexes = new ArrayList<Integer>();
    if (m_detailedOutput) {
        m_detailedOutputBuff = new StringBuffer();
    }

    // TODO for instance weights (folded into computing weighted correlations)
    // add another dimension just before the last [2] (0 for 0/1 binary vector
    // and
    // 1 for corresponding instance weights for the 1's)
    double[][][] nomAtts = new double[data.numAttributes()][][];
    for (int i = 0; i < data.numAttributes(); i++) {
        if (data.attribute(i).isNominal() && i != classIndex) {
            nomAtts[i] = new double[data.attribute(i).numValues()][data.numInstances()];
            Arrays.fill(nomAtts[i][0], 1.0); // set zero index for this att to all
                                             // 1's
            nominalIndexes.add(i);
        } else if (data.attribute(i).isNumeric() && i != classIndex) {
            numericIndexes.add(i);
        }
    }

    // do the nominal attributes
    if (nominalIndexes.size() > 0) {
        for (int i = 0; i < data.numInstances(); i++) {
            Instance current = data.instance(i);
            for (int j = 0; j < current.numValues(); j++) {
                if (current.attribute(current.index(j)).isNominal() && current.index(j) != classIndex) {
                    // Will need to check for zero in case this isn't a sparse
                    // instance (unless we add 1 and subtract 1)
                    nomAtts[current.index(j)][(int) current.valueSparse(j)][i] += 1;
                    nomAtts[current.index(j)][0][i] -= 1;
                }
            }
        }
    }

    if (data.classAttribute().isNumeric()) {
        double[] classVals = data.attributeToDoubleArray(classIndex);

        // do the numeric attributes
        for (Integer i : numericIndexes) {
            double[] numAttVals = data.attributeToDoubleArray(i);
            m_correlations[i] = Utils.correlation(numAttVals, classVals, numAttVals.length);

            if (m_correlations[i] == 1.0) {
                // check for zero variance (useless numeric attribute)
                if (Utils.variance(numAttVals) == 0) {
                    m_correlations[i] = 0;
                }
            }
        }

        // do the nominal attributes
        if (nominalIndexes.size() > 0) {

            // now compute the correlations for the binarized nominal attributes
            for (Integer i : nominalIndexes) {
                double sum = 0;
                double corr = 0;
                double sumCorr = 0;
                double sumForValue = 0;

                if (m_detailedOutput) {
                    m_detailedOutputBuff.append("\n\n").append(data.attribute(i).name());
                }

                for (int j = 0; j < data.attribute(i).numValues(); j++) {
                    sumForValue = Utils.sum(nomAtts[i][j]);
                    corr = Utils.correlation(nomAtts[i][j], classVals, classVals.length);

                    // useless attribute - all instances have the same value
                    if (sumForValue == numInstances || sumForValue == 0) {
                        corr = 0;
                    }
                    if (corr < 0.0) {
                        corr = -corr;
                    }
                    sumCorr += sumForValue * corr;
                    sum += sumForValue;

                    if (m_detailedOutput) {
                        m_detailedOutputBuff.append("\n\t").append(data.attribute(i).value(j)).append(": ");
                        m_detailedOutputBuff.append(Utils.doubleToString(corr, 6));
                    }
                }
                m_correlations[i] = (sum > 0) ? sumCorr / sum : 0;
            }
        }
    } else {
        // class is nominal
        // TODO extra dimension for storing instance weights too
        double[][] binarizedClasses = new double[data.classAttribute().numValues()][data.numInstances()];

        // this is equal to the number of instances for all inst weights = 1
        double[] classValCounts = new double[data.classAttribute().numValues()];

        for (int i = 0; i < data.numInstances(); i++) {
            Instance current = data.instance(i);
            binarizedClasses[(int) current.classValue()][i] = 1;
        }
        for (int i = 0; i < data.classAttribute().numValues(); i++) {
            classValCounts[i] = Utils.sum(binarizedClasses[i]);
        }

        double sumClass = Utils.sum(classValCounts);

        // do numeric attributes first
        if (numericIndexes.size() > 0) {
            for (Integer i : numericIndexes) {
                double[] numAttVals = data.attributeToDoubleArray(i);
                double corr = 0;
                double sumCorr = 0;

                for (int j = 0; j < data.classAttribute().numValues(); j++) {
                    corr = Utils.correlation(numAttVals, binarizedClasses[j], numAttVals.length);
                    if (corr < 0.0) {
                        corr = -corr;
                    }

                    if (corr == 1.0) {
                        // check for zero variance (useless numeric attribute)
                        if (Utils.variance(numAttVals) == 0) {
                            corr = 0;
                        }
                    }

                    sumCorr += classValCounts[j] * corr;
                }
                m_correlations[i] = sumCorr / sumClass;
            }
        }

        if (nominalIndexes.size() > 0) {
            for (Integer i : nominalIndexes) {
                if (m_detailedOutput) {
                    m_detailedOutputBuff.append("\n\n").append(data.attribute(i).name());
                }

                double sumForAtt = 0;
                double corrForAtt = 0;
                for (int j = 0; j < data.attribute(i).numValues(); j++) {
                    double sumForValue = Utils.sum(nomAtts[i][j]);
                    double corr = 0;
                    double sumCorr = 0;
                    double avgCorrForValue = 0;

                    sumForAtt += sumForValue;
                    for (int k = 0; k < numClasses; k++) {

                        // corr between value j and class k
                        corr = Utils.correlation(nomAtts[i][j], binarizedClasses[k],
                                binarizedClasses[k].length);

                        // useless attribute - all instances have the same value
                        if (sumForValue == numInstances || sumForValue == 0) {
                            corr = 0;
                        }
                        if (corr < 0.0) {
                            corr = -corr;
                        }
                        sumCorr += classValCounts[k] * corr;
                    }
                    avgCorrForValue = sumCorr / sumClass;
                    corrForAtt += sumForValue * avgCorrForValue;

                    if (m_detailedOutput) {
                        m_detailedOutputBuff.append("\n\t").append(data.attribute(i).value(j)).append(": ");
                        m_detailedOutputBuff.append(Utils.doubleToString(avgCorrForValue, 6));
                    }
                }

                // the weighted average corr for att i as
                // a whole (wighted by value frequencies)
                m_correlations[i] = (sumForAtt > 0) ? corrForAtt / sumForAtt : 0;
            }
        }
    }

    if (m_detailedOutputBuff != null && m_detailedOutputBuff.length() > 0) {
        m_detailedOutputBuff.append("\n");
    }
}

From source file:moa.classifiers.bayes.NaiveBayes.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
    for (int i = 0; i < inst.numAttributes() - 1; i++) {
        int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);
        AttributeClassObserver obs = this.attributeObservers.get(i);
        if (obs == null) {
            obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                    : newNumericClassObserver();
            this.attributeObservers.set(i, obs);
        }//from  w  w w . ja  v  a 2 s . co m
        obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight());
    }
}