List of usage examples for weka.core Instance value
public double value(Attribute att);
From source file:moa.core.utils.EvalUtils.java
License:Open Source License
/** * Convert Instance to bit array. Returns the labels of this instance as an * int array relevance representation.// ww w . j a v a 2 s. co m */ public static final int[] toIntArray(Instance x, int L) { int y[] = new int[L]; for (int j = 0; j < L; j++) { y[j] = (int) Math.round(x.value(j)); } return y; }
From source file:moa.evaluation.BasicConceptDriftPerformanceEvaluator.java
License:Open Source License
@Override public void addResult(Instance inst, double[] classVotes) { //classVotes[0] -> is Change //classVotes[1] -> is in Warning Zone //classVotes[2] -> delay //classVotes[3] -> estimation this.inputValues = inst.value(2); if (inst.weight() > 0.0 && classVotes.length == 4) { if (inst.numAttributes() > 1) { //if there is ground truth we monitor delay this.delay++; }/*from w ww . ja va2 s . com*/ this.weightObserved += inst.weight(); if (classVotes[0] == 1.0) { //Change detected //System.out.println("Change detected with delay "+ this.delay ); this.numberDetections += inst.weight(); if (this.hasChangeOccurred == true) { this.totalDelay += this.delay - classVotes[2]; this.numberDetectionsOccurred += inst.weight(); this.hasChangeOccurred = false; } } if (this.hasChangeOccurred && classVotes[1] == 1.0) { //Warning detected //System.out.println("Warning detected at "+getTotalWeightObserved()); if (this.isWarningZone == false) { this.numberWarnings += inst.weight(); this.isWarningZone = true; } } else { this.isWarningZone = false; } if (inst.numAttributes() > 1) { if (inst.value(inst.numAttributes() - 2) == 1.0) {//Attribute 1 //Ground truth Change this.numberChanges += inst.weight(); this.delay = 0; this.hasChangeOccurred = true; } } //Compute error prediction if (classVotes.length > 1) { this.errorPrediction += Math.abs(classVotes[3] - inst.value(0)); } } }
From source file:moa.evaluation.CMM_GTAnalysis.java
License:Apache License
/** * Calculates Euclidian distance //from w w w .j a va 2s . c om * @param inst1 point as an instance * @param inst2 point as double array * @return euclidian distance */ private double distance(Instance inst1, double[] inst2) { double distance = 0.0; for (int i = 0; i < numDims; i++) { double d = inst1.value(i) - inst2[i]; distance += d * d; } return Math.sqrt(distance); }
From source file:moa.learners.ChangeDetectorLearner.java
License:Open Source License
@Override public void trainOnInstanceImpl(Instance inst) { this.driftDetectionMethod.input(inst.value(0)); }
From source file:moa.streams.clustering.FileStream.java
License:Apache License
protected boolean readNextInstanceFromFile() { try {/* w w w. j a va 2s.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); } }
From source file:moa.streams.clustering.FileStream.java
License:Apache License
/** * @param ignoredAttributes Attributes that will be ignored * @return A list with min/max and diff=max-min values per attribute of the arff file *///from w w w .ja v a2 s . c om protected ArrayList<Double[]> readMinMaxDiffValues(HashSet<Integer> ignoredAttributes) { ArrayList<Double[]> valuesMinMaxDiff = null; if (ignoredAttributes == null) ignoredAttributes = new HashSet<Integer>(); try { InputStream fileStream = new FileInputStream(arffFileOption.getFile()); InputStreamProgressMonitor fileProgressMonitor = new InputStreamProgressMonitor(fileStream); Reader fileReader = new BufferedReader(new InputStreamReader(fileProgressMonitor)); Instances instances = new Instances(fileReader, 1); valuesMinMaxDiff = new ArrayList<Double[]>(); for (int i = 0; i < instances.numAttributes() - ignoredAttributes.size(); i++) { Double[] values = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0 }; valuesMinMaxDiff.add(values); } System.out.print("Reading arff file for normalization..."); int counter = 0; while (instances.readInstance(fileReader)) { Instance instance = instances.instance(0); int a = 0; for (int i = 0; i < instances.numAttributes(); i++) { if (!ignoredAttributes.contains(i)) { double value = instance.value(i); if (value < valuesMinMaxDiff.get(a)[0]) valuesMinMaxDiff.get(a)[0] = value; if (value > valuesMinMaxDiff.get(a)[1]) valuesMinMaxDiff.get(a)[1] = value; a++; } } instances.delete(); //show some progress counter++; if (counter >= 10000) { counter = 0; System.out.print("."); } } if (fileReader != null) { fileReader.close(); fileReader = null; } System.out.println("done!"); for (int i = 0; i < valuesMinMaxDiff.size(); i++) { valuesMinMaxDiff.get(i)[2] = valuesMinMaxDiff.get(i)[1] - valuesMinMaxDiff.get(i)[0]; } return valuesMinMaxDiff; } catch (IOException ioe) { throw new RuntimeException("ArffFileStream failed to read instance from stream.", ioe); } }
From source file:moa.streams.filters.AddNoiseFilter.java
License:Open Source License
@Override public Instance nextInstance() { Instance inst = (Instance) this.inputStream.nextInstance().copy(); for (int i = 0; i < inst.numAttributes(); i++) { double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue() : this.attNoiseFractionOption.getValue(); if (inst.attribute(i).isNominal()) { DoubleVector obs = (DoubleVector) this.attValObservers.get(i); if (obs == null) { obs = new DoubleVector(); this.attValObservers.set(i, obs); }//from ww w . ja v a2s . co m int originalVal = (int) inst.value(i); if (!inst.isMissing(i)) { obs.addToValue(originalVal, inst.weight()); } if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) { do { inst.setValue(i, this.random.nextInt(obs.numValues())); } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0)); } } else { GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i); if (obs == null) { obs = new GaussianEstimator(); this.attValObservers.set(i, obs); } obs.addObservation(inst.value(i), inst.weight()); inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac); } } return inst; }
From source file:moa.streams.filters.ReplacingMissingValuesFilter.java
License:Open Source License
@Override public Instance nextInstance() { Instance inst = (Instance) this.inputStream.nextInstance().copy(); // Initialization if (numAttributes < 0) { numAttributes = inst.numAttributes(); columnsStatistics = new double[numAttributes]; numberOfSamples = new long[numAttributes]; lastNominalValues = new String[numAttributes]; frequencies = new HashMap[numAttributes]; for (int i = 0; i < inst.numAttributes(); i++) { if (inst.attribute(i).isNominal()) frequencies[i] = new HashMap<String, Integer>(); }//w w w . j ava 2 s. c om numericalSelectedStrategy = this.numericReplacementStrategyOption.getChosenIndex(); nominalSelectedStrategy = this.nominalReplacementStrategyOption.getChosenIndex(); } for (int i = 0; i < numAttributes; i++) { // ---- Numerical values ---- if (inst.attribute(i).isNumeric()) { // Handle missing value if (inst.isMissing(i)) { switch (numericalSelectedStrategy) { case 0: // NOTHING break; case 1: // LAST KNOWN VALUE case 2: // MEAN case 3: // MAX case 4: // MIN inst.setValue(i, columnsStatistics[i]); break; case 5: // CONSTANT inst.setValue(i, numericalConstantValueOption.getValue()); break; default: continue; } } // Update statistics with non-missing values else { switch (numericalSelectedStrategy) { case 1: // LAST KNOWN VALUE columnsStatistics[i] = inst.value(i); break; case 2: // MEAN numberOfSamples[i]++; columnsStatistics[i] = columnsStatistics[i] + (inst.value(i) - columnsStatistics[i]) / numberOfSamples[i]; break; case 3: // MAX columnsStatistics[i] = columnsStatistics[i] < inst.value(i) ? inst.value(i) : columnsStatistics[i]; break; case 4: // MIN columnsStatistics[i] = columnsStatistics[i] > inst.value(i) ? inst.value(i) : columnsStatistics[i]; break; default: continue; } } } // ---- Nominal values ---- else if (inst.attribute(i).isNominal()) { // Handle missing value if (inst.isMissing(i)) { switch (nominalSelectedStrategy) { case 0: // NOTHING break; case 1: // LAST KNOWN VALUE if (lastNominalValues[i] != null) { inst.setValue(i, lastNominalValues[i]); } break; case 2: // MODE if (!frequencies[i].isEmpty()) { // Sort the map to get the most frequent value Map<String, Integer> sortedMap = MapUtil.sortByValue(frequencies[i]); inst.setValue(i, sortedMap.entrySet().iterator().next().getKey()); } break; default: continue; } } // Update statistics with non-missing values else { switch (nominalSelectedStrategy) { case 1: // LAST KNOWN VALUE lastNominalValues[i] = inst.stringValue(i); break; case 2: // MODE Integer previousCounter = frequencies[i].get(inst.stringValue(i)); if (previousCounter == null) previousCounter = 0; frequencies[i].put(inst.stringValue(i), ++previousCounter); break; default: continue; } } } } return inst; }
From source file:moa.streams.generators.multilabel.MetaMultilabelGenerator.java
License:Open Source License
/** * GenerateMLInstance.//from www . j a v a 2 s.c o m * * @param Y a set of label [indices] * @return a multit-labelled example */ private Instance generateMLInstance(HashSet<Integer> Y) { // create a multi-label instance: Instance x_ml = new SparseInstance(this.multilabelStreamTemplate.numAttributes()); x_ml.setDataset(this.multilabelStreamTemplate); // set classes for (int j = 0; j < m_L; j++) { x_ml.setValue(j, 0.0); } for (int l : Y) { x_ml.setValue(l, 1.0); } // generate binary instances Instance x_0 = getNextWithBinary(0); Instance x_1 = getNextWithBinary(1); // Loop through each feature attribute @warning: assumes class is last index for (int a = 0; a < m_A; a++) { // The combination is present: use a positive value if (Y.containsAll(m_TopCombinations[a])) { x_ml.setValue(m_L + a, x_1.value(a)); //x_ml.setValue(m_L+a,1.0); } // The combination is absent: use a negative value else { x_ml.setValue(m_L + a, x_0.value(a)); //x_ml.setValue(m_L+a,0.0); } } return x_ml; }
From source file:mulan.classifier.lazy.BRkNN.java
License:Open Source License
/** * Select the best value for k by hold-one-out cross-validation. Hamming * Loss is minimized//from w ww . j a v a 2 s . c o m * * @throws Exception */ protected void crossValidate() throws Exception { try { // the performance for each different k double[] hammingLoss = new double[cvMaxK]; for (int i = 0; i < cvMaxK; i++) { hammingLoss[i] = 0; } Instances dataSet = train; Instance instance; // the hold out instance Instances neighbours; // the neighboring instances double[] origDistances, convertedDistances; for (int i = 0; i < dataSet.numInstances(); i++) { if (getDebug() && (i % 50 == 0)) { debug("Cross validating " + i + "/" + dataSet.numInstances() + "\r"); } instance = dataSet.instance(i); neighbours = lnn.kNearestNeighbours(instance, cvMaxK); origDistances = lnn.getDistances(); // gathering the true labels for the instance boolean[] trueLabels = new boolean[numLabels]; for (int counter = 0; counter < numLabels; counter++) { int classIdx = labelIndices[counter]; String classValue = instance.attribute(classIdx).value((int) instance.value(classIdx)); trueLabels[counter] = classValue.equals("1"); } // calculate the performance metric for each different k for (int j = cvMaxK; j > 0; j--) { convertedDistances = new double[origDistances.length]; System.arraycopy(origDistances, 0, convertedDistances, 0, origDistances.length); double[] confidences = this.getConfidences(neighbours, convertedDistances); boolean[] bipartition = null; switch (extension) { case NONE: // BRknn MultiLabelOutput results; results = new MultiLabelOutput(confidences, 0.5); bipartition = results.getBipartition(); break; case EXTA: // BRknn-a bipartition = labelsFromConfidences2(confidences); break; case EXTB: // BRknn-b bipartition = labelsFromConfidences3(confidences); break; } double symmetricDifference = 0; // |Y xor Z| for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) { boolean actual = trueLabels[labelIndex]; boolean predicted = bipartition[labelIndex]; if (predicted != actual) { symmetricDifference++; } } hammingLoss[j - 1] += (symmetricDifference / numLabels); neighbours = new IBk().pruneToK(neighbours, convertedDistances, j - 1); } } // Display the results of the cross-validation if (getDebug()) { for (int i = cvMaxK; i > 0; i--) { debug("Hold-one-out performance of " + (i) + " neighbors "); debug("(Hamming Loss) = " + hammingLoss[i - 1] / dataSet.numInstances()); } } // Check through the performance stats and select the best // k value (or the lowest k if more than one best) double[] searchStats = hammingLoss; double bestPerformance = Double.NaN; int bestK = 1; for (int i = 0; i < cvMaxK; i++) { if (Double.isNaN(bestPerformance) || (bestPerformance > searchStats[i])) { bestPerformance = searchStats[i]; bestK = i + 1; } } numOfNeighbors = bestK; if (getDebug()) { System.err.println("Selected k = " + bestK); } } catch (Exception ex) { throw new Error("Couldn't optimize by cross-validation: " + ex.getMessage()); } }