/** * Creates a prediction history from the scratch. */ private List<Integer> getTestTrainPredHistory(Instances instances, int startIdx){ List<Integer> predictionHistory = new ArrayList<>(); for (int i= startIdx + this.kOption.getValue(); i < instances.numInstances(); i++){ predictionHistory.add((this.getLabelFct(distanceMatrixSTM[i], instances, startIdx, i-1)==instances.get(i).classValue()) ? 1 : 0); } return predictionHistory; } /**
/** * Creates a prediction history incrementally by using the previous predictions. */ private List<Integer> getIncrementalTestTrainPredHistory(Instances instances, int startIdx, List<Integer> predictionHistory){ for (int i= startIdx + this.kOption.getValue() + predictionHistory.size(); i < instances.numInstances(); i++){ predictionHistory.add((this.getLabelFct(distanceMatrixSTM[i], instances, startIdx, i-1)==instances.get(i).classValue()) ? 1 : 0); } return predictionHistory; } /**
/** * Returns the Euclidean distance between one sample and a collection of samples in an 1D-array. */ private double[] get1ToNDistances(Instance sample, Instances samples){ double distances[] = new double[samples.numInstances()]; for (int i=0; i<samples.numInstances(); i++){ distances[i] = this.getDistance(sample, samples.get(i)); } return distances; }
private void cleanSingle(Instances cleanAgainst, int cleanAgainstindex, Instances toClean){ Instances cleanAgainstTmp = new Instances(cleanAgainst); cleanAgainstTmp.delete(cleanAgainstindex); double distancesSTM[] = get1ToNDistances(cleanAgainst.get(cleanAgainstindex), cleanAgainstTmp); int nnIndicesSTM[] = nArgMin(Math.min(this.kOption.getValue(), distancesSTM.length), distancesSTM); double distancesLTM[] = get1ToNDistances(cleanAgainst.get(cleanAgainstindex), toClean); int nnIndicesLTM[] = nArgMin(Math.min(this.kOption.getValue(), distancesLTM.length), distancesLTM); double distThreshold = 0; for (int nnIdx: nnIndicesSTM){ if (cleanAgainstTmp.get(nnIdx).classValue() == cleanAgainst.get(cleanAgainstindex).classValue()){ if (distancesSTM[nnIdx] > distThreshold){ distThreshold = distancesSTM[nnIdx]; } } } List<Integer> delIndices = new ArrayList<>(); for (int nnIdx: nnIndicesLTM){ if (toClean.get(nnIdx).classValue() != cleanAgainst.get(cleanAgainstindex).classValue()) { if (distancesLTM[nnIdx] <= distThreshold){ delIndices.add(nnIdx); } } } Collections.sort(delIndices, Collections.reverseOrder()); for (Integer idx : delIndices) toClean.delete(idx); } /**
/** * Makes sure that the STM and LTM combined doe not surpass the maximum size. */ private void memorySizeCheck(){ if (this.stm.numInstances() + this.ltm.numInstances() > this.maxSTMSize + this.maxLTMSize){ if (this.ltm.numInstances() > this.maxLTMSize){ this.clusterDown(); }else{ //shift values from STM directly to LTM since STM is full int numShifts = this.maxLTMSize - this.ltm.numInstances() + 1; for (int i = 0; i < numShifts; i++){ this.ltm.add(this.stm.get(0).copy()); this.stm.delete(0); this.stmHistory.remove(0); this.ltmHistory.remove(0); this.cmHistory.remove(0); } this.clusterDown(); this.predictionHistories.clear(); for (int i = 0; i < this.stm.numInstances(); i++){ for (int j = 0; j < this.stm.numInstances(); j++){ this.distanceMatrixSTM[i][j] = this.distanceMatrixSTM[numShifts+i][numShifts+j]; } } } } }
@Override public Example<Instance> nextInstance() { // a value between 0.0 and 1.0 uniformly distributed double p = random.nextDouble(); int iClass = -1; // loops over all class probabilities to see from which class the next instance should be from while(p > 0.0){ iClass++; p -= probPerClass[iClass]; } // keeps on creating and storing instances until we have an instance for the desired class while(instancesBuffer[iClass].size() == 0){ Example<Instance> inst = originalStream.nextInstance(); instancesBuffer[(int) inst.getData().classValue()].add(inst.getData()); } // retrieves the instance from the desired class Instance instance = instancesBuffer[iClass].get(0); // and also removes it from the buffer instancesBuffer[iClass].delete(0); return new InstanceExample(instance); }