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:moa.classifiers.functions.SPegasos.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance the new training instance to include in the model
 *//*from www . j a v  a2  s.  c om*/
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new double[instance.numAttributes() + 1];
    }
    if (!instance.classIsMissing()) {

        double learningRate = 1.0 / (m_lambda * m_t);
        //double scale = 1.0 - learningRate * m_lambda;
        double scale = 1.0 - 1.0 / m_t;
        double y = (instance.classValue() == 0) ? -1 : 1;
        double wx = dotProd(instance, m_weights, instance.classIndex());
        double z = y * (wx + m_weights[m_weights.length - 1]);

        for (int j = 0; j < m_weights.length - 1; j++) {
            if (j != instance.classIndex()) {
                m_weights[j] *= scale;
            }
        }

        if (m_loss == LOGLOSS || (z < 1)) {
            double loss = dloss(z);
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    double m = learningRate * loss * (instance.valueSparse(p1) * y);
                    m_weights[indS] += m;
                }
            }

            // update the bias
            m_weights[m_weights.length - 1] += learningRate * loss * y;
        }

        double norm = 0;
        for (int k = 0; k < m_weights.length - 1; k++) {
            if (k != instance.classIndex()) {
                norm += (m_weights[k] * m_weights[k]);
            }
        }

        double scale2 = Math.min(1.0, (1.0 / (m_lambda * norm)));
        if (scale2 < 1.0) {
            scale2 = Math.sqrt(scale2);
            for (int j = 0; j < m_weights.length - 1; j++) {
                if (j != instance.classIndex()) {
                    m_weights[j] *= scale2;
                }
            }
        }
        m_t++;
    }
}

From source file:moa.classifiers.functions.SPegasos.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//from   w w w .  j  ava2 s .  co m
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance inst) {

    if (m_weights == null) {
        return new double[inst.numAttributes() + 1];
    }

    double[] result = new double[2];

    double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale;
    double z = (wx + m_weights[m_weights.length - 1]);
    //System.out.print("" + z + ": ");
    // System.out.println(1.0 / (1.0 + Math.exp(-z)));
    if (z <= 0) {
        //  z = 0;
        if (m_loss == LOGLOSS) {
            result[0] = 1.0 / (1.0 + Math.exp(z));
            result[1] = 1.0 - result[0];
        } else {
            result[0] = 1;
        }
    } else {
        if (m_loss == LOGLOSS) {
            result[1] = 1.0 / (1.0 + Math.exp(-z));
            result[0] = 1.0 - result[1];
        } else {
            result[1] = 1;
        }
    }
    return result;
}

From source file:moa.classifiers.LimAttClassifier.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    //Init Ensemble
    if (this.initClassifiers == true) {
        numberAttributes = numAttributesOption.getValue();
        if (bigTreesOption.isSet()) {
            numberAttributes = inst.numAttributes() - 1 - numAttributesOption.getValue();
        }//from   w  w w  . jav  a  2s. com
        CombinationGenerator x = new CombinationGenerator(inst.numAttributes() - 1, this.numberAttributes);
        int numberClassifiers = x.getTotal().intValue();
        this.ensemble = new Classifier[numberClassifiers];
        Classifier baseLearner = (Classifier) getPreparedClassOption(this.baseLearnerOption);
        baseLearner.resetLearning();
        for (int i = 0; i < this.ensemble.length; i++) {
            this.ensemble[i] = baseLearner.copy();
        }
        this.ADError = new ADWIN[this.ensemble.length];
        for (int i = 0; i < this.ensemble.length; i++) {
            this.ADError[i] = new ADWIN((double) this.deltaAdwinOption.getValue());
        }
        this.numberOfChangesDetected = 0;
        //Prepare combinations
        int i = 0;
        if (baseLearner instanceof LimAttHoeffdingTree) {
            while (x.hasMore()) {
                ((LimAttHoeffdingTree) this.ensemble[i]).setlistAttributes(x.getNext());
                i++;
            }
        }

        this.initClassifiers = false;
    }

    boolean Change = false;
    Instance weightedInst = (Instance) inst.copy();

    //Train Perceptron
    double[][] votes = new double[this.ensemble.length + 1][numClasses];
    for (int i = 0; i < this.ensemble.length; i++) {
        double[] v = new double[numClasses];
        for (int j = 0; j < v.length; j++) {
            v[j] = (double) this.oddsOffsetOption.getValue();
        }
        double[] vt = this.ensemble[i].getVotesForInstance(inst);
        double sum = Utils.sum(vt);
        if (!Double.isNaN(sum) && (sum > 0)) {
            for (int j = 0; j < vt.length; j++) {
                vt[j] /= sum;
            }
        } else {
            // Just in case the base learner returns NaN
            for (int k = 0; k < vt.length; k++) {
                vt[k] = 0.0;
            }
        }
        sum = numClasses * (double) this.oddsOffsetOption.getValue();
        for (int j = 0; j < vt.length; j++) {
            v[j] += vt[j];
            sum += vt[j];
        }
        for (int j = 0; j < vt.length; j++) {
            votes[i][j] = Math.log(v[j] / (sum - v[j]));
        }
    }

    if (adwinReplaceWorstClassifierOption.isSet() == false) {
        //Train ensemble of classifiers
        for (int i = 0; i < this.ensemble.length; i++) {
            boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst);
            double ErrEstim = this.ADError[i].getEstimation();
            if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) {
                numInstances = initialNumInstancesOption.getValue();
                if (this.ADError[i].getEstimation() > ErrEstim) {
                    Change = true;
                    //Replace classifier if ADWIN has detected change
                    numberOfChangesDetected++;
                    this.ensemble[i].resetLearning();
                    this.ADError[i] = new ADWIN((double) this.deltaAdwinOption.getValue());
                    for (int ii = 0; ii < inst.numClasses(); ii++) {
                        weightAttribute[ii][i] = 0.0;// 0.2 * Math.random() - 0.1;
                    }
                }
            }
        }
    } else {
        //Train ensemble of classifiers
        for (int i = 0; i < this.ensemble.length; i++) {
            boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst);
            double ErrEstim = this.ADError[i].getEstimation();
            if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) {
                if (this.ADError[i].getEstimation() > ErrEstim) {
                    Change = true;
                }
            }
        }
        //Replace classifier with higher error if ADWIN has detected change
        if (Change) {
            numberOfChangesDetected++;
            double max = 0.0;
            int imax = -1;
            for (int i = 0; i < this.ensemble.length; i++) {
                if (max < this.ADError[i].getEstimation()) {
                    max = this.ADError[i].getEstimation();
                    imax = i;
                }
            }
            if (imax != -1) {
                this.ensemble[imax].resetLearning();
                this.ADError[imax] = new ADWIN((double) this.deltaAdwinOption.getValue());
                for (int ii = 0; ii < inst.numClasses(); ii++) {
                    weightAttribute[ii][imax] = 0.0;
                }
            }
        }
    }

    trainOnInstanceImplPerceptron(inst.numClasses(), (int) inst.classValue(), votes);

    for (int i = 0; i < this.ensemble.length; i++) {
        this.ensemble[i].trainOnInstance(inst);
    }
}

From source file:moa.classifiers.meta.RandomRules.java

License:Open Source License

private Instance transformInstance(Instance inst, int classifierIndex) {
    if (this.listAttributes == null) {
        this.numAttributes = (int) (this.numAttributesPercentageOption.getValue() * inst.numAttributes()
                / 100.0);//ww w  .  ja  v a 2  s. c o m
        this.listAttributes = new int[this.numAttributes][this.ensemble.length];
        this.dataset = new InstancesHeader[this.ensemble.length];
        for (int ensembleIndex = 0; ensembleIndex < this.ensemble.length; ensembleIndex++) {
            for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) {
                boolean isUnique = false;
                while (isUnique == false) {
                    this.listAttributes[attributeIndex][ensembleIndex] = this.classifierRandom
                            .nextInt(inst.numAttributes() - 1);
                    isUnique = true;
                    for (int k = 0; k < attributeIndex; k++) {
                        if (this.listAttributes[attributeIndex][ensembleIndex] == this.listAttributes[k][ensembleIndex]) {
                            isUnique = false;
                            break;
                        }
                    }
                }
                //this.listAttributes[attributeIndex][ensembleIndex] = attributeIndex;
            }
            //Create Header
            FastVector attributes = new FastVector();
            for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) {
                attributes.addElement(inst.attribute(this.listAttributes[attributeIndex][ensembleIndex]));
                System.out.print(this.listAttributes[attributeIndex][ensembleIndex]);
            }
            System.out.println("Number of attributes: " + this.numAttributes + "," + inst.numAttributes());
            attributes.addElement(inst.classAttribute());
            this.dataset[ensembleIndex] = new InstancesHeader(
                    new Instances(getCLICreationString(InstanceStream.class), attributes, 0));
            this.dataset[ensembleIndex].setClassIndex(this.numAttributes);
            this.ensemble[ensembleIndex].setModelContext(this.dataset[ensembleIndex]);
        }
    }
    //Instance instance = new DenseInstance(this.numAttributes+1);
    //instance.setDataset(dataset[classifierIndex]);
    double[] attVals = new double[this.numAttributes + 1];
    for (int attributeIndex = 0; attributeIndex < this.numAttributes; attributeIndex++) {
        //instance.setValue(attributeIndex, inst.value(this.listAttributes[attributeIndex][classifierIndex]));
        attVals[attributeIndex] = inst.value(this.listAttributes[attributeIndex][classifierIndex]);
    }
    Instance instance = new DenseInstance(1.0, attVals);
    instance.setDataset(dataset[classifierIndex]);
    instance.setClassValue(inst.classValue());
    // System.out.println(inst.toString());
    // System.out.println(instance.toString());
    // System.out.println("============");
    return instance;
}

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  w  w.j  a  v  a2s.  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.Perceptron.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {

    //Init Perceptron
    if (this.reset == true) {
        this.reset = false;
        this.numberAttributes = inst.numAttributes();
        this.numberClasses = inst.numClasses();
        this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()];
        for (int i = 0; i < inst.numClasses(); i++) {
            for (int j = 0; j < inst.numAttributes(); j++) {
                weightAttribute[i][j] = 0.2 * Math.random() - 0.1;
            }//from   w  w  w . j  a va  2 s.  co  m
        }
    }

    double[] preds = new double[inst.numClasses()];
    for (int i = 0; i < inst.numClasses(); i++) {
        preds[i] = prediction(inst, i);
    }
    double learningRatio = learningRatioOption.getValue();

    int actualClass = (int) inst.classValue();
    for (int i = 0; i < inst.numClasses(); i++) {
        double actual = (i == actualClass) ? 1.0 : 0.0;
        double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]);
        for (int j = 0; j < inst.numAttributes() - 1; j++) {
            this.weightAttribute[i][j] += learningRatio * delta * inst.value(j);
        }
        this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta;
    }
}

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

License:Apache License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.numInstance = this.numInstance + 1;
    int countRuleFiredTrue = 0;
    boolean ruleFired = false;
    this.instance = inst;
    for (int j = 0; j < this.ruleSet.size(); j++) {
        if (this.ruleSet.get(j).ruleEvaluate(inst) == true) {
            countRuleFiredTrue = countRuleFiredTrue + 1;
            this.saveBestValGlobalSDR = new ArrayList<ArrayList<Double>>();
            this.saveBestGlobalSDR = new DoubleVector();
            this.saveTheBest = new ArrayList<Double>();
            double anomaly = computeAnomaly(this.ruleSet.get(j), j, inst); // compute anomaly
            if ((this.ruleSet.get(j).instancesSeen <= this.anomalyNumInstThresholdOption.getValue())
                    || (anomaly < this.anomalyProbabilityThresholdOption.getValue()
                            && this.anomalyDetectionOption.isSet())
                    || !this.anomalyDetectionOption.isSet()) {
                for (int i = 0; i < inst.numAttributes() - 1; i++) {
                    int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);
                    AttributeClassObserver obs = this.ruleSet.get(j).observers.get(i);
                    if (obs == null) {
                        obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                                : newNumericClassObserverRegression();
                        this.ruleSet.get(j).observers.set(i, obs);
                    }//w w  w . ja v  a2 s. co m
                    obs.observeAttributeTarget(inst.value(instAttIndex), inst.classValue());
                }

                double RuleError = computeRuleError(inst, this.ruleSet.get(j), j); // compute rule error
                boolean ph = PageHinckleyTest(RuleError, this.pageHinckleyThresholdOption.getValue(),
                        this.ruleSet.get(j));
                if (ph == true) { //Page Hinckley test.
                    //Pruning rule set.
                    //   System.out.print("Pruning rule set \n");
                    this.ruleSet.remove(j);
                    this.targetValue.remove(j);
                    this.numTargetValue.remove(j);
                    this.ruleTargetMean.remove(j);
                } else {
                    this.expandeRule(this.ruleSet.get(j), j, inst); //Expand the rule.
                }
            }
            if (this.orderedRulesOption.isSet()) { // Ordered rules
                break;
            }
        }
    }
    if (countRuleFiredTrue > 0) {
        ruleFired = true;
    } else {
        ruleFired = false;
    }
    if (ruleFired == false) { //Default rule
        this.saveBestValGlobalSDR = new ArrayList<ArrayList<Double>>();
        this.saveBestGlobalSDR = new DoubleVector();
        this.saveTheBest = new ArrayList<Double>();
        double anomalies = computeAnomalyDefaultRules(inst);
        if ((instancesSeenDefault <= this.anomalyNumInstThresholdOption.getValue())
                || (anomalies < this.anomalyProbabilityThresholdOption.getValue()
                        && this.anomalyDetectionOption.isSet())
                || !this.anomalyDetectionOption.isSet()) {
            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()
                            : newNumericClassObserverRegression();
                    this.attributeObservers.set(i, obs);
                }
                obs.observeAttributeTarget(inst.value(instAttIndex), inst.classValue());
            }
            initialyPerceptron(inst); // Initialize Perceptron if necessary.  
            this.updateAttWeight(inst, this.weightAttributeDefault, this.squaredActualClassStatisticsDefault,
                    this.actualClassStatisticsDefault, this.squaredAttributeStatisticsDefault,
                    this.attributeStatisticsDefault, this.instancesSeenDefault, resetDefault); // Update weights. Ensure actual class and the predicted class are normalised first.
            this.updatedefaultRuleStatistics(inst); //Update the default rule statistics.
            this.createRule(inst);//This function creates a rule
        }
    }
}

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

License:Apache License

public void theBestAttributes(Instance instance, AutoExpandVector<AttributeClassObserver> observersParameter) {
    for (int z = 0; z < instance.numAttributes() - 1; z++) {
        int instAttIndex = modelAttIndexToInstanceAttIndex(z, instance);
        if (instance.attribute(instAttIndex).isNumeric()) {
            this.root = ((BinaryTreeNumericAttributeClassObserverRegression) observersParameter.get(z)).root1;
            this.sumTotalLeft = 0.0;
            this.sumTotalRight = this.root.lessThan[0] + this.root.greaterThan[0];
            this.sumSqTotalLeft = 0.0;
            this.sumSqTotalRight = this.root.lessThan[1] + this.root.greaterThan[1];
            this.rightTotal = this.total = this.root.lessThan[2] + this.root.greaterThan[2];
            this.maxSDR = 0.0;
            this.symbol = 0.0;
            this.sumTotal = 0.0;
            this.numSomaTotal = 0.0;
            findBestSplit(this.root); // The best value (SDR) of a numeric attribute.
            ArrayList<Double> saveTheBestAtt = new ArrayList<Double>(); // Contains the best attribute.
            saveTheBestAtt.add(this.splitpoint);
            saveTheBestAtt.add(this.maxSDR);
            saveTheBestAtt.add(this.symbol);
            saveTheBestAtt.add(this.sumTotal);
            saveTheBestAtt.add(this.numSomaTotal);
            this.saveBestValGlobalSDR.add(saveTheBestAtt);
            this.saveBestGlobalSDR.setValue(z, this.maxSDR);
        }/*from   ww  w .ja va2  s .  c o  m*/
    }
}

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

License:Apache License

public double computeAnomaly(Rule rl, int ruleIndex, Instance inst) {
    ArrayList<Integer> caseAnomalyTemp = new ArrayList<Integer>();
    ArrayList<ArrayList<Double>> AttribAnomalyStatisticTemp2 = new ArrayList<ArrayList<Double>>();
    double D = 0.0;
    double N = 0.0;
    if (rl.instancesSeen > this.anomalyNumInstThresholdOption.getValue()
            && this.anomalyDetectionOption.isSet()) {
        for (int x = 0; x < inst.numAttributes() - 1; x++) {
            ArrayList<Double> AttribAnomalyStatisticTemp = new ArrayList<Double>();
            if (inst.attribute(x).isNumeric()) {
                double mean = computeMean(rl.attributeStatistics.getValue(x), rl.instancesSeen);
                double sd = computeSD(rl.squaredAttributeStatistics.getValue(x),
                        rl.attributeStatistics.getValue(x), rl.instancesSeen);
                double probability = computeProbability(mean, sd, inst.value(x));
                if (probability != 0.0) {
                    D = D + Math.log(probability);
                    if (probability < this.probabilityThresholdOption.getValue()) { //0.10
                        N = N + Math.log(probability);
                        AttribAnomalyStatisticTemp.add((double) x);
                        AttribAnomalyStatisticTemp.add(inst.value(x));
                        AttribAnomalyStatisticTemp.add(mean);
                        AttribAnomalyStatisticTemp.add(sd);
                        AttribAnomalyStatisticTemp.add(probability);
                        AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp);
                    }//  w  ww  .  j  a v a2s . c o  m
                }
            }
        }
    }
    double anomaly = Math.abs(N / D);
    if (anomaly >= this.anomalyProbabilityThresholdOption.getValue()) {
        caseAnomalyTemp.add(this.numInstance);
        double val = anomaly * 100;
        caseAnomalyTemp.add((int) val);
        this.caseAnomaly.add(caseAnomalyTemp);
        this.ruleSetAnomalies.add(this.ruleSet.get(ruleIndex));
        this.ruleTargetMeanAnomalies.add(this.ruleTargetMean.get(ruleIndex));
        this.ruleAnomaliesIndex.add(ruleIndex + 1);
        this.ruleAttribAnomalyStatistics.add(AttribAnomalyStatisticTemp2);
    }
    return anomaly;
}

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

License:Apache License

public double computeAnomalyDefaultRules(Instance inst) {
    double D = 0.0;
    double N = 0.0;
    ArrayList<Integer> caseAnomalyTemp = new ArrayList<Integer>();
    ArrayList<ArrayList<Double>> AttribAnomalyStatisticTemp2 = new ArrayList<ArrayList<Double>>();
    if (this.instancesSeenDefault > this.anomalyNumInstThresholdOption.getValue()
            && this.anomalyDetectionOption.isSet()) {
        for (int x = 0; x < inst.numAttributes() - 1; x++) {
            ArrayList<Double> AttribAnomalyStatisticTemp = new ArrayList<Double>();
            if (inst.attribute(x).isNumeric()) {
                double mean = computeMean(this.attributeStatisticsDefault.getValue(x),
                        this.instancesSeenDefault);
                double sd = computeSD(this.squaredAttributeStatisticsDefault.getValue(x),
                        this.attributeStatisticsDefault.getValue(x), this.instancesSeenDefault);
                double probability = computeProbability(mean, sd, inst.value(x));
                if (probability != 0.0) {
                    D = D + Math.log(probability);
                    if (probability < this.probabilityThresholdOption.getValue()) { //0.10
                        N = N + Math.log(probability);
                        AttribAnomalyStatisticTemp.add((double) x);
                        AttribAnomalyStatisticTemp.add(inst.value(x));
                        AttribAnomalyStatisticTemp.add(mean);
                        AttribAnomalyStatisticTemp.add(sd);
                        AttribAnomalyStatisticTemp.add(probability);
                        AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp);
                    }/* w  ww .jav a 2s  .  co  m*/
                }
            }
        }
    }
    double anomalies = Math.abs(N / D);
    if (anomalies >= this.anomalyProbabilityThresholdOption.getValue()) {
        caseAnomalyTemp.add(this.numInstance);
        double val = anomalies * 100;
        caseAnomalyTemp.add((int) val);
        this.caseAnomaly.add(caseAnomalyTemp);
        Rule rule = new Rule();
        this.ruleSetAnomalies.add(rule);
        this.ruleTargetMeanAnomalies.add(observersDistrib(this.instance, this.attributeObservers));
        this.ruleAnomaliesIndex.add(-1);
        this.ruleAttribAnomalyStatistics.add(AttribAnomalyStatisticTemp2);
    }
    return anomalies;
}