Java Code Examples for weka.core.Attribute#NOMINAL

The following examples show how to use weka.core.Attribute#NOMINAL . 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: ManhattanDataObject.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Performs manhattan-distance-calculation between two given values
 * @param index of the attribute within the DataObject's instance
 * @param v value_1
 * @param v1 value_2
 * @return double norm-distance between value_1 and value_2
 */
private double computeDistance(int index, double v, double v1) {
    switch (getInstance().attribute(index).type()) {
        case Attribute.NOMINAL:
            return (Utils.isMissingValue(v) || Utils.isMissingValue(v1)
                    || ((int) v != (int) v1)) ? 1 : 0;

        case Attribute.NUMERIC:
            if (Utils.isMissingValue(v) || Utils.isMissingValue(v1)) {
                if (Utils.isMissingValue(v) && Utils.isMissingValue(v1))
                    return 1;
                else {
                    return (Utils.isMissingValue(v)) ? norm(v1, index)
                            : norm(v, index);
                }
            } else
                return norm(v, index) - norm(v1, index);

        default:
            return 0;
    }
}
 
Example 2
Source File: PropositionalToMultiInstance.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 Exception if the input format can't be set 
 * successfully
 */
public boolean setInputFormat(Instances instanceInfo) 
  throws Exception {

  if (instanceInfo.attribute(0).type()!= Attribute.NOMINAL) {
    throw new Exception("The first attribute type of the original propositional instance dataset must be Nominal!");
  }
  super.setInputFormat(instanceInfo);

  /* create a new output format (multi-instance format) */
  Instances newData = instanceInfo.stringFreeStructure();
  Attribute attBagIndex = (Attribute) newData.attribute(0).copy();
  Attribute attClass = (Attribute) newData.classAttribute().copy();
  // remove the bagIndex attribute
  newData.deleteAttributeAt(0);
  // remove the class attribute
  newData.setClassIndex(-1);
  newData.deleteAttributeAt(newData.numAttributes() - 1);

  FastVector attInfo = new FastVector(3); 
  attInfo.addElement(attBagIndex);
  attInfo.addElement(new Attribute("bag", newData)); // relation-valued attribute
  attInfo.addElement(attClass);
  Instances data = new Instances("Multi-Instance-Dataset", attInfo, 0); 
  data.setClassIndex(data.numAttributes() - 1);

  super.setOutputFormat(data.stringFreeStructure());

  m_BagStringAtts = new StringLocator(data.attribute(1).relation());
  m_BagRelAtts    = new RelationalLocator(data.attribute(1).relation());
  
  return true;
}
 
Example 3
Source File: ARAMNetwork.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or 
 * Instance.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

double[] dist = distributionForInstance(instance);
if (dist == null) {
	throw new Exception("Null distribution predicted");
}
switch (instance.classAttribute().type()) {
	case Attribute.NOMINAL:
		double max = 0;
		int maxIndex = 0;
	
		for (int i = 0; i < dist.length; i++) {
			if (dist[i] > max) {
				maxIndex = i;
				max = dist[i];
			}
		}
		if (max > 0) {
			return maxIndex;
		} else {
		    //return Instance.missingValue();
		}
	case Attribute.NUMERIC:
		return dist[0];
default:
    return -1;
}

}
 
Example 4
Source File: AbstractClassifier.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or
 * Utils.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

  double [] dist = distributionForInstance(instance);
  if (dist == null) {
    throw new Exception("Null distribution predicted");
  }
  switch (instance.classAttribute().type()) {
  case Attribute.NOMINAL:
    double max = 0;
    int maxIndex = 0;

    for (int i = 0; i < dist.length; i++) {
      if (dist[i] > max) {
        maxIndex = i;
        max = dist[i];
      }
    }
    if (max > 0) {
      return maxIndex;
    } else {
      return Utils.missingValue();
    }
  case Attribute.NUMERIC:
    return dist[0];
  default:
    return Utils.missingValue();
  }
}
 
Example 5
Source File: KStar.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Calculates the transformation probability of the indexed test attribute 
  * to the indexed train attribute.
  *
  * @param first the test instance.
  * @param second the train instance.
  * @param col the index of the attribute in the instance.
  * @return the value of the transformation probability.
  */
 private double attrTransProb(Instance first, Instance second, int col) {
   String debug = "(KStar.attrTransProb)";
   double transProb = 0.0;
   KStarNominalAttribute ksNominalAttr;
   KStarNumericAttribute ksNumericAttr;
   switch ( m_Train.attribute(col).type() )
     {
     case Attribute.NOMINAL:
ksNominalAttr = new KStarNominalAttribute(first, second, col, m_Train, 
					  m_RandClassCols, 
					  m_Cache[col]);
ksNominalAttr.setOptions(m_MissingMode, m_BlendMethod, m_GlobalBlend);
transProb = ksNominalAttr.transProb();
ksNominalAttr = null;
break;

     case Attribute.NUMERIC:
ksNumericAttr = new KStarNumericAttribute(first, second, col, 
					  m_Train, m_RandClassCols, 
					  m_Cache[col]);
ksNumericAttr.setOptions(m_MissingMode, m_BlendMethod, m_GlobalBlend);
transProb = ksNumericAttr.transProb();
ksNumericAttr = null;
break;
     }
   return transProb;
 }
 
Example 6
Source File: ARAMNetworkfast.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or 
 * Instance.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

double[] dist = distributionForInstance(instance);
if (dist == null) {
	throw new Exception("Null distribution predicted");
}
switch (instance.classAttribute().type()) {
	case Attribute.NOMINAL:
		double max = 0;
		int maxIndex = 0;
	
		for (int i = 0; i < dist.length; i++) {
			if (dist[i] > max) {
				maxIndex = i;
				max = dist[i];
			}
		}
		if (max > 0) {
			return maxIndex;
		} else {
		    //return Instance.missingValue();
		}
	case Attribute.NUMERIC:
		return dist[0];
default:
    return -1;
}

}
 
Example 7
Source File: RemoveType.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the attribute type to be deleted by the filter.
 *
 * @param typeString a String representing the new type the filter should delete
 */
protected void setAttributeTypeString(String typeString) {

  typeString = typeString.toLowerCase();
  if (typeString.equals("nominal")) m_attTypeToDelete = Attribute.NOMINAL;
  else if (typeString.equals("numeric")) m_attTypeToDelete = Attribute.NUMERIC;
  else if (typeString.equals("string")) m_attTypeToDelete = Attribute.STRING;
  else if (typeString.equals("date")) m_attTypeToDelete = Attribute.DATE;
  else if (typeString.equals("relational")) m_attTypeToDelete = Attribute.RELATIONAL;
}
 
Example 8
Source File: ARAMNetworkSparseV.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or 
 * Instance.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

double[] dist = distributionForInstance(instance);
if (dist == null) {
	throw new Exception("Null distribution predicted");
}
switch (instance.classAttribute().type()) {
	case Attribute.NOMINAL:
		double max = 0;
		int maxIndex = 0;
	
		for (int i = 0; i < dist.length; i++) {
			if (dist[i] > max) {
				maxIndex = i;
				max = dist[i];
			}
		}
		if (max > 0) {
			return maxIndex;
		} else {
		    //return Instance.missingValue();
		}
	case Attribute.NUMERIC:
		return dist[0];
default:
    return -1;
}

}
 
Example 9
Source File: ARAMNetworkSparseH.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Classifies the given test instance. The instance has to belong to a
 * dataset when it's being classified. Note that a classifier MUST
 * implement either this or distributionForInstance().
 *
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or 
 * Instance.missingValue() if no prediction is made
 * @exception Exception if an error occurred during the prediction
 */
public double classifyInstance(Instance instance) throws Exception {

double[] dist = distributionForInstance(instance);
if (dist == null) {
	throw new Exception("Null distribution predicted");
}
switch (instance.classAttribute().type()) {
	case Attribute.NOMINAL:
		double max = 0;
		int maxIndex = 0;
	
		for (int i = 0; i < dist.length; i++) {
			if (dist[i] > max) {
				maxIndex = i;
				max = dist[i];
			}
		}
		if (max > 0) {
			return maxIndex;
		} else {
		    //return Instance.missingValue();
		}
	case Attribute.NUMERIC:
		return dist[0];
default:
    return -1;
}

}
 
Example 10
Source File: CheckEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
AttrTypes (int type) {
  if (type == Attribute.NOMINAL) nominal = true;
  if (type == Attribute.NUMERIC) numeric = true;
  if (type == Attribute.STRING) string = true;
  if (type == Attribute.DATE) date = true;
  if (type == Attribute.RELATIONAL) relational = true;
}
 
Example 11
Source File: CheckAttributeSelection.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Run a battery of tests for a given class attribute type
  *
  * @param classType true if the class attribute should be numeric
  * @param weighted true if the scheme says it handles weights
  * @param multiInstance true if the scheme handles multi-instance data
  */
 protected void testsPerClassType(int classType, 
                                  boolean weighted,
                                  boolean multiInstance) {
   
   boolean PNom = canPredict(true,  false, false, false, false, multiInstance, classType)[0];
   boolean PNum = canPredict(false, true,  false, false, false, multiInstance, classType)[0];
   boolean PStr = canPredict(false, false, true,  false, false, multiInstance, classType)[0];
   boolean PDat = canPredict(false, false, false, true,  false, multiInstance, classType)[0];
   boolean PRel;
   if (!multiInstance)
     PRel = canPredict(false, false, false, false,  true, multiInstance, classType)[0];
   else
     PRel = false;

   if (PNom || PNum || PStr || PDat || PRel) {
     if (weighted)
       instanceWeights(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
     
     if (classType == Attribute.NOMINAL)
       canHandleNClasses(PNom, PNum, PStr, PDat, PRel, multiInstance, 4);

     if (!multiInstance) {
canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 0);
canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 1);
     }
     
     canHandleZeroTraining(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
     boolean handleMissingPredictors = canHandleMissing(PNom, PNum, PStr, PDat, PRel, 
         multiInstance, classType, 
         true, false, 20)[0];
     if (handleMissingPredictors)
       canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, true, false, 100);
     
     boolean handleMissingClass = canHandleMissing(PNom, PNum, PStr, PDat, PRel, 
         multiInstance, classType, 
         false, true, 20)[0];
     if (handleMissingClass)
       canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, false, true, 100);
     
     correctSearchInitialisation(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
     datasetIntegrity(PNom, PNum, PStr, PDat, PRel, multiInstance, classType,
         handleMissingPredictors, handleMissingClass);
   }
 }
 
Example 12
Source File: RandomTree.java    From KEEL 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 PrototypeSet[] splitData(PrototypeSet data) throws Exception {

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

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

      // Get instance
      Prototype inst = data.get(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) {
        	  Prototype copy = new Prototype(inst);
            copy.setWeight(m_Prop[k] * inst.getWeight());
            subsets[k].add(copy);
          }
        }

        // Proceed to next instance
        continue;
      }*/

      // Do we have a nominal attribute?
      if (Attributes.getInputAttribute(m_Attribute).getType() == Attribute.NOMINAL) {//data.attribute(m_Attribute).isNominal()
        subsets[(int)inst.getInput(m_Attribute)].add(inst);

        // Proceed to next instance
        continue;
      }

      // Do we have a numeric attribute?
      if (!(Attributes.getInputAttribute(m_Attribute).getType() == Attribute.NOMINAL)) { // ata.attribute(m_Attribute).isNumeric()
        subsets[(inst.getInput(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: NO SE LO QUE HACE!
    /*for (int i = 0; i < m_Prop.length; i++) {
      subsets[i].compactify();
    }
*/
    // Return the subsets
    return subsets;
  }
 
Example 13
Source File: CheckAssociator.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print out a short summary string for the dataset characteristics
 *
 * @param nominalPredictor true if nominal predictor attributes are present
 * @param numericPredictor true if numeric predictor attributes are present
 * @param stringPredictor true if string predictor attributes are present
 * @param datePredictor true if date predictor attributes are present
 * @param relationalPredictor true if relational predictor attributes are present
 * @param multiInstance whether multi-instance is needed
 * @param classType the class type (NUMERIC, NOMINAL, etc.)
 */
protected void printAttributeSummary(boolean nominalPredictor, 
                                     boolean numericPredictor, 
                                     boolean stringPredictor, 
                                     boolean datePredictor, 
                                     boolean relationalPredictor, 
                                     boolean multiInstance,
                                     int classType) {
  
  String str = "";

  if (numericPredictor)
    str += " numeric";
  
  if (nominalPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " nominal";
  }
  
  if (stringPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " string";
  }
  
  if (datePredictor) {
    if (str.length() > 0)
      str += " &";
    str += " date";
  }
  
  if (relationalPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " relational";
  }
  
  str += " predictors)";
  
  switch (classType) {
    case Attribute.NUMERIC:
      str = " (numeric class," + str;
      break;
    case Attribute.NOMINAL:
      str = " (nominal class," + str;
      break;
    case Attribute.STRING:
      str = " (string class," + str;
      break;
    case Attribute.DATE:
      str = " (date class," + str;
      break;
    case Attribute.RELATIONAL:
      str = " (relational class," + str;
      break;
    case NO_CLASS:
      str = " (no class," + str;
      break;
  }
  
  print(str);
}
 
Example 14
Source File: CheckClassifier.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Run a battery of tests for a given class attribute type
 *
 * @param classType true if the class attribute should be numeric
 * @param updateable true if the classifier is updateable
 * @param weighted true if the classifier says it handles weights
 * @param multiInstance true if the classifier is a multi-instance classifier
 */
protected void testsPerClassType(int classType,
                                 boolean updateable,
                                 boolean weighted,
                                 boolean multiInstance) {

  boolean PNom = canPredict(true,  false, false, false, false, multiInstance, classType)[0];
  boolean PNum = canPredict(false, true,  false, false, false, multiInstance, classType)[0];
  boolean PStr = canPredict(false, false, true,  false, false, multiInstance, classType)[0];
  boolean PDat = canPredict(false, false, false, true,  false, multiInstance, classType)[0];
  boolean PRel;
  if (!multiInstance)
    PRel = canPredict(false, false, false, false,  true, multiInstance, classType)[0];
  else
    PRel = false;

  if (PNom || PNum || PStr || PDat || PRel) {
    if (weighted)
      instanceWeights(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);

    canHandleOnlyClass(PNom, PNum, PStr, PDat, PRel, classType);

    if (classType == Attribute.NOMINAL)
      canHandleNClasses(PNom, PNum, PStr, PDat, PRel, multiInstance, 4);

    if (!multiInstance) {
      canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 0);
      canHandleClassAsNthAttribute(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, 1);
    }

    canHandleZeroTraining(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
    boolean handleMissingPredictors = canHandleMissing(PNom, PNum, PStr, PDat, PRel,
        multiInstance, classType,
        true, false, 20)[0];
    if (handleMissingPredictors)
      canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, true, false, 100);

    boolean handleMissingClass = canHandleMissing(PNom, PNum, PStr, PDat, PRel,
        multiInstance, classType,
        false, true, 20)[0];
    if (handleMissingClass)
      canHandleMissing(PNom, PNum, PStr, PDat, PRel, multiInstance, classType, false, true, 100);

    correctBuildInitialisation(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
    datasetIntegrity(PNom, PNum, PStr, PDat, PRel, multiInstance, classType,
        handleMissingPredictors, handleMissingClass);
    doesntUseTestClassVal(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
    if (updateable)
      updatingEquality(PNom, PNum, PStr, PDat, PRel, multiInstance, classType);
  }
}
 
Example 15
Source File: CheckClassifier.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print out a short summary string for the dataset characteristics
 *
 * @param nominalPredictor true if nominal predictor attributes are present
 * @param numericPredictor true if numeric predictor attributes are present
 * @param stringPredictor true if string predictor attributes are present
 * @param datePredictor true if date predictor attributes are present
 * @param relationalPredictor true if relational predictor attributes are present
 * @param multiInstance whether multi-instance is needed
 * @param classType the class type (NUMERIC, NOMINAL, etc.)
 */
protected void printAttributeSummary(boolean nominalPredictor,
                                     boolean numericPredictor,
                                     boolean stringPredictor,
                                     boolean datePredictor,
                                     boolean relationalPredictor,
                                     boolean multiInstance,
                                     int classType) {

  String str = "";

  if (numericPredictor)
    str += " numeric";

  if (nominalPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " nominal";
  }

  if (stringPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " string";
  }

  if (datePredictor) {
    if (str.length() > 0)
      str += " &";
    str += " date";
  }

  if (relationalPredictor) {
    if (str.length() > 0)
      str += " &";
    str += " relational";
  }

  str += " predictors)";

  switch (classType) {
    case Attribute.NUMERIC:
      str = " (numeric class," + str;
      break;
    case Attribute.NOMINAL:
      str = " (nominal class," + str;
      break;
    case Attribute.STRING:
      str = " (string class," + str;
      break;
    case Attribute.DATE:
      str = " (date class," + str;
      break;
    case Attribute.RELATIONAL:
      str = " (relational class," + str;
      break;
  }

  print(str);
}
 
Example 16
Source File: CheckEstimator.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print out a short summary string for the dataset characteristics
 *
 * @param attrTypes the attribute types used (NUMERIC, NOMINAL, etc.)
 * @param classType the class type (NUMERIC, NOMINAL, etc.)
 */
protected void printAttributeSummary(AttrTypes attrTypes, int classType) {
  
  String str = "";
  
  if (attrTypes.numeric)
    str += " numeric";
  
  if (attrTypes.nominal) {
    if (str.length() > 0)
      str += " &";
    str += " nominal";
  }
  
  if (attrTypes.string) {
    if (str.length() > 0)
      str += " &";
    str += " string";
  }
  
  if (attrTypes.date) {
    if (str.length() > 0)
      str += " &";
    str += " date";
  }
  
  if (attrTypes.relational) {
    if (str.length() > 0)
      str += " &";
    str += " relational";
  }
  
  str += " attributes)";
  
  switch (classType) {
    case Attribute.NUMERIC:
      str = " (numeric class," + str;
      break;
    case Attribute.NOMINAL:
      str = " (nominal class," + str;
      break;
    case Attribute.STRING:
      str = " (string class," + str;
      break;
    case Attribute.DATE:
      str = " (date class," + str;
      break;
    case Attribute.RELATIONAL:
      str = " (relational class," + str;
      break;
  }
  
  print(str);
}
 
Example 17
Source File: RandomTree.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Computes class distribution of an instance using the decision tree.
  * 
  * @param instance
  *            the instance to compute the distribution for
  * @return the computed class distribution
  * @throws Exception
  *             if computation fails
  */
 public double[] distributionForInstance(Prototype instance) throws Exception {


   double[] returnedDist = null;

//   System.out.println("DISTRUBITON FOR INSTANCE = "+m_Attribute);
  // System.out.println("Number of class "+ this.nClasses);
   
   if (m_Attribute > -1) {

     // Node is not a leaf
     /*if (instance.isMissing(m_Attribute)) {

       // Value is missing
       returnedDist = new double[this.nClasses]; //m_Info.numClasses()

       // Split instance up
       for (int i = 0; i < m_Successors.length; i++) {
         double[] help = m_Successors[i]
                                      .distributionForInstance(instance);
         if (help != null) {
           for (int j = 0; j < help.length; j++) {
             returnedDist[j] += m_Prop[i] * help[j];
           }
         }
       }
     } else */
   	
   	// no hay missing values en mi programa.
   	
      if (Attributes.getInputAttribute(m_Attribute).getType() == Attribute.NOMINAL)  {

       // For nominal attributes
       returnedDist = m_Successors[(int) instance.getInput(m_Attribute)].distributionForInstance(instance);
     } else {

       // For numeric attributes
       if (instance.getInput(m_Attribute) < m_SplitPoint) {
         returnedDist = m_Successors[0].distributionForInstance(instance);
       } else {
         returnedDist = m_Successors[1].distributionForInstance(instance);
       }
     }
   }


   // Node is a leaf or successor is empty?
   if ((m_Attribute == -1) || (returnedDist == null)) {

     // Is node empty?
     if (m_ClassDistribution == null) {
       if (getAllowUnclassifiedInstances()) {
       
         return new double[this.nClasses]; //m_Info.numClasses()
       } else {
         return null;
       }
     }

     // Else return normalized distribution
     double[] normalizedDistribution = (double[]) m_ClassDistribution.clone();
     
     //    Utils.normalize(normalizedDistribution);
     double sumatoria =0;
     
     for(int j=0; j<normalizedDistribution.length; j++){
   	  sumatoria+=normalizedDistribution[j];
     }
     
     for(int j=0; j<normalizedDistribution.length; j++){
   	  normalizedDistribution[j]/=sumatoria;
     }
     
 
     
     return normalizedDistribution;
   } else {
     return returnedDist;
   }
 }
 
Example 18
Source File: XMLInstances.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * adds the attribute to the XML structure
  * 
  * @param parent	the parent node to add the attribute node as child
  * @param att		the attribute to add
  */
 protected void addAttribute(Element parent, Attribute att) {
   Element		node;
   Element		child;
   Element		property;
   Element		label;
   String		tmpStr;
   Enumeration		enm;
   int			i;
   
   node = m_Document.createElement(TAG_ATTRIBUTE);
   parent.appendChild(node);
   
   // XML attributes
   // name
   node.setAttribute(ATT_NAME, validContent(att.name()));
   
   // type
   switch (att.type()) {
     case Attribute.NUMERIC:
node.setAttribute(ATT_TYPE, VAL_NUMERIC);
break;

     case Attribute.DATE:
node.setAttribute(ATT_TYPE, VAL_DATE);
break;

     case Attribute.NOMINAL:
node.setAttribute(ATT_TYPE, VAL_NOMINAL);
break;

     case Attribute.STRING:
node.setAttribute(ATT_TYPE, VAL_STRING);
break;

     case Attribute.RELATIONAL:
node.setAttribute(ATT_TYPE, VAL_RELATIONAL);
break;

     default:
node.setAttribute(ATT_TYPE, "???");
   }
   
   // labels
   if (att.isNominal()) {
     child = m_Document.createElement(TAG_LABELS);
     node.appendChild(child);
     enm = att.enumerateValues();
     while (enm.hasMoreElements()) {
tmpStr = enm.nextElement().toString();
label = m_Document.createElement(TAG_LABEL);
child.appendChild(label);
label.appendChild(m_Document.createTextNode(validContent(tmpStr)));
     }
   }
   
   // format
   if (att.isDate())
     node.setAttribute(ATT_FORMAT, validContent(att.getDateFormat()));
   
   // class
   if (m_Instances.classIndex() > -1) {
     if (att == m_Instances.classAttribute())
node.setAttribute(ATT_CLASS, VAL_YES);
   }
   
   // add meta-data
   if ( (att.getMetadata() != null) && (att.getMetadata().size() > 0) ) {
     child = m_Document.createElement(TAG_METADATA);
     node.appendChild(child);
     enm = att.getMetadata().propertyNames();
     while (enm.hasMoreElements()) {
tmpStr = enm.nextElement().toString();
property = m_Document.createElement(TAG_PROPERTY);
child.appendChild(property);
property.setAttribute(ATT_NAME, validContent(tmpStr));
property.appendChild(m_Document.createTextNode(validContent(att.getMetadata().getProperty(tmpStr, ""))));
     }
   }
   
   // relational attribute?
   if (att.isRelationValued()) {
     child = m_Document.createElement(TAG_ATTRIBUTES);
     node.appendChild(child);
     for (i = 0; i < att.relation().numAttributes(); i++)
addAttribute(child, att.relation().attribute(i));
   }
 }
 
Example 19
Source File: CheckEstimator.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print out a short summary string for the dataset characteristics
 *
 * @param attrType the attribute type (NUMERIC, NOMINAL, etc.)
 * @param classType the class type (NUMERIC, NOMINAL, etc.)
 */
protected void printAttributeSummary(int attrType, int classType) {
  
  String str = "";
  
  switch (attrType) {
  case Attribute.NUMERIC:
    str = " numeric" + str;
    break;
  case Attribute.NOMINAL:
    str = " nominal" + str;
    break;
  case Attribute.STRING:
    str = " string" + str;
    break;
  case Attribute.DATE:
    str = " date" + str;
    break;
  case Attribute.RELATIONAL:
    str = " relational" + str;
    break;
  }
  str += " attribute(s))";
  
  switch (classType) {
  case Attribute.NUMERIC:
    str = " (numeric class," + str;
    break;
  case Attribute.NOMINAL:
    str = " (nominal class," + str;
    break;
  case Attribute.STRING:
    str = " (string class," + str;
    break;
  case Attribute.DATE:
    str = " (date class," + str;
    break;
  case Attribute.RELATIONAL:
    str = " (relational class," + str;
    break;
  }
  
  print(str);
}
 
Example 20
Source File: Add.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
  * Parses a given list of options. <p/>
  * 
  <!-- options-start -->
  * Valid options are: <p/>
  * 
  * <pre> -T &lt;NUM|NOM|STR|DAT&gt;
  *  The type of attribute to create:
  *  NUM = Numeric attribute
  *  NOM = Nominal attribute
  *  STR = String attribute
  *  DAT = Date attribute
  *  (default: NUM)</pre>
  * 
  * <pre> -C &lt;index&gt;
  *  Specify where to insert the column. First and last
  *  are valid indexes.(default: last)</pre>
  * 
  * <pre> -N &lt;name&gt;
  *  Name of the new attribute.
  *  (default: 'Unnamed')</pre>
  * 
  * <pre> -L &lt;label1,label2,...&gt;
  *  Create nominal attribute with given labels
  *  (default: numeric attribute)</pre>
  * 
  * <pre> -F &lt;format&gt;
  *  The format of the date values (see ISO-8601)
  *  (default: yyyy-MM-dd'T'HH:mm:ss)</pre>
  * 
  <!-- options-end -->
  *
  * @param options the list of options as an array of strings
  * @throws Exception if an option is not supported
  */
 public void setOptions(String[] options) throws Exception {
   String	tmpStr;

   tmpStr = Utils.getOption('T', options);
   if (tmpStr.length() != 0)
     setAttributeType(new SelectedTag(tmpStr, TAGS_TYPE));
   else
     setAttributeType(new SelectedTag(Attribute.NUMERIC, TAGS_TYPE));
   
   tmpStr = Utils.getOption('C', options);
   if (tmpStr.length() == 0)
     tmpStr = "last";
   setAttributeIndex(tmpStr);
   
   setAttributeName(Utils.unbackQuoteChars(Utils.getOption('N', options)));
   
   if (m_AttributeType == Attribute.NOMINAL) {
     tmpStr = Utils.getOption('L', options);
     if (tmpStr.length() != 0)
setNominalLabels(tmpStr);
   }
   else if (m_AttributeType == Attribute.DATE) {
     tmpStr = Utils.getOption('F', options);
     if (tmpStr.length() != 0)
setDateFormat(tmpStr);
   }

   if (getInputFormat() != null) {
     setInputFormat(getInputFormat());
   }
 }