List of usage examples for weka.core Instance value
public double value(Attribute att);
From source file:moa.classifiers.multilabel.MajorityLabelset.java
License:Open Source License
private static final String toBitString(Instance ins, int c) { StringBuilder sb = new StringBuilder(c); for (int i = 0; i < c; i++) { sb.append((int) Math.round(ins.value(i))); }/* w w w .jav a2 s . c o m*/ return sb.toString(); }
From source file:moa.classifiers.multilabel.MajorityLabelset.java
License:Open Source License
protected void updateCount(Instance x, int L) { String y = toBitString(x, L); if (classFreqs.containsKey(y)) { double freq = classFreqs.get(y) + x.weight(); classFreqs.put(y, freq);/* w w w.jav a 2s.c om*/ if (freq >= maxValue) { maxValue = freq; this.prediction = new double[L]; for (int j = 0; j < L; j++) { this.prediction[j] = x.value(j); } } } else { classFreqs.put(y, x.weight()); } }
From source file:moa.classifiers.multilabel.meta.MLOzaBagAdwin.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { boolean Change = false; for (int i = 0; i < this.ensemble.length; i++) { int k = MiscUtils.poisson(1.0, this.classifierRandom); if (k > 0) { Instance weightedInst = (Instance) inst.copy(); weightedInst.setWeight(inst.weight() * k); this.ensemble[i].trainOnInstance(weightedInst); }//from www. jav a 2 s . c om double[] prediction = this.ensemble[i].getVotesForInstance(inst); //Compute accuracy double actual[] = new double[prediction.length]; for (short j = 0; j < prediction.length; j++) { actual[j] = inst.value(j); } // calculate int p_sum = 0, r_sum = 0; int set_union = 0; int set_inter = 0; double t = 0.01; for (int j = 0; j < prediction.length; j++) { int p = (prediction[j] >= t) ? 1 : 0; int R = (int) actual[j]; if (p == 1) { p_sum++; // predt 1, real 1 if (R == 1) { set_inter++; set_union++; } // predt 1, real 0 else { set_union++; } } else { // predt 0, real 1 if (R == 1) { set_union++; } // predt 0, real 0 else { } } } double accuracy = 0.0; if (set_union > 0) //avoid NaN { accuracy = ((double) set_inter / (double) set_union); } double ErrEstim = this.ADError[i].getEstimation(); if (this.ADError[i].setInput(1.0 - accuracy)) { if (this.ADError[i].getEstimation() > ErrEstim) { Change = true; } } } if (Change) { System.err.println("change!"); 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] = null; this.ensemble[imax] = (Classifier) getPreparedClassOption(this.baseLearnerOption); this.ensemble[imax].setModelContext(this.modelContext); this.ensemble[imax].trainOnInstance(inst); this.ADError[imax] = new ADWIN(); } } }
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 *//*from ww w. j ava 2 s .c o m*/ @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 www . j a v a 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); }/*www .java 2 s . c om*/ 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 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); }//from w w w. j a v a 2 s. com } } } } 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); }//from w w w . j a v a2 s . c o 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; }
From source file:moa.classifiers.rules.AMRules.java
License:Apache License
public double prediction(Instance inst, double[] weightAtt, double squaredActualClassStatistics, double actualClassStatistics, int instancesSeen, boolean reset) { double prediction = 0; if (reset == false) { for (int j = 0; j < inst.numAttributes() - 1; j++) { if (inst.attribute(j).isNumeric()) { prediction += weightAtt[j] * inst.value(j); }/*from w ww . j av a2 s . c o m*/ } prediction += weightAtt[inst.numAttributes() - 1]; } double sdPredictedClass = computeSD(squaredActualClassStatistics, actualClassStatistics, instancesSeen); double outputDesnorm = 0; if (sdPredictedClass > 0.0000001) { outputDesnorm = 3 * prediction * sdPredictedClass + (actualClassStatistics / instancesSeen); } return outputDesnorm; }
From source file:moa.classifiers.rules.AMRules.java
License:Apache License
public double updateAttWeight(Instance inst, double[] weightAtt, double squaredActualClassStatistics, double actualClassStatistics, DoubleVector squaredAttributeStatistics, DoubleVector attributeStatistics, int instancesSeen, boolean reset) { double learningRatio = 0.0; if (this.learningRatio_Decay_or_Const_Option.isSet()) { //Decaying learning rate option learningRatio = this.learningRatioOption.getValue(); } else {//from w w w. j a v a2s. co m learningRatio = initLearnRate / (1 + instancesSeen * this.learnRateDecay); } double predict = 0.0; if (instancesSeen > 30) { predict = this.prediction(inst, weightAtt, squaredActualClassStatistics, actualClassStatistics, instancesSeen, reset); double sdClass = computeSD(squaredActualClassStatistics, actualClassStatistics, instancesSeen); double actualClass = 0.0; double predictedClass = 0.0; if (sdClass > 0.0000001) { actualClass = (inst.classValue() - (actualClassStatistics / instancesSeen)) / (3 * sdClass); predictedClass = (predict - (actualClassStatistics / instancesSeen)) / (3 * sdClass); } double delta = actualClass - predictedClass; for (int x = 0; x < inst.numAttributes() - 1; x++) { if (inst.attribute(x).isNumeric()) { // Update weights. Ensure attribute values are normalised first. double sd = Math.sqrt((squaredAttributeStatistics.getValue(x) - ((attributeStatistics.getValue(x) * attributeStatistics.getValue(x)) / instancesSeen)) / instancesSeen); double instanceValue = 0; instanceValue = (inst.value(x) - (attributeStatistics.getValue(x) / instancesSeen)); if (sd > 0.0000001) { instanceValue = instanceValue / (3 * sd); } if (sd == 0.0) { weightAtt[x] = 0.0; } else { weightAtt[x] += learningRatio * delta * instanceValue; } } } weightAtt[inst.numAttributes() - 1] += learningRatio * delta; } return predict; }