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

The following examples show how to use weka.core.Instance#setMissing() . 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: CheckEstimator.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Add missing values to a dataset.
 *
 * @param data the instances to add missing values to
 * @param level the level of missing values to add (if positive, this
 * is the probability that a value will be set to missing, if negative
 * all but one value will be set to missing (not yet implemented))
 * @param attributeMissing if true, attributes will be modified
 * @param classMissing if true, the class attribute will be modified
 * @param attrIndex index of the attribute
 */
protected void addMissing(Instances data, int level,
	    boolean attributeMissing, boolean classMissing,
	    int attrIndex) {
  
  int classIndex = data.classIndex();
  Random random = new Random(1);
  for (int i = 0; i < data.numInstances(); i++) {
    Instance current = data.instance(i);

    for (int j = 0; j < data.numAttributes(); j++) {
      if (((j == classIndex) && classMissing) ||
          ((j == attrIndex) && attributeMissing)) {
        if (Math.abs(random.nextInt()) % 100 < level)
          current.setMissing(j);
      }
    }
  }
}
 
Example 2
Source File: CheckClusterer.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add missing values to a dataset.
 *
 * @param data the instances to add missing values to
 * @param level the level of missing values to add (if positive, this
 * is the probability that a value will be set to missing, if negative
 * all but one value will be set to missing (not yet implemented))
 * @param predictorMissing if true, predictor attributes will be modified
 */
protected void addMissing(Instances data, int level, boolean predictorMissing) {
  
  Random random = new Random(1);
  for (int i = 0; i < data.numInstances(); i++) {
    Instance current = data.instance(i);
    for (int j = 0; j < data.numAttributes(); j++) {
      if (predictorMissing) {
        if (Math.abs(random.nextInt()) % 100 < level)
          current.setMissing(j);
      }
    }
  }
}
 
Example 3
Source File: StringToNominal.java    From tsml with GNU General Public License v3.0 5 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 structure has been defined.
 */
@Override
public boolean input(Instance instance) {

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

  if (isOutputFormatDefined()) {
    Instance newInstance = (Instance) instance.copy();

    // make sure that we get the right indexes set for the converted
    // string attributes when operating on a second batch of instances
    for (int i = 0; i < newInstance.numAttributes(); i++) {
      if (newInstance.attribute(i).isString() && !newInstance.isMissing(i)
          && m_AttIndices.isInRange(i)) {
        Attribute outAtt = getOutputFormat().attribute(
            newInstance.attribute(i).name());
        String inVal = newInstance.stringValue(i);
        int outIndex = outAtt.indexOfValue(inVal);
        if (outIndex < 0) {
          newInstance.setMissing(i);
        } else {
          newInstance.setValue(i, outIndex);
        }
      }
    }
    push(newInstance);
    return true;
  }

  bufferInput(instance);
  return false;
}
 
Example 4
Source File: MLUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * SetLabelsMissing - Set all (L) labels in x to missing.
 */
public static Instance setLabelsMissing(Instance x, int L) {
	for(int j = 0; j < L ; j++) {
		x.setMissing(j);
	}
	return x;
}
 
Example 5
Source File: AddNoise.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * method to set a new value
  *
  * @param r random function
  * @param numOfValues 
  * @param instance
  * @param useMissing
  */
 private void changeValueRandomly(Random r, int numOfValues,
                                  int indexOfAtt, 
                                  Instance instance, 
                                  boolean useMissing) {
   int currValue;

   // get current value 
   // if value is missing set current value to number of values
   // whiche is the highest possible value plus one 
   if (instance.isMissing(indexOfAtt)) {
     currValue = numOfValues;
   } else {
     currValue = (int) instance.value(indexOfAtt);
   }

   // with only two possible values it is easier
   if ((numOfValues == 2) && (!instance.isMissing(indexOfAtt))) {
instance.setValue(indexOfAtt, (double) ((currValue+1)% 2));
   } else {
     // get randomly a new value not equal to the current value
     // if missing values are used as values they must be treated
     // in a special way
     while (true) {
  int newValue;
       if (useMissing) {
         newValue = (int) (r.nextDouble() * (double) (numOfValues + 1));
       } else {
         newValue = (int) (r.nextDouble() * (double) numOfValues);
       }
       // have we found a new value?
       if (newValue != currValue) { 
         // the value 1 above the highest possible value (=numOfValues)
         // is used as missing value
         if (newValue == numOfValues) { instance.setMissing(indexOfAtt); }
         else { instance.setValue(indexOfAtt, (double) newValue); }
         break;
       }
     }
   }
 }