/** * Adds the. * * @param inst the inst */ public void add(Instance inst) { this.instances.add(inst.copy()); }
public void add(Instance inst) { this.instances.add(inst.copy()); }
@Override public Example copy() { return new InstanceExample(instance.copy()); }
@Override public void trainOnInstanceImpl(Instance inst) { double zt = 0.0; double weight = 1.0; for (int i = 0; i < this.ensemble.length; i++) { zt += (this.ensemble[i].correctlyClassifies(inst) ? 1 : -1) - theta; //normalized_predict(ex.x) * ex.y - theta; Instance weightedInst = (Instance) inst.copy(); weightedInst.setWeight(weight); this.ensemble[i].trainOnInstance(weightedInst); weight = (zt<=0)? 1.0 : Math.pow(1.0-gamma, zt/2.0); } }
/** * Returns the distance between a point and an hyperrectangle. * * @param node The current node from whose hyperrectangle * the distance is to be measured. * @param x the point * @return the distance * @throws Exception If some problem occurs in determining * the distance to the hyperrectangle. */ protected double distanceToHrect(KDTreeNode node, Instance x) throws Exception { double distance = 0.0; Instance closestPoint = (Instance)x.copy(); boolean inside; inside = clipToInsideHrect(node, closestPoint); if (!inside) distance = m_EuclideanDistance.distance(closestPoint, x); return distance; }
@Override public InstanceExample nextInstance() { Instance inst = (Instance) ((Instance) this.inputStream.nextInstance().getData()).copy(); return new InstanceExample(filterInstance(inst)); }
@Override public void trainOnInstanceImpl(Instance inst) { 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); } } }
public Instance formatInstance(Instance original) { //Copy the original instance Instance converted = (Instance) original.copy(); converted.setDataset(null); //Delete all class attributes for (int j = 0; j < m_L; j++) { converted.deleteAttributeAt(0); } //Add one of those class attributes at the begginning converted.insertAttributeAt(0); //Hopefully setting the dataset will configure that attribute properly converted.setDataset(m_InstancesTemplate); return converted; }
@Override public Example<Instance> nextInstance() { if(waitingToSend==0){ do { queuedInstance=originalStream.nextInstance(); waitingToSend= MiscUtils.poisson(1.0, this.randomGenerator);; } while (waitingToSend==0); } Example<Instance> instance=new InstanceExample(queuedInstance.getData().copy()); instance.setWeight(queuedInstance.weight()); waitingToSend--; return instance; }
/** * 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]; } } } } }
public double[] getVotesForInstanceBinary(Instance inst) { double combinedVote[] = new double[(int) inst.numClasses()]; Instance weightedInst = (Instance) inst.copy(); if (this.initMatrixCodes == false) { for (int i = 0; i < this.ensemble.length; i++) { //Replace class by OC weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]); double vote[]; vote = this.ensemble[i].getVotesForInstance(weightedInst); //Binary Case int voteClass = 0; if (vote.length == 2) { voteClass = (vote[1] > vote[0] ? 1 : 0); } //Update votes for (int j = 0; j < inst.numClasses(); j++) { if (this.matrixCodes[i][j] == voteClass) { combinedVote[j] += 1; } } } } return combinedVote; }
@Override public void trainOnInstanceImpl(Instance inst) { double lambda_d = 1.0; for (int i = 0; i < this.ensemble.length; i++) { double k = this.pureBoostOption.isSet() ? lambda_d : MiscUtils.poisson(lambda_d, this.classifierRandom); if (k > 0.0) { Instance weightedInst = (Instance) inst.copy(); weightedInst.setWeight(inst.weight() * k); this.ensemble[i].trainOnInstance(weightedInst); } if (this.ensemble[i].correctlyClassifies(inst)) { this.scms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.scms[i]); } else { this.swms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.swms[i]); } } }
@Override public void trainOnInstanceImpl(Instance inst) { int trueClass = (int) inst.classValue(); 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); if (Utils.maxIndex(this.ensemble[i].getVotesForInstance(inst)) == trueClass) { this.error[i] += alpha * (0.0 - this.error[i]); //EWMA } else { this.error[i] += alpha * (1.0 - this.error[i]); //EWMA } this.ensemble[i].trainOnInstance(weightedInst); } } }
public double[] getVotesForInstanceBinary(Instance inst) { double combinedVote[] = new double[(int) inst.numClasses()]; Instance weightedInst = (Instance) inst.copy(); if (this.initMatrixCodes == false) { for (int i = 0; i < this.ensemble.length; i++) { //Replace class by OC weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]); double vote[]; vote = this.ensemble[i].getVotesForInstance(weightedInst); //Binary Case int voteClass = 0; if (vote.length == 2) { voteClass = (vote[1] > vote[0] ? 1 : 0); } //Update votes for (int j = 0; j < inst.numClasses(); j++) { if (this.matrixCodes[i][j] == voteClass) { combinedVote[j] += getEnsembleMemberWeight(i); } } } } return combinedVote; }
@Override public void trainOnInstanceImpl(Instance instance) { double factor=this.fadingErrorFactorOption.getValue(); for (int i = 0; i < this.ensemble.length; i++) { Instance inst=instance.copy(); int k = 1; if ( this.useBaggingOption.isSet()) { k = MiscUtils.poisson(1.0, this.classifierRandom); } if (k > 0) { //Instance weightedInst = transformInstance(inst,i); inst.setWeight(inst.weight() * k); //estimate error double error = Math.abs(inst.classValue()-ensemble[i].getVotesForInstance(inst)[0]); sumError[i]=error*inst.weight()+sumError[i]*factor; nError[i]=inst.weight()+nError[i]*factor; //train learner this.ensemble[i].trainOnInstance(inst); } } }
/** * Train. * * @param inEvent the in event */ protected void train(InstanceContentEvent inEvent) { Instance trainInst = inEvent.getInstance(); for (int i = 0; i < sizeEnsemble; i++) { int k = MiscUtils.poisson(1.0, this.random); if (k > 0) { Instance weightedInst = trainInst.copy(); weightedInst.setWeight(trainInst.weight() * k); InstanceContentEvent instanceContentEvent = new InstanceContentEvent( inEvent.getInstanceIndex(), weightedInst, true, false); instanceContentEvent.setClassifierIndex(i); instanceContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); trainingStream.put(instanceContentEvent); } } }
/** * Train. * * @param inEvent the in event */ protected void train(InstanceContentEvent inEvent) { Instance trainInst = inEvent.getInstance(); for (int i = 0; i < sizeEnsemble; i++) { int k = MiscUtils.poisson(1.0, this.random); if (k > 0) { Instance weightedInst = (Instance) trainInst.copy(); weightedInst.setWeight(trainInst.weight() * k); boolean isTraining = true; InstanceContentEvent instanceContentEvent = new InstanceContentEvent( inEvent.getInstanceIndex(), weightedInst, isTraining, !isTraining); instanceContentEvent.setClassifierIndex(i); instanceContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); trainingStream.put(instanceContentEvent); } } }
private void computeBoosting(ResultContentEvent inEvent, int instanceIndex) { // Starts code for Boosting //Send instances to train double lambda_d = 1.0; for (int i = 0; i < this.ensembleSize; i++) { double k = lambda_d; Instance inst = inEvent.getInstance(); if (k > 0.0) { Instance weightedInst = inst.copy(); weightedInst.setWeight(inst.weight() * k); //this.ensemble[i].trainOnInstance(weightedInst); InstanceContentEvent instanceContentEvent = new InstanceContentEvent( inEvent.getInstanceIndex(), weightedInst, true, false); instanceContentEvent.setClassifierIndex(i); instanceContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); trainingStream.put(instanceContentEvent); } if (this.correctlyClassifies(i, inst, instanceIndex)){ this.scms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.scms[i]); } else { this.swms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.swms[i]); } } }
private void computeBoosting(ResultContentEvent inEvent, int instanceIndex) { // Starts code for Boosting //Send instances to train double lambda_d = 1.0; for (int i = 0; i < this.ensembleSize; i++) { double k = true ? lambda_d : MiscUtils.poisson(lambda_d, this.random); Instance inst = inEvent.getInstance(); if (k > 0.0) { Instance weightedInst = (Instance) inst.copy(); weightedInst.setWeight(inst.weight() * k); //this.ensemble[i].trainOnInstance(weightedInst); boolean isTraining = true; boolean isTesting = false; InstanceContentEvent instanceContentEvent = new InstanceContentEvent( inEvent.getInstanceIndex(), weightedInst, isTraining, isTesting); instanceContentEvent.setClassifierIndex(i); instanceContentEvent.setEvaluationIndex(inEvent.getEvaluationIndex()); trainingStream.put(instanceContentEvent); } if (this.correctlyClassifies(i, inst, instanceIndex) == true){ this.scms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.scms[i]); } else { this.swms[i] += lambda_d; lambda_d *= this.trainingWeightSeenByModel / (2 * this.swms[i]); } } }
@Override public double[] getVotesForInstance(Instance instance) { Instance testInstance = instance.copy(); if(this.ensemble == null) initEnsemble(testInstance); DoubleVector combinedVote = new DoubleVector(); for(int i = 0 ; i < this.ensemble.length ; ++i) { DoubleVector vote = new DoubleVector(this.ensemble[i].getVotesForInstance(testInstance)); if (vote.sumOfValues() > 0.0) { vote.normalize(); double acc = this.ensemble[i].evaluator.getPerformanceMeasurements()[1].getValue(); if(! this.disableWeightedVote.isSet() && acc > 0.0) { for(int v = 0 ; v < vote.numValues() ; ++v) { vote.setValue(v, vote.getValue(v) * acc); } } combinedVote.addValues(vote); } } return combinedVote.getArrayRef(); }