@Override public void addPrediction(Prediction prediction, Prediction trueClass, double weight) { int numOutputs=prediction.numOutputAttributes(); if (!hasStarted){ sumSquaredError=new double[numOutputs]; hasStarted=true; for(int i=0; i<numOutputs;i++) if(prediction.hasVotesForAttribute(i)) ++numLearnedOutputs; hasStarted=true; } for(int i=0; i<numOutputs;i++){ if(prediction.hasVotesForAttribute(i)){ double errorOutput=prediction.getVote(i, 0)-trueClass.getVote(i, 0); sumSquaredError[i]=errorOutput*errorOutput*weight+fadingErrorFactor*sumSquaredError[i]; } } weightSeen=weight+fadingErrorFactor*weightSeen; }
@Override public Prediction targetPredictionToSource(Prediction targetPrediction) { Prediction sourcePrediction=new MultiLabelPrediction(this.numSourceInstancesOutputs); for (int i=0; i<targetPrediction.numOutputAttributes();i++){ sourcePrediction.setVotes(this.targetOutputIndices[i], targetPrediction.getVotes(i)); } return sourcePrediction; }
@Override public Prediction computeWeightedVote() { int n=votes.size(); if (n>0){ int numOutputs=outputAttributesCount.length; weights=new double[n][numOutputs]; weightedVote=new MultiLabelPrediction(numOutputs); //For each output attribute for (int o=0;o<numOutputs;o++) { //For each vote for (int i=0; i<n; i++) { int numClasses=votes.get(i).numClasses(o); if(votes.get(i).hasVotesForAttribute(o)) weights[i][o]=1.0/outputAttributesCount[o]; //else takes value 0 //For each class for(int j=0; j<numClasses; j++){ weightedVote.setVote(o, j, weightedVote.getVote(o, j)+votes.get(i).getVote(o, j)*weights[i][o]); } } } } return weightedVote; }
protected double[] defaultRuleErrors(Prediction vote) { double [] errors=new double[vote.numOutputAttributes()]; for(int i=0; i<vote.numOutputAttributes(); i++){ if(vote.hasVotesForAttribute(i)) errors[i]=Double.MAX_VALUE; } return errors; }
public double [] sumVoteDistrib() { int numOutputs=vote.numOutputAttributes(); double [] sum= new double[numOutputs]; for (int j=0; j<numOutputs;j++){ for (int i=0; i<vote.numClasses(j); ++i) sum[j]+=vote.getVote(j, i); } return sum; }
if(errors==null) //if errors==null, rule has seen no predictions since expansion: return maximum error, since prediction is not reliable errors=defaultRuleErrors(vote); debug("Rule No"+ rule.getRuleNumberID() + " Vote: " + vote.toString() + " Error: " + errors + " Y: " + instance.classValue(),3); //predictionValueForThisRule); errorWeightedVote.addVote(vote,errors); System.out.print("Rule " + rule.getRuleNumberID()+ ": "); for (int i=0; i< instance.numOutputAttributes() ; i++){ System.out.print(" " + vote.getVotes(i)[0]); defaultErrors=defaultRuleErrors(defaultVote); double [] fixErrors=new double[vote.numOutputAttributes()]; Prediction fixVote= new MultiLabelPrediction(vote.numOutputAttributes()); for (int i=0; i<vote.numOutputAttributes(); i++){ if(!vote.hasVotesForAttribute(i)){ fixVote.setVotes(i, defaultVote.getVotes(i)); fixErrors[i]=defaultErrors[i]; debug("Default Rule Vote " + defaultVote.toString() + "\n Error " + defaultErrors + " Y: " + instance,3);
public void normalize() { double [] sum=sumVoteDistrib(); for (int j=0; j<vote.numOutputAttributes();j++) for (int i=0; i<vote.numClasses(j); ++i) if(sum[j]!=0) vote.setVote(j, i, vote.getVote(j, i)/sum[j]); } }
@Override public Prediction getPredictionForInstance(MultiLabelInstance inst) { ErrorWeightedVoteMultiLabel vote=getVotes(inst); Prediction pred=vote.getPrediction(); if(vote!=null) { if ( (MultiLabelLearner) getPreparedClassOption(this.learnerOption) instanceof moa.classifiers.rules.multilabel.functions.MultiLabelPerceptronClassification ) { for(int i=0; i<pred.size() ; i++){ pred.setVote(i,0, pred.getVote(i,0) < 0.5 ? 1:0 ); } } return pred; } else return null; }
@Override public Prediction computeWeightedVote() { int n=votes.size(); if (n>0){ int numOutputs=outputAttributesCount.length; weights=new double[n][numOutputs]; weightedVote=new MultiLabelPrediction(numOutputs); //For each output attribute for (int o=0;o<numOutputs;o++) { //int numClasses=votes.get(0).numClasses(o); //For each vote for (int i=0; i<n; i++) { if(votes.get(i).hasVotesForAttribute(o)){ //set as weighted vote and set weight to 1 weights[i][o]=1; /*for(int j=0; j<numClasses; j++){ weightedVote.setVote(o, j, votes.get(i).getVote(o, j)); }*/ weightedVote.setVotes(o, votes.get(i).getVotes(o)); break; } } } } return weightedVote; } }
public static Prediction combinePredictions(Prediction[] predictions, Instance inst) { Prediction result = new MultiLabelPrediction(inst.numOutputAttributes()); for (int i = 0; i < predictions.length; i++) { try { Prediction more_votes = predictions[i]; if (more_votes != null) { for (int numOutputAttribute = 0; numOutputAttribute < inst.numOutputAttributes(); numOutputAttribute++) { int length = 0; if (more_votes.getVotes(numOutputAttribute) != null) length = more_votes.getVotes(numOutputAttribute).length; for (int numValueAttribute = 0; numValueAttribute < length; numValueAttribute++) { result.setVote(numOutputAttribute, numValueAttribute, (result.getVote(numOutputAttribute, numValueAttribute) + more_votes.getVote(numOutputAttribute, numValueAttribute) / (double) predictions.length)); } } } } catch (NullPointerException e) { System.err.println("NullPointer"); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("OutofBounds"); } } return result; }
for (int j = 0; j < y.numOutputAttributes(); j++) { System.out.print( (int)x.classValue(j)); for (int j = 0; j < y.numOutputAttributes(); j++) { System.out.print( (y.getVote(j,0) > t) ? 1 : 0); System.err.print("[WARNING] Prediction is null! (Ignoring this prediction)"); else if (y.numOutputAttributes() < x.numOutputAttributes()) { System.err.println("[WARNING] Only "+y.numOutputAttributes()+" labels found! (Expecting "+x.numOutputAttributes()+")\n (Ignoring this prediction)"); for (int j = 0; j < y.numOutputAttributes(); j++) { int yp = (y.getVote(j,0) > t) ? 1 : 0; correct += ((int)x.classValue(j) == yp) ? 1 : 0;
protected double [] getNormalizedErrors(Prediction prediction, Instance instance) { double [] errors= new double[outputsToLearn.length]; for (int i=0; i<outputsToLearn.length;i++){ double predY=normalizeOutputValue(i,prediction.getVote(outputsToLearn[i], 0)); double trueY=normalizeOutputValue(i,instance.valueOutputAttribute(outputsToLearn[i])); errors[i]=Math.abs(predY-trueY); } return errors; }
@Override public void addResult(Example<Instance> example, double[] classVotes) { Prediction p=new MultiLabelPrediction(1); p.setVotes(classVotes); addResult(example, p); }
@Override public double[] getVotesForInstance(Instance inst) { Prediction pred = getPredictionForInstance(inst); if (pred != null) { return pred.getVotes(); } else { return new double[]{0}; //for compatibility with single target code } }
@Override public Prediction getPredictionForInstance(MultiLabelInstance inst) { int numOutputs=inst.numOutputAttributes(); Prediction p=new MultiLabelPrediction(numOutputs); if(countVector!=null){ for (int i=0; i<numOutputs; i++){ double frac=countVector[i]/numInstances; p.setVote(i, 1, frac); p.setVote(i, 0, 1-frac); } } return p; }
if(errors==null) //if errors==null, rule has seen no predictions since expansion: return maximum error, since prediction is not reliable errors=defaultRuleErrors(vote); debug("Rule No"+ rule.getRuleNumberID() + " Vote: " + vote.toString() + " Error: " + errors + " Y: " + instance.classValue(),3); //predictionValueForThisRule); errorWeightedVote.addVote(vote,errors); if(defaultErrors==null) defaultErrors=defaultRuleErrors(defaultVote); double [] fixErrors=new double[vote.numOutputAttributes()]; Prediction fixVote= new MultiLabelPrediction(vote.numOutputAttributes()); for (int i=0; i<vote.numOutputAttributes(); i++){ if(!vote.hasVotesForAttribute(i)){ fixVote.setVotes(i, defaultVote.getVotes(i)); fixErrors[i]=defaultErrors[i]; debug("Default Rule Vote " + defaultVote.toString() + "\n Error " + defaultErrors + " Y: " + instance,3);
protected double[] defaultRuleErrors(Prediction vote) { double [] errors=new double[vote.numOutputAttributes()]; for(int i=0; i<vote.numOutputAttributes(); i++){ if(vote.hasVotesForAttribute(i)) errors[i]=Double.MAX_VALUE; } return errors; }
@Override public void addResult(Example<Instance> example, Prediction prediction) { MultiLabelInstance inst = (MultiLabelInstance) example.getData(); if (numberOutputs == 0) { numberOutputs = inst.numberOutputTargets(); } if (inst.weight() > 0.0) { this.weightObserved += inst.weight(); if (prediction != null ) { for (int i = 0; i< numberOutputs;i++){ double err = inst.classValue(i) - ((prediction.numOutputAttributes()==0) ? 0.0 : prediction.getVote(i,0)); this.squareError += (err) * (err); this.averageError += Math.abs(err); } } //System.out.println(inst.classValue()+", "+prediction); } }
protected double [] getNormalizedErrors(Prediction prediction, Instance instance) { double [] errors= new double[outputsToLearn.length]; for (int i=0; i<outputsToLearn.length;i++){ double predY=normalizeOutputValue(i,prediction.getVote(outputsToLearn[i], 0)); double trueY=normalizeOutputValue(i,instance.valueOutputAttribute(outputsToLearn[i])); errors[i]=Math.abs(predY-trueY); } return errors; }
@Override public Prediction getPredictionForInstance(Instance inst){ Prediction prediction= new MultiLabelPrediction(1); prediction.setVotes(getVotesForInstance(inst)); return prediction; }