Example usage for weka.core Instance classIndex

List of usage examples for weka.core Instance classIndex

Introduction

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

Prototype

public int classIndex();

Source Link

Document

Returns the class attribute's index.

Usage

From source file:moa.classifiers.multilabel.MultilabelHoeffdingTree.java

License:Open Source License

@Override
public double[] getVotesForInstance(Instance inst) {

    int L = inst.classIndex() + 1;
    if (m_L != L) {
        // Update class labels
        m_L = L;/*  ww w  . j  a va2 s.c o  m*/
        // Create a converter, and its template
        converter = new Converter(m_L);
        try {
            converter.createTemplate(new Instances(new StringReader(this.modelContext.toString()), 0));
        } catch (Exception e) {
            System.err.println("Error, failed to create a multi-label Instances template with L = " + m_L);
            System.out.println("Instances: " + this.modelContext.toString());
            e.printStackTrace();
            System.exit(1);
        }
    }

    if (this.treeRoot != null) {
        FoundNode foundNode = this.treeRoot.filterInstanceToLeaf(inst, null, -1);
        Node leafNode = foundNode.node;
        if (leafNode == null) {
            leafNode = foundNode.parent;
        }
        //System.out.println("y[] = "+Arrays.toString(leafNode.getClassVotes(inst,this)));
        return leafNode.getClassVotes(inst, this);
    }
    // Return empty array (this should only happen once! -- before we build the root node).
    return new double[this.m_L];
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 *//* w ww  .  j  av a  2  s . c om*/
@Override
public void trainOnInstanceImpl(Instance inst) {
    if (this.reset == true) {
        this.m_numClasses = inst.numClasses();
        double laplace = this.laplaceCorrectionOption.getValue();
        int numAttributes = inst.numAttributes();

        m_probOfClass = new double[m_numClasses];
        Arrays.fill(m_probOfClass, laplace);

        m_classTotals = new double[m_numClasses];
        Arrays.fill(m_classTotals, laplace * numAttributes);

        m_wordTotalForClass = new double[numAttributes][m_numClasses];
        for (double[] wordTotal : m_wordTotalForClass) {
            Arrays.fill(wordTotal, laplace);
        }
        this.reset = false;
    }
    // Update classifier
    int classIndex = inst.classIndex();
    int classValue = (int) inst.value(classIndex);

    double w = inst.weight();
    m_probOfClass[classValue] += w;

    m_classTotals[classValue] += w * totalSize(inst);
    double total = m_classTotals[classValue];

    for (int i = 0; i < inst.numValues(); i++) {
        int index = inst.index(i);
        if (index != classIndex && !inst.isMissing(i)) {
            m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i);
        }
    }
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//ww  w.j  a  va  2 s. c  o  m
 *
 * @param instance    the instance to be classified
 * @return       predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance instance) {
    if (this.reset == true) {
        return new double[2];
    }
    double[] probOfClassGivenDoc = new double[m_numClasses];
    double totalSize = totalSize(instance);

    for (int i = 0; i < m_numClasses; i++) {
        probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]);
    }

    for (int i = 0; i < instance.numValues(); i++) {

        int index = instance.index(i);
        if (index == instance.classIndex() || instance.isMissing(i)) {
            continue;
        }

        double wordCount = instance.valueSparse(i);
        for (int c = 0; c < m_numClasses; c++) {
            probOfClassGivenDoc[c] += wordCount * Math.log(m_wordTotalForClass[index][c]);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}

From source file:moa.classifiers.rules.AbstractAMRules.java

License:Apache License

/**
 * Gets the index of the attribute in the instance,
 * given the index of the attribute in the learner.
 *
 * @param index the index of the attribute in the learner
 * @param inst the instance//from w  w  w.  ja  va 2 s  .c o m
 * @return the index in the instance
 */
public static int modelAttIndexToInstanceAttIndex(int index, Instance inst) {
    return index <= inst.classIndex() ? index : index + 1;
}

From source file:moa.classifiers.rules.core.conditionaltests.NumericAttributeBinaryRulePredicate.java

License:Apache License

@Override
public int branchForInstance(Instance inst) {
    int instAttIndex = this.attIndex < inst.classIndex() ? this.attIndex : this.attIndex + 1;
    if (inst.isMissing(instAttIndex)) {
        return -1;
    }// w  w w  . j  a  v  a2s  .  c om
    double v = inst.value(instAttIndex);
    int ret = 0;
    switch (this.operator) {
    case 0:
        ret = (v == this.attValue) ? 0 : 1;
        break;
    case 1:
        ret = (v <= this.attValue) ? 0 : 1;
        break;
    case 2:
        ret = (v > this.attValue) ? 0 : 1;
    }
    return ret;
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * /*from w w w  .j  av  a2 s . co m*/
 * @param x data instance in question for comparison to this distribution.
 * @return probability that point x is a member of this cluster
 */
@Override
public final double getInclusionProbability(Instance x) {
    int labelIndex = x.classIndex();
    double[] values = x.toDoubleArray();
    return getInclusionProbability(values, labelIndex);
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Use the multi-variate Gaussian probability estimation approach to determine probability x is a member of this cluster.
 * This is not the same as a true Mahalanobis distance derived from a full covariance matrix as we take the heuristic shortcut
 * of assuming variable independence (rho = 0), and thus have an axis-parallel hyper-ellipse (i.e. diagonal covariance matrix)
 * under the same 'naive' assumption as in Naive Bayes. The computational speed-up here is worth the reduced accuracy.
 * @param x instance in question/*from  ww  w  .ja v a2s .c o m*/
 * @return probability that point x is a member of this cluster
 * @deprecated 
 */
@Deprecated
public double getInclusionMVGProbability(Instance x) {

    double[] diagonalCovariance = this.getVariances();
    int degreesOfFreedom = 0;
    double axisParallelSquaredMahalanobusDistance = 0;
    // Compute Mahalanobis (axis-parallel estimation) distance and tally degrees of freedom
    for (int i = 0; i < centroid.length; ++i) {
        if (i == x.classIndex()) {
            continue;
        }
        double attributeDelta = (this.symbolFrequencies[i] == null) ? x.value(i) - centroid[i]
                : (Math.abs(x.value(i) - centroid[i]) < 1e-32) ? 0 : 1;
        if ((diagonalCovariance[i] != 0) && Double.isFinite(diagonalCovariance[i])) {
            axisParallelSquaredMahalanobusDistance += attributeDelta * attributeDelta / diagonalCovariance[i];
        }
        degreesOfFreedom++;
    }

    // Zero distance means exact match, so stop working on more computations...
    if ((axisParallelSquaredMahalanobusDistance == 0) && (degreesOfFreedom > 0)) {
        return 1.0;
    }

    // Compute the determinant of the Covariance matrix (which is O(N) if we only have the diagonal of the matrix)
    double covarianceDet = 1;
    for (int i = 0; i < diagonalCovariance.length; ++i) {
        if (i == x.classIndex()) {
            continue;
        }
        if ((diagonalCovariance[i] != 0) && Double.isFinite(diagonalCovariance[i])) {
            covarianceDet *= diagonalCovariance[i];
        } else {
            covarianceDet *= weka.core.Utils.SMALL;
        }
    }
    if (covarianceDet == 0) {
        covarianceDet = weka.core.Utils.SMALL / 1000.0;
    } // Safety
    double score = Math.exp(0.0 - 0.5 * axisParallelSquaredMahalanobusDistance)
            / (Math.pow(2.0 * Math.PI, degreesOfFreedom / 2.0) * Math.sqrt(Math.abs(covarianceDet)));
    return (1.0 - weka.core.FastStats.chiSquaredProbability(score, degreesOfFreedom));
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Sanity check and initialization of dynamic fields
 *
 * @param x/*from   ww  w  .j a v  a  2  s.  c om*/
 */
protected final void safeInit(Instance x) {
    if (this.embeddedLearnerOption.getValueAsCLIString().contains("Majority class")) {
        this.excludeOutlierVoting = true;
    }
    if (centroid == null) {
        centroid = x.toDoubleArray();
    }
    if (this.instances == null) {
        prepareEmbeddedClassifier();
        ArrayList<Attribute> attribs = new ArrayList<>();
        this.symbolFrequencies = new double[x.dataset().numAttributes()][];
        for (int i = 0; i < x.dataset().numAttributes(); ++i) {
            Attribute a = (Attribute) x.dataset().attribute(i).copy();
            if (i == x.classIndex()) {
                a.setWeight(0.0);
            } else {
                a.setWeight(1.0);
            }
            switch (a.type()) {
            case Attribute.STRING:
            case Attribute.NOMINAL:
                //UnsafeUtils.setAttributeRange(a, x.value(i), x.value(i));
                this.symbolFrequencies[i] = new double[a.numValues()];
                break;
            case Attribute.NUMERIC:
            case Attribute.RELATIONAL:
            case Attribute.DATE:
            default:
                // UnsafeUtils.setAttributeRange(a, x.value(i), x.value(i));
                this.symbolFrequencies[i] = null;
            }
            attribs.add(a);
        }
        this.instances = new Instances("ClusterData", attribs, 1);
        this.instances.setClassIndex(x.classIndex());

    }
    //        else {
    //            for (int i = 0; i < x.dataset().numAttributes() && i < this.header.numAttributes(); ++i) {
    //                double val = x.value(i);
    //                Attribute a = this.header.attribute(i);
    //                // expand range as necessary
    //                if (val < a.getLowerNumericBound() || val > a.getUpperNumericBound()){
    //                    UnsafeUtils.setAttributeRange(a, Math.min(val,a.getLowerNumericBound()), Math.max(val,a.getUpperNumericBound()));
    //                }
    //                // increase frequency counts if new string value is encountered
    //                if (a.type() == Attribute.STRING && (val >= Math.max(this.symbolFrequencies[i].length, a.numValues()))) {
    //                    double newArray[] = new double[Math.max(this.symbolFrequencies[i].length, a.numValues())];
    //                    Arrays.fill(newArray, 0);
    //                    for(int j = 0; j <= this.symbolFrequencies[i].length; j++) {
    //                        newArray[j] = this.symbolFrequencies[i][j];
    //                    }
    //                    this.symbolFrequencies[i] = newArray;
    //                }
    //            }
    //        }
    if (this.variances == null) {
        this.variances = new double[x.numAttributes()];
        Arrays.fill(this.variances, 1);
    }
    if (this.entropies == null) {
        this.entropies = new double[x.numAttributes()];
        Arrays.fill(this.entropies, 0);
    }
    if (this.labelFrequencies == null) {
        this.labelFrequencies = new double[x.numClasses()];
        Arrays.fill(this.labelFrequencies, 0);
    }
    if (this.gtLabelFrequencies == null) {
        this.gtLabelFrequencies = new double[x.numClasses()];
        Arrays.fill(this.gtLabelFrequencies, 0);
    }
    if (this.rho == null) {
        this.rho = new double[x.numAttributes()];
        Arrays.fill(this.rho, 0);
    }
}

From source file:moa.clusterers.AbstractClusterer.java

License:Open Source License

protected static int modelAttIndexToInstanceAttIndex(int index, Instance inst) {
    return inst.classIndex() > index ? index : index + 1;
}

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

License:Apache License

protected boolean readNextInstanceFromFile() {
    try {/*from  w w  w  .jav a2 s.  c  o 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);
    }
}