Java Code Examples for weka.core.Instances#renameAttribute()

The following examples show how to use weka.core.Instances#renameAttribute() . 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: SuvervisedFilterPreprocessor.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Instances apply(final Instances data) throws PreprocessingException {
	if (!this.prepared) {
		throw new IllegalStateException("Cannot apply preprocessor before it has been prepared!");
	}
	try {
		Instances inst = this.selector.reduceDimensionality(data);
		if (inst.classIndex() >= 0) {
			inst = WekaUtil.removeClassAttribute(inst);
		}
		for (int i = 0; i < inst.numAttributes(); i++) {
			Attribute a = inst.attribute(i);
			inst.renameAttribute(a, this.getClass().getSimpleName() + "_" + a.name());
		}
		return inst;
	}
	catch (Exception e) {
		throw new PreprocessingException(e);
	}
}
 
Example 2
Source File: DD_DTW.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Instances determineOutputFormat(Instances inputFormat) throws Exception {

    Instances output = new Instances(inputFormat,0);
    output.deleteAttributeAt(0);
    output.setRelationName("goreckiDerivative_"+output.relationName());
    for(int a = 0; a < output.numAttributes()-1; a++){
        output.renameAttribute(a, "derivative_"+a);
    }

    return output;

}
 
Example 3
Source File: TimeSeriesDelta.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the format of the input instances.
 *
 * @param instanceInfo an Instances object containing the input instance
 * structure (any instances contained in the object are ignored - only the
 * structure is required).
 * @return true if the outputFormat may be collected immediately
 * @throws UnsupportedAttributeTypeException if selected
 * attributes are not numeric.  
 */
public boolean setInputFormat(Instances instanceInfo) throws Exception {

  if ((instanceInfo.classIndex() > 0) && (!getFillWithMissing())) {
    throw new IllegalArgumentException("TimeSeriesDelta: Need to fill in missing values " +
                                       "using appropriate option when class index is set.");
  }
  super.setInputFormat(instanceInfo);
  // Create the output buffer
  Instances outputFormat = new Instances(instanceInfo, 0); 
  for(int i = 0; i < instanceInfo.numAttributes(); i++) {
    if (i != instanceInfo.classIndex()) {
      if (m_SelectedCols.isInRange(i)) {
        if (outputFormat.attribute(i).isNumeric()) {
          outputFormat.renameAttribute(i, outputFormat.attribute(i).name()
                                       + " d"
                                       + (m_InstanceRange < 0 ? '-' : '+')
                                       + Math.abs(m_InstanceRange));
        } else {
          throw new UnsupportedAttributeTypeException("Time delta attributes must be numeric!");
        }
      }
    }
  }
  outputFormat.setClassIndex(instanceInfo.classIndex());
  setOutputFormat(outputFormat);
  return true;
}
 
Example 4
Source File: TimeSeriesTranslate.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the format of the input instances.
 *
 * @param instanceInfo an Instances object containing the input instance
 * structure (any instances contained in the object are ignored - only the
 * structure is required).
 * @return true if the outputFormat may be collected immediately
 * @throws UnsupportedAttributeTypeException if selected
 * attributes are not numeric or nominal.
 */
public boolean setInputFormat(Instances instanceInfo) throws Exception {

  if ((instanceInfo.classIndex() > 0) && (!getFillWithMissing())) {
    throw new IllegalArgumentException("TimeSeriesTranslate: Need to fill in missing values " +
                                       "using appropriate option when class index is set.");
  }
  super.setInputFormat(instanceInfo);
  // Create the output buffer
  Instances outputFormat = new Instances(instanceInfo, 0); 
  for(int i = 0; i < instanceInfo.numAttributes(); i++) {
    if (i != instanceInfo.classIndex()) {
      if (m_SelectedCols.isInRange(i)) {
        if (outputFormat.attribute(i).isNominal()
            || outputFormat.attribute(i).isNumeric()) {
          outputFormat.renameAttribute(i, outputFormat.attribute(i).name()
                                       + (m_InstanceRange < 0 ? '-' : '+')
                                       + Math.abs(m_InstanceRange));
        } else {
          throw new UnsupportedAttributeTypeException("Only numeric and nominal attributes may be "
                                                      + " manipulated in time series.");
        }
      }
    }
  }
  outputFormat.setClassIndex(instanceInfo.classIndex());
  setOutputFormat(outputFormat);
  return true;
}