Java Code Examples for weka.core.Attribute#index()

The following examples show how to use weka.core.Attribute#index() . 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: FPGrowth.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
private Instances parseTransactionsMustContain(Instances data) {
  String[] split = m_transactionsMustContain.trim().split(",");
  boolean[] transactionsMustContainIndexes = new boolean[data.numAttributes()];
  int numInTransactionsMustContainList = split.length;
  
  for (int i = 0; i < split.length; i++) {
    String attName = split[i].trim();
    Attribute att = data.attribute(attName);
    if (att == null) {
      System.err.println("[FPGrowth] : WARNING - can't find attribute " 
          + attName + " in the data.");
      numInTransactionsMustContainList--;
    } else {
      transactionsMustContainIndexes[att.index()] = true;
    }
  }
  
  if (numInTransactionsMustContainList == 0) {
    return data;
  } else {
    Instances newInsts = new Instances(data, 0);
    for (int i = 0; i < data.numInstances(); i++) {
      if (passesMustContain(data.instance(i), 
          transactionsMustContainIndexes, numInTransactionsMustContainList)) {
        newInsts.add(data.instance(i));
      }
    }
    newInsts.compactify();
    return newInsts;
  }
}
 
Example 2
Source File: Prism.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Is this attribute mentioned in the rule?
 *
 * @param attr the attribute to be checked for
 * @param t test contained by rule
 * @return true if the attribute is mentioned in the rule
 */
private static boolean isMentionedIn(Attribute attr, Test t) {

  if (t == null) { 
    return false;
  }
  if (t.m_attr == attr.index()) {
    return true;
  }
  return isMentionedIn(attr, t.m_next);
}
 
Example 3
Source File: Id3.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Method for building an Id3 tree.
 *
 * @param data the training data
 * @exception Exception if decision tree can't be built successfully
 */
private void makeTree(Instances data) throws Exception {

  // Check if no instances have reached this node.
  if (data.numInstances() == 0) {
    m_Attribute = null;
    m_ClassValue = Utils.missingValue();
    m_Distribution = new double[data.numClasses()];
    return;
  }

  // Compute attribute with maximum information gain.
  double[] infoGains = new double[data.numAttributes()];
  Enumeration attEnum = data.enumerateAttributes();
  while (attEnum.hasMoreElements()) {
    Attribute att = (Attribute) attEnum.nextElement();
    infoGains[att.index()] = computeInfoGain(data, att);
  }
  m_Attribute = data.attribute(Utils.maxIndex(infoGains));
  
  // Make leaf if information gain is zero. 
  // Otherwise create successors.
  if (Utils.eq(infoGains[m_Attribute.index()], 0)) {
    m_Attribute = null;
    m_Distribution = new double[data.numClasses()];
    Enumeration instEnum = data.enumerateInstances();
    while (instEnum.hasMoreElements()) {
      Instance inst = (Instance) instEnum.nextElement();
      m_Distribution[(int) inst.classValue()]++;
    }
    Utils.normalize(m_Distribution);
    m_ClassValue = Utils.maxIndex(m_Distribution);
    m_ClassAttribute = data.classAttribute();
  } else {
    Instances[] splitData = splitData(data, m_Attribute);
    m_Successors = new Id3[m_Attribute.numValues()];
    for (int j = 0; j < m_Attribute.numValues(); j++) {
      m_Successors[j] = new Id3();
      m_Successors[j].makeTree(splitData[j]);
    }
  }
}