@Override public int numberOutputTargets() { return this.instanceHeader.numOutputAttributes(); }
/** * Output the prediction made by this perceptron on the given instance */ public double[] prediction(double[] instanceValues) { double[] out = new double[tree.getModelContext().numOutputAttributes()]; for (int i = 0; i < tree.getModelContext().numOutputAttributes(); i++) { out[i] = 0; for (int j = 0; j < instanceValues.length; j++) { out[i] += weights[i][j] * instanceValues[j]; } } return out; }
public void initializeWeights() { instancesSeen = 0; int numTargets = tree.getModelContext().numOutputAttributes(); int numInputs = tree.getModelContext().numInputAttributes(); weights = new double[numTargets][numInputs+1]; tree.classifierRandom.setSeed(1234); for (int i = 0; i < numTargets; i++) { for (int j = 0; j < numInputs + 1; j++) { // The last index corresponds to the constant b weights[i][j] = 2 * tree.classifierRandom.nextDouble() - 1; } } }
public double[] getNormalizedError(MultiLabelInstance inst, double[] prediction) { double[] normalPrediction = normalizeTargetVector(prediction); double[] normalValue = normalizedTargetVector(inst); double[] out = new double[getModelContext().numOutputAttributes()]; for (int i = 0; i < inst.numOutputAttributes(); i++) { out[i] = Math.abs(normalValue[i] - normalPrediction[i]); } return out; }
public Prediction getPredictionForInstance(MultiLabelInstance inst) { checkRoot(); double[] predictionVector = treeRoot.getPrediction(inst); MultiLabelPrediction prediction = new MultiLabelPrediction(getModelContext().numOutputAttributes()); for (int i = 0; i < getModelContext().numOutputAttributes(); i++) { prediction.setVote(i, 1, predictionVector[i]); } return prediction; }
public void getModelDescription(StringBuilder out, int indent) { for (int i = 0; i < tree.getModelContext().numOutputAttributes(); i++) { StringUtils.appendIndented(out, indent, " [" + tree.getModelContext().outputAttribute(i).name() + "]"); if (getModelContext() != null) { for (int j = 0; j < getModelContext().numOutputAttributes(); j++) { if (getModelContext().attribute(j).isNumeric()) { out.append((j == 0 || weights[i][j] < 0) ? " " : " + "); out.append(String.format("%.4f", weights[i][j])); out.append(" * "); out.append(getAttributeNameString(j)); } } out.append(" + " + weights[i][getModelContext().numOutputAttributes()]); } StringUtils.appendNewline(out); } } }
public InstanceAttributesSelector(InstancesHeader sourceInstances, int [] targetInputIndices, int [] targetOutputIndices){ this.targetInputIndices=targetInputIndices; this.targetOutputIndices=targetOutputIndices; this.numSourceInstancesOutputs=sourceInstances.numOutputAttributes(); int totAttributes=this.targetInputIndices.length+this.targetOutputIndices.length; targetInstances= new InstancesHeader(); List<Attribute> v = new ArrayList<Attribute>(totAttributes); List<Integer> indexValues = new ArrayList<Integer>(totAttributes); int ct=0; for (int i=0; i<this.targetInputIndices.length;i++) { v.add(sourceInstances.inputAttribute(this.targetInputIndices[i])); indexValues.add(ct); ct++; } for (int i=0; i<this.targetOutputIndices.length;i++) { v.add(sourceInstances.outputAttribute(this.targetOutputIndices[i])); indexValues.add(ct); ct++; } targetInstances.setAttributes(v,indexValues); Range r= new Range("-" + targetOutputIndices.length); r.setUpper(totAttributes); targetInstances.setRangeOutputIndices(r); }
public InstanceOutputAttributesSelector(InstancesHeader sourceInstances, int [] targetOutputIndices){ this.targetOutputIndices=targetOutputIndices; this.numSourceInstancesOutputs=sourceInstances.numOutputAttributes(); int totAttributes=sourceInstances.numInputAttributes()+this.targetOutputIndices.length; targetInstances= new InstancesHeader(); List<Attribute> v = new ArrayList<Attribute>(totAttributes); List<Integer> indexValues = new ArrayList<Integer>(totAttributes); int numInputs=sourceInstances.numInputAttributes(); for (int i=0; i<numInputs;i++) { v.add(sourceInstances.inputAttribute(i)); indexValues.add(i); } for (int i=0; i<this.targetOutputIndices.length;i++) { v.add(sourceInstances.outputAttribute(this.targetOutputIndices[i])); indexValues.add(numInputs+i); } targetInstances.setAttributes(v,indexValues); Range r= new Range("-" + targetOutputIndices.length); r.setUpper(totAttributes); targetInstances.setRangeOutputIndices(r); }
public void restartChangeDetection() { if (this.alternateTree == null) { changeDetection = true; PHsums = new DoubleVector(); PHmins = new DoubleVector(); for (int i = 0; i < tree.getModelContext().numOutputAttributes(); i++) { PHmins.setValue(i, Double.MAX_VALUE); } for (Node child : children) child.restartChangeDetection(); } }
/** * Return the best split suggestions for this node using the given split criteria */ public AttributeExpansionSuggestion[] getBestSplitSuggestions(MultiLabelSplitCriterion criterion) { List<AttributeExpansionSuggestion> bestSuggestions = new LinkedList<AttributeExpansionSuggestion>(); for (int i = 0; i < attributeObservers.size(); i++) { AttributeStatisticsObserver obs = attributeObservers.get(i); if (obs != null) { DoubleVector[] preSplitStatistics = new DoubleVector[tree.getModelContext().numOutputAttributes()]; for (int j = 0; j < tree.getModelContext().numOutputAttributes(); j++) { preSplitStatistics[j] = new DoubleVector(); preSplitStatistics[j].setValue(0, examplesSeen); preSplitStatistics[j].setValue(1, sumOfValues.getValue(j)); preSplitStatistics[j].setValue(2, sumOfSquares.getValue(j)); } AttributeExpansionSuggestion bestSuggestion = null; bestSuggestion = obs.getBestEvaluatedSplitSuggestion(criterion, preSplitStatistics, i); if (bestSuggestion != null) { bestSuggestions.add(bestSuggestion); } } } return bestSuggestions.toArray(new AttributeExpansionSuggestion[bestSuggestions.size()]); }
private double[] denormalizePrediction(double[] normalizedPrediction) { double[] out = new double[normalizedPrediction.length]; if (tree.normalize()) { for (int i = 0; i < tree.getModelContext().numOutputAttributes(); i++) { double mean = tree.sumOfValues.getValue(i) / tree.examplesSeen; double sd = computeSD(tree.sumOfSquares.getValue(i), tree.sumOfValues.getValue(i), tree.examplesSeen); if (examplesSeen > 1) out[i] = normalizedPrediction[i] * sd + mean; else out[i] = 0; } return out; } else return normalizedPrediction; }
public double[] normalizedTargetVector(MultiLabelInstance inst) { double[] out = new double[getModelContext().numOutputAttributes()]; if (normalize()) { for (int i = 0; i < inst.numOutputAttributes(); i++) { double value = inst.valueOutputAttribute(i); double sd = computeSD(sumOfSquares.getValue(i), sumOfValues.getValue(i), examplesSeen); double average = sumOfValues.getValue(i) / examplesSeen; if (sd > 0 && examplesSeen > 1) out[i] = (value - average) / (sd); else out[i] = 0; } } else { for (int i = 0; i < inst.numOutputAttributes(); i++) { out[i] = inst.valueOutputAttribute(i); } } return out; }
for (int i = 0; i < tree.getModelContext().numOutputAttributes(); i++) {