Java Code Examples for weka.core.Instance#copy()

The following examples show how to use weka.core.Instance#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BRUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateClassifier(Instance x) throws Exception {

	int L = x.classIndex();

	if(getDebug()) System.out.print("-: Updating "+L+" models");

	for(int j = 0; j < L; j++) {
		Instance x_j = (Instance)x.copy();
		x_j.setDataset(null);
		x_j = MLUtils.keepAttributesAt(x_j,new int[]{j},L);
		x_j.setDataset(m_InstancesTemplates[j]);
		((UpdateableClassifier)m_MultiClassifiers[j]).updateClassifier(x_j);
	}

	if(getDebug()) System.out.println(":- ");
}
 
Example 2
Source File: Add.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Input an instance for filtering. Ordinarily the instance is processed
 * and made available for output immediately. Some filters require all
 * instances be read before producing output.
 *
 * @param instance the input instance
 * @return true if the filtered instance may now be
 * collected with output().
 * @throws IllegalStateException if no input format has been defined.
 */
public boolean input(Instance instance) {

  if (getInputFormat() == null) {
    throw new IllegalStateException("No input instance format defined");
  }
  if (m_NewBatch) {
    resetQueue();
    m_NewBatch = false;
  }

  Instance inst = (Instance)instance.copy();

  // First copy string values from input to output
  copyValues(inst, true, inst.dataset(), getOutputFormat());
  
  // Insert the new attribute and reassign to output
  inst.setDataset(null);
  inst.insertAttributeAt(m_Insert.getIndex());
  inst.setDataset(getOutputFormat());
  push(inst);
  return true;
}
 
Example 3
Source File: DatasetLoader.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
public static Instances loadAngerMetaClassification() throws Exception {
  final Instances data = DatasetLoader
      .loadArff("src/test/resources/numeric/anger.meta.arff");
  ArrayList<Attribute> atts = new ArrayList<>();
  atts.add(data.attribute(0));
  Attribute cls = new Attribute("cls", Arrays.asList("0", "1"));
  atts.add(cls);
  Instances dataDiscretized = new Instances("anger-classification", atts, data.numInstances());
  dataDiscretized.setClassIndex(1);
  for (Instance datum : data) {
    Instance cpy = (Instance) datum.copy();
    cpy.setDataset(dataDiscretized);
    cpy.setValue(0, datum.stringValue(0));
    cpy.setValue(1, datum.classValue() > 0.5 ? "1" : "0");
    dataDiscretized.add(cpy);
  }
  return dataDiscretized;
}
 
Example 4
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instances mergeClassesOfInstances(final Instances data, final List<Set<String>> instancesCluster) {
	List<String> classes = new LinkedList<>();
	IntStream.range(0, instancesCluster.size()).forEach(x -> classes.add("C" + ((double) x)));

	Instances newData = WekaUtil.getEmptySetOfInstancesWithRefactoredClass(data, classes);

	for (Instance i : data) {
		Instance iNew = (Instance) i.copy();
		String className = i.classAttribute().value((int) Math.round(i.classValue()));
		for (Set<String> cluster : instancesCluster) {
			if (cluster.contains(className)) {
				iNew.setClassValue(instancesCluster.indexOf(cluster));
				iNew.setDataset(newData);
				newData.add(iNew);
			}
		}
	}
	return newData;
}
 
Example 5
Source File: RT.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * ConvertInstance - Convert an Instance to multi-class format by deleting all but one of the label attributes.
 * @param	x	incoming Instance
 * @return	the converted Instance
 */
public Instance convertInstance(Instance x) {

	int L = x.classIndex();

	//Copy the original instance
	Instance x_ = (Instance) x.copy(); 
	x_.setDataset(null);

	//Delete all class attributes
	for (int i = 0; i < L; i++)
		x_.deleteAttributeAt(0);

	//Add one of those class attributes at the begginning
	x_.insertAttributeAt(0);

	//Hopefully setting the dataset will configure that attribute properly
	x_.setDataset(m_InstancesTemplate);

	return x_;
}
 
Example 6
Source File: CR.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public double[] distributionForInstance(Instance x) throws Exception {

	int L = x.classIndex(); 

	double y[] = new double[L*2];

	for (int j = 0; j < L; j++) {
		Instance x_j = (Instance)x.copy();
		x_j.setDataset(null);
		x_j = MLUtils.keepAttributesAt(x_j,new int[]{j},L);
		x_j.setDataset(m_Templates[j]);
		double w[] = m_MultiClassifiers[j].distributionForInstance(x_j); // e.g. [0.1, 0.8, 0.1]
		y[j] = Utils.maxIndex(w);									     // e.g. 1
		y[L+j] = w[(int)y[j]];											 // e.g. 0.8
	}

	return y;
}
 
Example 7
Source File: MultiClassClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Returns the individual predictions of the base classifiers
  * for an instance. Used by StackedMultiClassClassifier.
  * Returns the probability for the second "class" predicted
  * by each base classifier.
  *
  * @param inst the instance to get the prediction for
  * @return the individual predictions
  * @throws Exception if the predictions can't be computed successfully
  */
 public double[] individualPredictions(Instance inst) throws Exception {
   
   double[] result = null;

   if (m_Classifiers.length == 1) {
     result = new double[1];
     result[0] = m_Classifiers[0].distributionForInstance(inst)[1];
   } else {
     result = new double[m_ClassFilters.length];
     for(int i = 0; i < m_ClassFilters.length; i++) {
if (m_Classifiers[i] != null) {
  if (m_Method == METHOD_1_AGAINST_1) {    
    Instance tempInst = (Instance)inst.copy(); 
    tempInst.setDataset(m_TwoClassDataset);
    result[i] = m_Classifiers[i].distributionForInstance(tempInst)[1];  
  } else {
    m_ClassFilters[i].input(inst);
    m_ClassFilters[i].batchFinished();
    result[i] = m_Classifiers[i].
      distributionForInstance(m_ClassFilters[i].output())[1];
  }
}
     }
   }
   return result;
 }
 
Example 8
Source File: CCp.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
protected void classify(Instance test) throws Exception {
	// copy
	Instance copy = (Instance)test.copy();
	copy.setDataset(null);

	// delete attributes we don't need
	for(int i = excld.length-1; i >= 0; i--) {
		copy.deleteAttributeAt(this.excld[i]);
	}

	//set template
	copy.setDataset(this._template);

	// round
	for(int k = 0; k < this.j; k++) {
		copy.setValue(j,Math.round(copy.value(k)));
	}

	//set class
	double dist[] = this.classifier.distributionForInstance(copy);
	int max_index = Utils.maxIndex(dist);
	confidences[this.index] = dist[max_index];
	test.setValue(this.index,max_index);

	//carry on
	if (next!=null) next.classify(test);
}
 
Example 9
Source File: ClassOrder.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Input an instance for filtering. Ordinarily the instance is processed
  * and made available for output immediately. Some filters require all
  * instances be read before producing output.
  *
  * @param instance the input instance
  * @return true if the filtered instance may now be
  * collected with output().
  * @throws IllegalStateException if no input format has been defined.
  */
 public boolean input(Instance instance) {

   if (getInputFormat() == null) {
     throw new IllegalStateException("No input instance format defined");
   }
   if (m_NewBatch) {
     resetQueue();
     m_NewBatch = false;     
   }	
   
   // In case some one use this routine in testing, 
   // although he/she should not do so
   if(m_Converter != null){
     Instance datum = (Instance)instance.copy();
     if (!datum.isMissing(m_ClassAttribute)){
datum.setClassValue((double)m_Converter[(int)datum.classValue()]);
     }
     push(datum);
     return true;
   }
   
   if (!instance.isMissing(m_ClassAttribute)) {
     m_ClassCounts[(int)instance.classValue()] += instance.weight();
   }

   bufferInput(instance);
   return false;
 }
 
Example 10
Source File: ReservoirSample.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Decides whether the current instance gets retained in the
 * reservoir.
 *
 * @param instance the Instance to potentially retain
 */
protected void processInstance(Instance instance) {
  if (m_currentInst < m_SampleSize) {
    m_subSample[m_currentInst] = (Instance)instance.copy();
  } else {
    double r = m_random.nextDouble();
    if (r < ((double)m_SampleSize / (double)m_currentInst)) {
      r = m_random.nextDouble();
      int replace = (int)((double)m_SampleSize * r);
      m_subSample[replace] = (Instance)instance.copy();
    }
  }
  m_currentInst++;
}
 
Example 11
Source File: MergeManyValues.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Input an instance for filtering. The instance is processed
  * and made available for output immediately.
  *
  * @param instance 	the input instance
  * @return 		true if the filtered instance may now be
  * 			collected with output().
  * @throws IllegalStateException	if no input format has been set.
  */
 public boolean input(Instance instance) {
   if (getInputFormat() == null) {
     throw new IllegalStateException("No input instance format defined");
   }
   if (m_NewBatch) {
     resetQueue();
     m_NewBatch = false;
   }

   Attribute att = getInputFormat().attribute(m_AttIndex.getIndex());
   FastVector newVals = new FastVector(att.numValues() - 1);
   for (int i = 0; i < att.numValues(); i++) {
     boolean inMergeList = false;

     if(att.value(i).equalsIgnoreCase(m_Label)){
//don't want to add this one.
inMergeList = true;		
     }else{
inMergeList = m_MergeRange.isInRange(i);
     }

     if(!inMergeList){
//add it.
newVals.addElement(att.value(i));
     }
   }
   newVals.addElement(m_Label);

   Attribute temp = new Attribute(att.name(), newVals);

   Instance newInstance = (Instance)instance.copy();    
   if (!newInstance.isMissing(m_AttIndex.getIndex())) {
     String currValue = newInstance.stringValue(m_AttIndex.getIndex());
     if(temp.indexOfValue(currValue) == -1)
newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(m_Label));
     else
newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(currValue));
   }

   push(newInstance);
   return true;
 }
 
Example 12
Source File: RandomTree.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Splits instances into subsets based on the given split.
 * 
 * @param data the data to work with
 * @return the subsets of instances
 * @throws Exception if something goes wrong
 */
protected Instances[] splitData(Instances data) throws Exception {

  // Allocate array of Instances objects
  Instances[] subsets = new Instances[m_Prop.length];
  for (int i = 0; i < m_Prop.length; i++) {
    subsets[i] = new Instances(data, data.numInstances());
  }

  // Go through the data
  for (int i = 0; i < data.numInstances(); i++) {

    // Get instance
    Instance inst = data.instance(i);

    // Does the instance have a missing value?
    if (inst.isMissing(m_Attribute)) {

      // Split instance up
      for (int k = 0; k < m_Prop.length; k++) {
        if (m_Prop[k] > 0) {
          Instance copy = (Instance) inst.copy();
          copy.setWeight(m_Prop[k] * inst.weight());
          subsets[k].add(copy);
        }
      }

      // Proceed to next instance
      continue;
    }

    // Do we have a nominal attribute?
    if (data.attribute(m_Attribute).isNominal()) {
      subsets[(int) inst.value(m_Attribute)].add(inst);

      // Proceed to next instance
      continue;
    }

    // Do we have a numeric attribute?
    if (data.attribute(m_Attribute).isNumeric()) {
      subsets[(inst.value(m_Attribute) < m_SplitPoint) ? 0 : 1].add(inst);

      // Proceed to next instance
      continue;
    }

    // Else throw an exception
    throw new IllegalArgumentException("Unknown attribute type");
  }

  // Save memory
  for (int i = 0; i < m_Prop.length; i++) {
    subsets[i].compactify();
  }

  // Return the subsets
  return subsets;
}
 
Example 13
Source File: PlainText.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Store the prediction made by the classifier as a string.
 * 
 * @param dist        the distribution to use
 * @param inst        the instance to generate text from
 * @param index       the index in the dataset
 * @throws Exception  if something goes wrong
 */
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int width = 7 + m_NumDecimals;
  int prec = m_NumDecimals;
  
  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // index
  append(Utils.padLeft("" + (index+1), 6));

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    if (inst.classIsMissing())
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(inst.classValue(), width, prec));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(predValue, width, prec));
    // error
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.doubleToString(predValue - inst.classValue(), width, prec));
  } else {
    // actual
    append(" " + Utils.padLeft(((int) inst.classValue()+1) + ":" + inst.toString(inst.classIndex()), width));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(" " + Utils.padLeft("?", width));
    else
      append(" " + Utils.padLeft(((int) predValue+1) + ":" + inst.dataset().classAttribute().value((int)predValue), width));
    // error?
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append(" " + "  +  ");
    else
      append(" " + "     ");
    // prediction/distribution
    if (m_OutputDistribution) {
      if (Utils.isMissingValue(predValue)) {
        append(" " + "?");
      }
      else {
        append(" ");
        for (int n = 0; n < dist.length; n++) {
          if (n > 0)
            append(",");
          if (n == (int) predValue)
            append("*");
          append(Utils.doubleToString(dist[n], prec));
        }
      }
    }
    else {
      if (Utils.isMissingValue(predValue))
        append(" " + "?");
      else
        append(" " + Utils.doubleToString(dist[(int)predValue], prec));
    }
  }

  // attributes
  append(" " + attributeValuesString(withMissing) + "\n");
  
}
 
Example 14
Source File: HTML.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int prec = m_NumDecimals;
  
  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // index
  append("<tr>");
  append("<td>" + (index+1) + "</td>");

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    if (inst.classIsMissing())
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(inst.classValue(), prec) + "</td>");
    // predicted
    if (Utils.isMissingValue(predValue))
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(predValue, prec) + "</td>");
    // error
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append("<td align=\"right\">" + "?" + "</td>");
    else
      append("<td align=\"right\">" + Utils.doubleToString(predValue - inst.classValue(), prec) + "</td>");
  } else {
    // actual
    append("<td>" + ((int) inst.classValue()+1) + ":" + sanitize(inst.toString(inst.classIndex())) + "</td>");
    // predicted
    if (Utils.isMissingValue(predValue))
      append("<td>" + "?" + "</td>");
    else
      append("<td>" + ((int) predValue+1) + ":" + sanitize(inst.dataset().classAttribute().value((int)predValue)) + "</td>");
    // error?
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append("<td>" + "+" + "</td>");
    else
      append("<td>" + "&nbsp;" + "</td>");
    // prediction/distribution
    if (m_OutputDistribution) {
      if (Utils.isMissingValue(predValue)) {
        append("<td>" + "?" + "</td>");
      }
      else {
        append("<td align=\"right\">");
        for (int n = 0; n < dist.length; n++) {
          if (n > 0)
            append("</td><td align=\"right\">");
          if (n == (int) predValue)
            append("*");
          append(Utils.doubleToString(dist[n], prec));
        }
        append("</td>");
      }
    }
    else {
      if (Utils.isMissingValue(predValue))
        append("<td align=\"right\">" + "?" + "</td>");
      else
        append("<td align=\"right\">" + Utils.doubleToString(dist[(int)predValue], prec) + "</td>");
    }
  }

  // attributes
  append(attributeValuesString(withMissing) + "</tr>\n");    
}
 
Example 15
Source File: CSV.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Store the prediction made by the classifier as a string.
 * 
 * @param dist        the distribution to use
 * @param inst        the instance to generate text from
 * @param index       the index in the dataset
 * @throws Exception  if something goes wrong
 */
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int prec = m_NumDecimals;

  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // index
  append("" + (index+1));

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    if (inst.classIsMissing())
      append(m_Delimiter + "?");
    else
      append(m_Delimiter + Utils.doubleToString(inst.classValue(), prec));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(m_Delimiter + "?");
    else
      append(m_Delimiter + Utils.doubleToString(predValue, prec));
    // error
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append(m_Delimiter + "?");
    else
      append(m_Delimiter + Utils.doubleToString(predValue - inst.classValue(), prec));
  } else {
    // actual
    append(m_Delimiter + ((int) inst.classValue()+1) + ":" + inst.toString(inst.classIndex()));
    // predicted
    if (Utils.isMissingValue(predValue))
      append(m_Delimiter + "?");
    else
      append(m_Delimiter + ((int) predValue+1) + ":" + inst.dataset().classAttribute().value((int)predValue));
    // error?
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append(m_Delimiter + "+");
    else
      append(m_Delimiter + "");
    // prediction/distribution
    if (m_OutputDistribution) {
      if (Utils.isMissingValue(predValue)) {
        append(m_Delimiter + "?");
      }
      else {
        append(m_Delimiter);
        for (int n = 0; n < dist.length; n++) {
          if (n > 0)
            append(m_Delimiter);
          if (n == (int) predValue)
            append("*");
          append(Utils.doubleToString(dist[n], prec));
        }
      }
    }
    else {
      if (Utils.isMissingValue(predValue))
        append(m_Delimiter + "?");
      else
        append(m_Delimiter + Utils.doubleToString(dist[(int)predValue], prec));
    }
  }

  // attributes
  if (m_Attributes != null)
    append(m_Delimiter + attributeValuesString(withMissing));
  append("\n");
}
 
Example 16
Source File: XML.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Store the prediction made by the classifier as a string.
 * 
 * @param dist        the distribution to use
 * @param inst        the instance to generate text from
 * @param index       the index in the dataset
 * @throws Exception  if something goes wrong
 */
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
  int prec = m_NumDecimals;

  Instance withMissing = (Instance)inst.copy();
  withMissing.setDataset(inst.dataset());
  
  double predValue = 0;
  if (Utils.sum(dist) == 0) {
    predValue = Utils.missingValue();
  } else {
    if (inst.classAttribute().isNominal()) {
      predValue = Utils.maxIndex(dist);
    } else {
      predValue = dist[0];                         
    }
  }
  
  // opening tag
  append("  <" + TAG_PREDICTION + " " + ATT_INDEX + "=\"" + (index+1) + "\">\n");

  if (inst.dataset().classAttribute().isNumeric()) {
    // actual
    append("    <" + TAG_ACTUAL_VALUE + ">");
    if (inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(inst.classValue(), prec));
    append("</" + TAG_ACTUAL_VALUE + ">\n");
    // predicted
    append("    <" + TAG_PREDICTED_VALUE + ">");
    if (inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(predValue, prec));
    append("</" + TAG_PREDICTED_VALUE + ">\n");
    // error
    append("    <" + TAG_ERROR + ">");
    if (Utils.isMissingValue(predValue) || inst.classIsMissing())
      append("?");
    else
      append(Utils.doubleToString(predValue - inst.classValue(), prec));
    append("</" + TAG_ERROR + ">\n");
  } else {
    // actual
    append("    <" + TAG_ACTUAL_LABEL + " " + ATT_INDEX + "=\"" + ((int) inst.classValue()+1) + "\"" + ">");
    append(sanitize(inst.toString(inst.classIndex())));
    append("</" + TAG_ACTUAL_LABEL + ">\n");
    // predicted
    append("    <" + TAG_PREDICTED_LABEL + " " + ATT_INDEX + "=\"" + ((int) predValue+1) + "\"" + ">");
    if (Utils.isMissingValue(predValue))
      append("?");
    else
      append(sanitize(inst.dataset().classAttribute().value((int)predValue)));
    append("</" + TAG_PREDICTED_LABEL + ">\n");
    // error?
    append("    <" + TAG_ERROR + ">");
    if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
      append(VAL_YES);
    else
      append(VAL_NO);
    append("</" + TAG_ERROR + ">\n");
    // prediction/distribution
    if (m_OutputDistribution) {
      append("    <" + TAG_DISTRIBUTION + ">\n");
      for (int n = 0; n < dist.length; n++) {
        append("      <" + TAG_CLASS_LABEL + " " + ATT_INDEX + "=\"" + (n+1) + "\"");
        if (!Utils.isMissingValue(predValue) && (n == (int) predValue))
          append(" " + ATT_PREDICTED + "=\"" + VAL_YES + "\"");
        append(">");
        append(Utils.doubleToString(dist[n], prec));
        append("</" + TAG_CLASS_LABEL + ">\n");
      }
      append("    </" + TAG_DISTRIBUTION + ">\n");
    }
    else {
      append("    <" + TAG_PREDICTION + ">");
      if (Utils.isMissingValue(predValue))
        append("?");
      else
        append(Utils.doubleToString(dist[(int)predValue], prec));
      append("</" + TAG_PREDICTION + ">\n");
    }
  }

  // attributes
  if (m_Attributes != null)
    append(attributeValuesString(withMissing));
  
  // closing tag
  append("  </" + TAG_PREDICTION + ">\n");
}
 
Example 17
Source File: RemoveWithValues.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Input an instance for filtering. Ordinarily the instance is processed
  * and made available for output immediately. Some filters require all
  * instances be read before producing output.
  *
  * @param instance the input instance
  * @return true if the filtered instance may now be
  * collected with output().
  * @throws IllegalStateException if no input format has been set.
  */
 public boolean input(Instance instance) {

   if (getInputFormat() == null) {
     throw new IllegalStateException("No input instance format defined");
   }
   if (m_NewBatch) {
     resetQueue();
     m_NewBatch = false;
   }
   
   if (isFirstBatchDone() && m_dontFilterAfterFirstBatch) {
     push((Instance)instance.copy());
     return true;
   }
   
   if (instance.isMissing(m_AttIndex.getIndex())) {
     if (!getMatchMissingValues()) {
       push((Instance)instance.copy());
       return true;
     } else {
       return false;
     }
   }
   if (isNumeric()) {
     if (!m_Values.getInvert()) {
if (instance.value(m_AttIndex.getIndex()) < m_Value) {
  push((Instance)instance.copy());
  return true;
} 
     } else {
if (instance.value(m_AttIndex.getIndex()) >= m_Value) {
  push((Instance)instance.copy());
  return true;
} 
     }
   }
   if (isNominal()) {
     if (m_Values.isInRange((int)instance.value(m_AttIndex.getIndex()))) {
Instance temp = (Instance)instance.copy();
if (getModifyHeader()) {
  temp.setValue(m_AttIndex.getIndex(),
		m_NominalMapping[(int)instance.value(m_AttIndex.getIndex())]);
}
push(temp);
return true;
     }
   }
   return false;
 }
 
Example 18
Source File: LatentSemanticAnalysis.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Transform an instance in original (unnormalized) format
 * @param instance an instance in the original (unnormalized) format
 * @return a transformed instance
 * @throws Exception if instance can't be transformed
 */
public Instance convertInstance(Instance instance) throws Exception {
  if (m_s == null) {
    throw new Exception("convertInstance: Latent Semantic Analysis not " +
                         "performed yet.");
  }
  
  // array to hold new attribute values
  double [] newValues = new double[m_outputNumAttributes];
  
  // apply filters so new instance is in same format as training instances
  Instance tempInstance = (Instance)instance.copy();
  if (!instance.dataset().equalHeaders(m_trainHeader)) {
    throw new Exception("Can't convert instance: headers don't match: " +
    "LatentSemanticAnalysis");
  }
  // replace missing values
  m_replaceMissingFilter.input(tempInstance);
  m_replaceMissingFilter.batchFinished();
  tempInstance = m_replaceMissingFilter.output();
  // normalize
  if (m_normalize) {
    m_normalizeFilter.input(tempInstance);
    m_normalizeFilter.batchFinished();
    tempInstance = m_normalizeFilter.output();
  }
  // convert nominal attributes to binary
  m_nominalToBinaryFilter.input(tempInstance);
  m_nominalToBinaryFilter.batchFinished();
  tempInstance = m_nominalToBinaryFilter.output();
  // remove class/other attributes
  if (m_attributeFilter != null) {
    m_attributeFilter.input(tempInstance);
    m_attributeFilter.batchFinished();
    tempInstance = m_attributeFilter.output();
  }
  
  // record new attribute values
  if (m_hasClass) { // copy class value
    newValues[m_outputNumAttributes - 1] = instance.classValue();
  }
  double [][] oldInstanceValues = new double[1][m_numAttributes];
  oldInstanceValues[0] = tempInstance.toDoubleArray();
  Matrix instanceVector = new Matrix(oldInstanceValues); // old attribute values
  instanceVector = instanceVector.times(m_transformationMatrix); // new attribute values
  for (int i = 0; i < m_actualRank; i++) {
    newValues[i] = instanceVector.get(0, i);
  }
  
  // return newly transformed instance
  if (instance instanceof SparseInstance) {
    return new SparseInstance(instance.weight(), newValues);
  } else {
    return new DenseInstance(instance.weight(), newValues);
  }
}
 
Example 19
Source File: PrincipalComponents.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Transform an instance in original (unormalized) format. Convert back
 * to the original space if requested.
 * @param instance an instance in the original (unormalized) format
 * @return a transformed instance
 * @throws Exception if instance cant be transformed
 */
public Instance convertInstance(Instance instance) throws Exception {

  if (m_eigenvalues == null) {
    throw new Exception("convertInstance: Principal components not "
                        +"built yet");
  }

  double[] newVals = new double[m_outputNumAtts];
  Instance tempInst = (Instance)instance.copy();
  if (!instance.dataset().equalHeaders(m_trainHeader)) {
    throw new Exception("Can't convert instance: header's don't match: "
                        +"PrincipalComponents\n"
                        + instance.dataset().equalHeadersMsg(m_trainHeader));
  }

  m_replaceMissingFilter.input(tempInst);
  m_replaceMissingFilter.batchFinished();
  tempInst = m_replaceMissingFilter.output();

  /*if (m_normalize) {
    m_normalizeFilter.input(tempInst);
    m_normalizeFilter.batchFinished();
    tempInst = m_normalizeFilter.output();
  }*/

  m_nominalToBinFilter.input(tempInst);
  m_nominalToBinFilter.batchFinished();
  tempInst = m_nominalToBinFilter.output();

  if (m_attributeFilter != null) {
    m_attributeFilter.input(tempInst);
    m_attributeFilter.batchFinished();
    tempInst = m_attributeFilter.output();
  }
  
  if (!m_center) {
    m_standardizeFilter.input(tempInst);
    m_standardizeFilter.batchFinished();
    tempInst = m_standardizeFilter.output();
  } else {
    m_centerFilter.input(tempInst);
    m_centerFilter.batchFinished();
    tempInst = m_centerFilter.output();
  }

  if (m_hasClass) {
     newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex());
  }

  double cumulative = 0;
  for (int i = m_numAttribs - 1; i >= 0; i--) {
    double tempval = 0.0;
    for (int j = 0; j < m_numAttribs; j++) {
      tempval += (m_eigenvectors[j][m_sortedEigens[i]] * 
                  tempInst.value(j));
     }
    newVals[m_numAttribs - i - 1] = tempval;
    cumulative+=m_eigenvalues[m_sortedEigens[i]];
    if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) {
      break;
    }
  }
  
  if (!m_transBackToOriginal) {
    if (instance instanceof SparseInstance) {
    return new SparseInstance(instance.weight(), newVals);
    } else {
      return new DenseInstance(instance.weight(), newVals);
    }      
  } else {
    if (instance instanceof SparseInstance) {
      return convertInstanceToOriginal(new SparseInstance(instance.weight(), 
                                                          newVals));
    } else {
      return convertInstanceToOriginal(new DenseInstance(instance.weight(),
                                                    newVals));
    }
  }
}
 
Example 20
Source File: RenameAttribute.java    From tsml with GNU General Public License v3.0 2 votes vote down vote up
/**
 * processes the given instance (may change the provided instance) and
 * returns the modified version.
 *
 * @param instance    the instance to process
 * @return            the modified data
 * @throws Exception  in case the processing goes wrong
 */
protected Instance process(Instance instance) throws Exception {
  return (Instance) instance.copy();
}