/** * Copy. * * @return the instance */ @Override public Instance copy() { InstanceImpl inst = new InstanceImpl(this); return inst; }
/** * Text representation of a InstanceImpl. */ @Override public String toString() { StringBuilder str = new StringBuilder(); for (int attIndex = 0; attIndex < this.numAttributes(); attIndex++) { if (!this.isMissing(attIndex)) { if (this.attribute(attIndex).isNominal()) { int valueIndex = (int) this.value(attIndex); String stringValue = this.attribute(attIndex).value(valueIndex); str.append(stringValue).append(","); } else if (this.attribute(attIndex).isNumeric()) { str.append(this.value(attIndex)).append(","); } else if (this.attribute(attIndex).isDate()) { SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); str.append(dateFormatter.format(this.value(attIndex))).append(","); } } else { str.append("?,"); } } return str.toString(); }
/** * Sets the class value. * * @param d the new class value */ @Override public void setClassValue(double d) { this.setValue(classIndex(), d); }
@Override public void addResult(Example<Instance> exampleInstance, double[] classVotes) { InstanceImpl inst = (InstanceImpl) exampleInstance.getData(); double weight = inst.weight(); if (inst.classIsMissing() == false) { int trueClass = (int) inst.classValue(); if (weight > 0.0) { // // initialize evaluator if (totalObservedInstances == 0) { reset(inst.dataset().numClasses()); } this.totalObservedInstances += 1; //// if classVotes has length == 1, then the negative (0) class //// got all the votes Double normalizedVote = 0.0; //// normalize and add score if (classVotes.length == 2) { normalizedVote = classVotes[1] / (classVotes[0] + classVotes[1]); } if (normalizedVote.isNaN()) { normalizedVote = 0.0; } this.aucEstimator.add(normalizedVote, trueClass == 1, Utils.maxIndex(classVotes) == trueClass); this.weightMajorityClassifier .add((this.aucEstimator.getRatio() <= 1 ? 0 : 1) == trueClass ? weight : 0); } } }
/** * Class is missing. * * @return true, if successful */ @Override public boolean classIsMissing() { return this.instanceData.isMissing(classIndex()); }
@Override public int numOutputAttributes() { return numberOutputTargets(); }
@Override public boolean isMissing(Attribute attribute) { int index = this.instanceHeader.indexOf(attribute); return this.isMissing(index); }
@Override public void setMissing(Attribute attribute) { int index = this.instanceHeader.indexOf(attribute); this.setMissing(index); }
@Override public void addResult(Example<Instance> exampleInstance, double[] classVotes) { InstanceImpl inst = (InstanceImpl)exampleInstance.getData(); double weight = inst.weight(); if (inst.classIsMissing() == false){ int trueClass = (int) inst.classValue(); reset(inst.dataset().numClasses());
/** * Class attribute. * * @return the attribute */ @Override public Attribute classAttribute() { return this.instanceHeader.attribute(classIndex()); }
Instance inst = new InstanceImpl(1, attributes); inst.setDataset(this.ltm); this.ltm.add(inst);
/** * Class value. * * @return the double */ @Override public double classValue() { return this.instanceData.value(classIndex()); }
@Override public Instance sourceInstanceToTarget(Instance sourceInstance) { double [] attValues = new double[targetInstances.numAttributes()]; Instance newInstance=new InstanceImpl(sourceInstance.weight(),attValues); for (int i=0; i<this.targetInputIndices.length; i++){ newInstance.setValue(i, sourceInstance.valueInputAttribute(targetInputIndices[i])); } for (int i=0; i<this.targetOutputIndices.length; i++){ newInstance.setValue(i, sourceInstance.valueOutputAttribute(targetOutputIndices[i])); } newInstance.setDataset(targetInstances); return newInstance; }
@Override public Instance sourceInstanceToTarget(Instance sourceInstance) { double [] attValues = new double[targetInstances.numAttributes()]; Instance newInstance=new InstanceImpl(sourceInstance.weight(),attValues); int numInputs=this.targetInstances.numInputAttributes(); for (int i=0; i<numInputs; i++){ newInstance.setValue(i, sourceInstance.valueInputAttribute(i)); } for (int i=0; i<this.targetOutputIndices.length; i++){ newInstance.setValue(numInputs+i, sourceInstance.valueOutputAttribute(targetOutputIndices[i])); } newInstance.setDataset(targetInstances); return newInstance; }
/** * Filter an instance. * Assume that the instance has a single class label, as the final attribute. Note that this may not always be the case! * * @param x input instance * @return output instance */ public Instance filterInstance(Instance x) { if(dataset==null){ initialize(x); } double z_[] = new double[H+1]; int d = x.numAttributes() - 1; // suppose one class attribute (at the end) for(int k = 0; k < H; k++) { // for each hidden unit ... double a_k = 0.; // k-th activation (dot product) for(int j = 0; j < d; j++) { a_k += (x.value(j) * W[k][j]); } z_[k] = (a_k > 0. ? a_k : 0.); // <------- can change threshold here } z_[H] = x.classValue(); Instance z = new InstanceImpl(x.weight(),z_); z.setDataset(dataset); return z; }
@Override public InstanceExample nextInstance() { Instance x = (Instance) ((Instance) this.inputStream.nextInstance().getData()); if(dataset==null){ System.out.println("INIT. "); initialize(x); } double z_[] = new double[dataset.numAttributes()]; Instance z = new InstanceImpl(x.weight(),z_); int d = x.numAttributes(); int h = numLatentOption.getValue(); int j_c = x.classIndex(); for(int k = 0; k < h; k++) { double sum_k = 0.; for(int j = 0; j < d; j++) { sum_k += (x.value(j) - c[k]); } double v = sum_k / Math.pow(r[k],2); z.setValue(k,Math.exp(-sum_k)); } z.setValue(h,x.classValue()); z.setDataset(dataset); return new InstanceExample(z); }
private Instance processInstance(Instance instance) { double [] attValues = new double[dataset.numAttributes()]; Instance newInstance=new InstanceImpl(instance.weight(),attValues); int count=0; for (int i=0; i<inputsSelected.numEntries(); i++){ int start=inputsSelected.getStart(i)-1; int end=inputsSelected.getEnd(i)-1; for (int j=start; j<=end; j++){ newInstance.setValue(count, instance.value(j)); count++; } } for (int i=0; i<outputsSelected.numEntries(); i++){ int start=outputsSelected.getStart(i)-1; int end=outputsSelected.getEnd(i)-1; for (int j=start; j<=end; j++){ newInstance.setValue(count, instance.value(j)); count++; } } newInstance.setDataset(dataset); return newInstance; }