List of usage examples for weka.core Instance numAttributes
public int numAttributes();
From source file:moa.classifiers.rules.functions.Perceptron.java
License:Apache License
public double updateWeights(Instance inst, double learningRatio) { // Normalize Instance double[] normalizedInstance = normalizedInstance(inst); // Compute the Normalized Prediction of Perceptron double normalizedPredict = prediction(normalizedInstance); double normalizedY = normalizeActualClassValue(inst); double sumWeights = 0.0; double delta = normalizedY - normalizedPredict; for (int j = 0; j < inst.numAttributes() - 1; j++) { int instAttIndex = modelAttIndexToInstanceAttIndex(j, inst); if (inst.attribute(instAttIndex).isNumeric()) { this.weightAttribute[j] += learningRatio * delta * normalizedInstance[j]; sumWeights += Math.abs(this.weightAttribute[j]); }//from ww w . j a va 2 s .c o m } this.weightAttribute[inst.numAttributes() - 1] += learningRatio * delta; sumWeights += Math.abs(this.weightAttribute[inst.numAttributes() - 1]); if (sumWeights > inst.numAttributes()) { // Lasso regression for (int j = 0; j < inst.numAttributes() - 1; j++) { int instAttIndex = modelAttIndexToInstanceAttIndex(j, inst); if (inst.attribute(instAttIndex).isNumeric()) { this.weightAttribute[j] = this.weightAttribute[j] / sumWeights; } } this.weightAttribute[inst.numAttributes() - 1] = this.weightAttribute[inst.numAttributes() - 1] / sumWeights; } return denormalizedPrediction(normalizedPredict); }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
@Override public void trainOnInstanceImpl(Instance inst) { int countRuleFiredTrue = 0; boolean ruleFired = false; this.instance = inst; this.numAttributes = instance.numAttributes() - 1; this.numClass = instance.numClasses(); this.numInstance = numInstance + 1; int conta1 = 0; for (int j = 0; j < ruleSet.size(); j++) { if (this.ruleSet.get(j).ruleEvaluate(inst) == true) { countRuleFiredTrue = countRuleFiredTrue + 1; double anomaly = 0.0; if (this.Supervised.isSet()) { anomaly = computeAnomalySupervised(this.ruleSet.get(j), j, inst); // compute anomaly (Supervised method) } else if (this.Unsupervised.isSet()) { anomaly = computeAnomalyUnsupervised(this.ruleSet.get(j), j, inst); // compute anomaly (Unsupervised method) }/*w w w. ja va 2 s .c om*/ if (anomaly >= this.anomalyProbabilityThresholdOption.getValue()) { conta1 = conta1 + 1; } // System.out.print(numInstance+";"+anomaly+"\n"); try { File dir = new File("SeaAnomaliesUnsupervised.txt"); FileWriter fileWriter = new FileWriter(dir, true); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println(numInstance + ";" + anomaly); printWriter.flush(); printWriter.close(); } catch (IOException e) { e.printStackTrace(); } if ((this.ruleSet.get(j).instancesSeen <= this.anomalyNumInstThresholdOption.getValue()) || (anomaly < this.anomalyProbabilityThresholdOption.getValue() && this.anomalyDetectionOption.isSet()) || !this.anomalyDetectionOption.isSet()) { this.ruleSet.get(j).obserClassDistrib.addToValue((int) inst.classValue(), inst.weight()); for (int i = 0; i < inst.numAttributes() - 1; i++) { int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst); if (!inst.isMissing(instAttIndex)) { AttributeClassObserver obs = this.ruleSet.get(j).observers.get(i); // Nominal and binary tree. AttributeClassObserver obsGauss = this.ruleSet.get(j).observersGauss.get(i); // Gaussian. if (obs == null) { obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver() : newNumericClassObserver(); this.ruleSet.get(j).observers.set(i, obs); } if (obsGauss == null) { obsGauss = inst.attribute(instAttIndex).isNumeric() ? newNumericClassObserver2() : null; this.ruleSet.get(j).observersGauss.set(i, obsGauss); } obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); if (inst.attribute(instAttIndex).isNumeric()) { obsGauss.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); } } } expandeRule(this.ruleSet.get(j), inst, j); // This function expands the rule } if (this.orderedRulesOption.isSet()) { // Ordered rules break; } } } if (countRuleFiredTrue > 0) { ruleFired = true; } else { ruleFired = false; } if (ruleFired == false) { //If none of the rules cover the example update sufficient statistics of the default rule this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight()); for (int i = 0; i < inst.numAttributes() - 1; i++) { int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst); if (!inst.isMissing(instAttIndex)) { AttributeClassObserver obs = this.attributeObservers.get(i); AttributeClassObserver obsGauss = this.attributeObserversGauss.get(i); if (obs == null) { obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver() : newNumericClassObserver(); this.attributeObservers.set(i, obs); } if (obsGauss == null) { obsGauss = inst.attribute(instAttIndex).isNumeric() ? newNumericClassObserver2() : null; this.attributeObserversGauss.set(i, obsGauss); } obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); if (inst.attribute(instAttIndex).isNumeric()) { obsGauss.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight()); } } } createRule(inst); //This function creates a rule } }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
public void updateRuleAttribStatistics(Instance inst, Rule rl, int ruleIndex) { rl.instancesSeen++;/*from www . ja v a 2 s. c o m*/ if (rl.squaredAttributeStatisticsSupervised.size() == 0 && rl.attributeStatisticsSupervised.size() == 0) { for (int s = 0; s < inst.numAttributes() - 1; s++) { ArrayList<Double> temp1 = new ArrayList<Double>(); ArrayList<Double> temp2 = new ArrayList<Double>(); rl.attributeStatisticsSupervised.add(temp1); rl.squaredAttributeStatisticsSupervised.add(temp2); int instAttIndex = modelAttIndexToInstanceAttIndex(s, inst); if (instance.attribute(instAttIndex).isNumeric()) { for (int i = 0; i < inst.numClasses(); i++) { rl.attributeStatisticsSupervised.get(s).add(0.0); rl.squaredAttributeStatisticsSupervised.get(s).add(1.0); } } } } for (int s = 0; s < inst.numAttributes() - 1; s++) { int instAttIndex = modelAttIndexToInstanceAttIndex(s, inst); if (!inst.isMissing(instAttIndex)) { if (instance.attribute(instAttIndex).isNumeric()) { rl.attributeStatistics.addToValue(s, inst.value(s)); rl.squaredAttributeStatistics.addToValue(s, inst.value(s) * inst.value(s)); double sumValue = rl.attributeStatisticsSupervised.get(s).get((int) inst.classValue()) + inst.value(s); rl.attributeStatisticsSupervised.get(s).set((int) inst.classValue(), sumValue); double squaredSumvalue = rl.squaredAttributeStatisticsSupervised.get(s) .get((int) inst.classValue()) + (inst.value(s) * inst.value(s)); rl.squaredAttributeStatisticsSupervised.get(s).set((int) inst.classValue(), squaredSumvalue); } } else { rl.attributeMissingValues.addToValue(s, 1); } } }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
public double computeAnomalyUnsupervised(Rule rl, int ruleIndex, Instance inst) { //Unsupervised 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++) { if (!inst.isMissing(x)) { ArrayList<Double> AttribAnomalyStatisticTemp = new ArrayList<Double>(); if (inst.attribute(x).isNumeric()) { //Numeric Attributes if ((rl.instancesSeen - rl.attributeMissingValues.getValue(x)) > 30) { 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 ww .j a va 2 s .c o m } } } else { //Nominal Attributes AutoExpandVector<DoubleVector> attribNominal = ((NominalAttributeClassObserver) rl.observers .get(x)).attValDistPerClass; //Attributes values distribution per class double numbAttribValue = 0.0; double attribVal = inst.value(x); //Attribute value for (int i = 0; i < attribNominal.size(); i++) { if (attribNominal.get(i) != null) { numbAttribValue = numbAttribValue + attribNominal.get(i).getValue((int) attribVal); } } double probability = numbAttribValue / rl.instancesSeen; 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(probability); AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp); } } } } } } double anomaly = 0.0; if (D != 0) { 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); Rule x = new Rule(this.ruleSet.get(ruleIndex)); this.ruleSetAnomalies.add(x); this.ruleAnomaliesIndex.add(ruleIndex + 1); this.ruleAttribAnomalyStatistics.add(AttribAnomalyStatisticTemp2); } return anomaly; }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
public double computeAnomalySupervised(Rule rl, int ruleIndex, Instance inst) { //Not supervised 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++) { if (!inst.isMissing(x)) { ArrayList<Double> AttribAnomalyStatisticTemp = new ArrayList<Double>(); if (inst.attribute(x).isNumeric()) { //Numeric Attributes if ((rl.instancesSeen - rl.attributeMissingValues.getValue(x)) > 30) { double mean = computeMean( (double) rl.attributeStatisticsSupervised.get(x).get((int) inst.classValue()), (int) rl.obserClassDistrib.getValue((int) inst.classValue())); double sd = computeSD( (double) rl.squaredAttributeStatisticsSupervised.get(x) .get((int) inst.classValue()), (double) rl.attributeStatisticsSupervised.get(x).get((int) inst.classValue()), (int) rl.obserClassDistrib.getValue((int) inst.classValue())); 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 .jav a 2 s .com } } } else { //Nominal double attribVal = inst.value(x); //Attribute value double classVal = inst.classValue(); //Attribute value double probability = rl.observers.get(x).probabilityOfAttributeValueGivenClass(attribVal, (int) classVal); 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(probability); AttribAnomalyStatisticTemp2.add(AttribAnomalyStatisticTemp); } } } } } } double anomaly = 0.0; if (D != 0) { anomaly = Math.abs(N / D); } if (anomaly >= this.anomalyProbabilityThresholdOption.getValue()) { caseAnomalyTemp.add(this.numInstance); double val = anomaly * 100; caseAnomalyTemp.add((int) val); this.caseAnomalySupervised.add(caseAnomalyTemp); Rule y = new Rule(this.ruleSet.get(ruleIndex)); this.ruleSetAnomaliesSupervised.add(y); this.ruleAnomaliesIndexSupervised.add(ruleIndex + 1); this.ruleAttribAnomalyStatisticsSupervised.add(AttribAnomalyStatisticTemp2); } return anomaly; }
From source file:moa.classifiers.rules.RuleClassifier.java
License:Apache License
public void theBestAttributes(Instance instance, AutoExpandVector<AttributeClassObserver> observersParameter) { for (int z = 0; z < instance.numAttributes() - 1; z++) { if (!instance.isMissing(z)) { int instAttIndex = modelAttIndexToInstanceAttIndex(z, instance); ArrayList<Double> attribBest = new ArrayList<Double>(); if (instance.attribute(instAttIndex).isNominal()) { this.minEntropyNominalAttrib = Double.MAX_VALUE; AutoExpandVector<DoubleVector> attribNominal = ((NominalAttributeClassObserver) observersParameter .get(z)).attValDistPerClass; findBestValEntropyNominalAtt(attribNominal, instance.attribute(z).numValues()); // The best value (lowest entropy) of a nominal attribute. attribBest.add(this.saveBestEntropyNominalAttrib.getValue(0)); attribBest.add(this.saveBestEntropyNominalAttrib.getValue(1)); attribBest.add(this.saveBestEntropyNominalAttrib.getValue(2)); this.saveBestValGlobalEntropy.add(attribBest); this.saveBestGlobalEntropy.setValue(z, this.saveBestEntropyNominalAttrib.getValue(1)); } else { this.root = ((BinaryTreeNumericAttributeClassObserver) observersParameter.get(z)).root; mainFindBestValEntropy(this.root); // The best value (lowest entropy) of a numeric attribute. attribBest.add(this.saveBestEntropy.getValue(0)); attribBest.add(this.saveBestEntropy.getValue(1)); attribBest.add(this.saveBestEntropy.getValue(2)); attribBest.add(this.saveBestEntropy.getValue(4)); this.saveBestValGlobalEntropy.add(attribBest); this.saveBestGlobalEntropy.setValue(z, this.saveBestEntropy.getValue(1)); }// w w w . ja v a2s . co m } else { double value = Double.MAX_VALUE; this.saveBestGlobalEntropy.setValue(z, value); } } }
From source file:moa.cluster.Riffle.java
License:Apache License
/** * Create a new cluster from an exemplar data point * @param x // w w w. jav a2 s . co m */ public Riffle(Instance x) { safeInit(x); this.numLabeledPoints = (int) Math.ceil(x.weight()); this.labelFrequencies[(int) x.classValue()] += x.weight(); this.gtLabelFrequencies[(int) x.classValue()]++; for (int i = 0; (i < this.symbolFrequencies.length) && (i < x.numAttributes()); ++i) { double value = x.value(i); if (this.symbolFrequencies[i] == null) { if ((this.parentClusterer != null) && (this.parentClusterer.getUniverse() != null)) { this.variances[i] = this.parentClusterer.getUniverse().variances[i]; } else { this.variances[i] = this.initialStandardDeviationOption.getValue(); } } else { this.variances[i] = 1; this.symbolFrequencies[i][(int) value]++; } } this.numTotalPoints = 1; this.setGroundTruth(x.classValue()); this.setCenter(x.toDoubleArray()); this.setWeight(x.weight()); this.setRadius(this.initialStandardDeviationOption.getValue()); this.runningSumOfSquares = 0.0; this.setId(autoindex.getAndIncrement()); }
From source file:moa.cluster.Riffle.java
License:Apache License
/** * Sanity check and initialization of dynamic fields * * @param x/*from w w w.j ava 2 s. com*/ */ 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.clusterer.FeS2.java
License:Apache License
/** * In cases where this class is not used by the moa.tasks.EvaluateNonStationaryDynamicStream task, * this safety (fallback) initialization procedure is necessary. * @param x /* www .ja v a 2 s. c om*/ */ public void safeInit(Instance x) { if (this.universalCluster == null) { universalCluster = new Riffle(x); universalCluster.updateStrategyOption.setChosenIndex(this.updateStrategyOption.getChosenIndex()); universalCluster.outlierDefinitionStrategyOption .setChosenIndex(this.outlierDefinitionStrategyOption.getChosenIndex()); universalCluster.distanceStrategyOption.setChosenIndex(this.distanceStrategyOption.getChosenIndex()); universalCluster.initialStandardDeviationOption .setValue(this.initialStandardDeviationOption.getValue()); universalCluster.alphaAdjustmentWeightOption.setValue(this.learningRateAlphaOption.getValue()); double[] initialVariances = new double[x.numAttributes()]; Arrays.fill(initialVariances, 1.0); universalCluster.setVariances(initialVariances); universalCluster.setWeight(0); universalCluster.recompute(); this.knownLabels.clear(); bestProbabilitySums = 0; bestProbabilityCount = 0; } }
From source file:moa.clusterer.outliers.Sieve.java
License:Apache License
/** * In cases where this class is not used by the moa.tasks.EvaluateNonStationaryDynamicStream task, this safety * (fallback) initialization procedure is necessary. * * @param x/*from w ww. j a va 2s . c o m*/ */ public final void safeInit(Instance x) { if (this.universalCluster == null) { universalCluster = new Riffle(x); universalCluster.distanceStrategyOption.setChosenIndex(this.distanceStrategyOption.getChosenIndex()); double[] initialVariances = new double[x.numAttributes()]; Arrays.fill(initialVariances, 1.0); universalCluster.setVariances(initialVariances); universalCluster.setWeight(0); universalCluster.recompute(); bestProbabilitySums = 0; bestProbabilityCount = 0; } if (this.knownLabels == null) { this.knownLabels = new int[x.numClasses()]; Arrays.fill(knownLabels, 0); this.numAttributes = x.numAttributes(); } if (this.header == null) { this.header = AbstractNovelClassClassifier.augmentInstances(x.dataset()); } }