Java Code Examples for weka.core.FastVector#elements()

The following examples show how to use weka.core.FastVector#elements() . 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: Sequence.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Checks if the Sequence is subsequence of a given data sequence.
  * 
  * @param dataSequence 	the data sequence to verify against
  * @return 			true, if the Sequnce is subsequence of the data 
  * 				sequence, else false
  */
 protected boolean isSubsequenceOf(Instances dataSequence) {
   FastVector elements = getElements();
   Enumeration elementEnum = elements.elements();
   Element curElement = (Element) elementEnum.nextElement();

   for (int i = 0; i < dataSequence.numInstances(); i++) {
     if (curElement.isContainedBy(dataSequence.instance(i))) {
if (!elementEnum.hasMoreElements()) {
  return true;
} else {
  curElement = (Element) elementEnum.nextElement();
  continue;
}
     }
   }
   return false;
 }
 
Example 2
Source File: Sequence.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Updates the support count of a set of Sequence candidates according to a 
  * given set of data sequences.
  * 
  * @param candidates 		the set of candidates
  * @param dataSequences 	the set of data sequences
  */
 public static void updateSupportCount(FastVector candidates, FastVector dataSequences) {
   Enumeration canEnumeration = candidates.elements();

   while(canEnumeration.hasMoreElements()){
     Enumeration dataSeqEnumeration = dataSequences.elements();
     Sequence candidate = (Sequence) canEnumeration.nextElement();

     while(dataSeqEnumeration.hasMoreElements()) {
Instances dataSequence = (Instances) dataSeqEnumeration.nextElement();

if (candidate.isSubsequenceOf(dataSequence)) {
  candidate.setSupportCount(candidate.getSupportCount() + 1);
}
     }
   }
 }
 
Example 3
Source File: Apriori.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method that finds all class association rules.
 * 
 * @throws Exception if an attribute is numeric
 */
private void findCarRulesQuickly() throws Exception {

  FastVector[] rules;

  // Build rules
  for (int j = 0; j < m_Ls.size(); j++) {
    FastVector currentLabeledItemSets = (FastVector) m_Ls.elementAt(j);
    Enumeration enumLabeledItemSets = currentLabeledItemSets.elements();
    while (enumLabeledItemSets.hasMoreElements()) {
      LabeledItemSet currentLabeledItemSet = (LabeledItemSet) enumLabeledItemSets
          .nextElement();
      rules = currentLabeledItemSet.generateRules(m_minMetric, false);
      for (int k = 0; k < rules[0].size(); k++) {
        m_allTheRules[0].addElement(rules[0].elementAt(k));
        m_allTheRules[1].addElement(rules[1].elementAt(k));
        m_allTheRules[2].addElement(rules[2].elementAt(k));
      }
    }
  }
}
 
Example 4
Source File: PredictiveApriori.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns an enumeration describing the available options.
 *
 * @return an enumeration of all the available options.
 */
public Enumeration listOptions() {

  String string1 = "\tThe required number of rules. (default = " + (m_numRules-5) + ")",
    string2 = "\tIf set class association rules are mined. (default = no)",
    string3 = "\tThe class index. (default = last)";
  FastVector newVector = new FastVector(3);

  newVector.addElement(new Option(string1, "N", 1, 
		    "-N <required number of rules output>"));
  newVector.addElement(new Option(string2, "A", 0,
		    "-A"));
  newVector.addElement(new Option(string3, "c", 1,
		    "-c <the class index>"));
  return newVector.elements();
}
 
Example 5
Source File: PredictiveApriori.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** 
 * Method that finds all association rules.
 *
 * @throws Exception if an attribute is numeric
 */
private void findRulesQuickly() throws Exception {

  RuleGeneration currentItemSet;
  
  // Build rules
  for (int j = 0; j < m_Ls.size(); j++) {
    FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);
    Enumeration enumItemSets = currentItemSets.elements();
    while (enumItemSets.hasMoreElements()) { 
      currentItemSet = new RuleGeneration((ItemSet)enumItemSets.nextElement());
      m_best = currentItemSet.generateRules(m_numRules-5, m_midPoints,m_priors,m_expectation,
                                      m_instances,m_best,m_count);
        
      m_count = currentItemSet.m_count;
      if(!m_bestChanged && currentItemSet.m_change)
         m_bestChanged = true;
      //update minimum expected predictive accuracy to get into the n best
      if(m_best.size() >= m_numRules-5)
          m_expectation = ((RuleItem)m_best.first()).accuracy();
      else m_expectation =0;
    }
  }
}
 
Example 6
Source File: PredictiveApriori.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** 
 * Method that finds all class association rules.
 *
 * @throws Exception if an attribute is numeric
 */
private void findCaRulesQuickly() throws Exception {
  
  CaRuleGeneration currentLItemSet;
  // Build rules
  for (int j = 0; j < m_Ls.size(); j++) {
    FastVector currentItemSets = (FastVector)m_Ls.elementAt(j);
    Enumeration enumItemSets = currentItemSets.elements();
    while (enumItemSets.hasMoreElements()) {
      currentLItemSet = new CaRuleGeneration((ItemSet)enumItemSets.nextElement());
      m_best = currentLItemSet.generateRules(m_numRules-5, m_midPoints,m_priors,m_expectation,
                                      m_instances,m_best,m_count);
      m_count = currentLItemSet.count();
      if(!m_bestChanged && currentLItemSet.change())
              m_bestChanged = true;
      if(m_best.size() == m_numRules-5)
          m_expectation = ((RuleItem)m_best.first()).accuracy();
      else 
          m_expectation = 0;
    }
  }
}
 
Example 7
Source File: LabeledItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates counter of a specific item set
 * 
 * @param itemSets an item sets
 * @param instancesNoClass instances without the class attribute
 * @param instancesClass the values of the class attribute sorted according to
 *          instances
 */
public static void upDateCountersTreatZeroAsMissing(FastVector itemSets,
    Instances instancesNoClass, Instances instancesClass) {
  for (int i = 0; i < instancesNoClass.numInstances(); i++) {
    Enumeration enu = itemSets.elements();
    while (enu.hasMoreElements())
      ((LabeledItemSet) enu.nextElement()).upDateCounterTreatZeroAsMissing(
          instancesNoClass.instance(i), instancesClass.instance(i));
  }
}
 
Example 8
Source File: ComplementNaiveBayes.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns an enumeration describing the available options.
 *
 * @return an enumeration of all the available options.
 */
public java.util.Enumeration listOptions() {
    FastVector newVector = new FastVector(2);
    newVector.addElement(
    new Option("\tNormalize the word weights for each class\n",
               "N", 0,"-N"));
    newVector.addElement(
    new Option("\tSmoothing value to avoid zero WordGivenClass"+
               " probabilities (default=1.0).\n",
               "S", 1,"-S"));
    
    return newVector.elements();
}
 
Example 9
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Returns a String representation of a set of Sequences where the numeric 
  * value of each event/item is represented by its respective nominal value.
  * 
  * @param setOfSequences 	the set of Sequences
  * @param dataSet 		the corresponding data set containing the header 
  * 				information
  * @param filterAttributes	the attributes to filter out
  * @return 			the String representation
  */
 public static String setOfSequencesToString(FastVector setOfSequences, Instances dataSet, FastVector filterAttributes) {
   StringBuffer resString = new StringBuffer();
   Enumeration SequencesEnum = setOfSequences.elements();
   int i = 1;
   boolean printSeq;

   while(SequencesEnum.hasMoreElements()) {
     Sequence seq = (Sequence) SequencesEnum.nextElement();
     Integer filterAttr = (Integer) filterAttributes.elementAt(0);
     printSeq = true;

     if (filterAttr.intValue() != -1) {
for (int j=0; j < filterAttributes.size(); j++) {
  filterAttr = (Integer) filterAttributes.elementAt(j);
  FastVector seqElements = seq.getElements();

  if (printSeq) {
    for (int k=0; k < seqElements.size(); k++) {
      Element currentElement = (Element) seqElements.elementAt(k);
      int[] currentEvents = currentElement.getEvents();

      if (currentEvents[filterAttr.intValue()] != -1) {
	continue;
      } else {
	printSeq = false;
	break;
      }
    }
  }
}
     }
     if (printSeq) {
resString.append("[" + i++ + "]" + " " + seq.toNominalString(dataSet));
     }
   }
   return resString.toString();
 }
 
Example 10
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Prints a set of Sequences as String output.
 * 
 * @param setOfSequences	the set of sequences
 */
public static void printSetOfSequences(FastVector setOfSequences) {
  Enumeration seqEnum = setOfSequences.elements();
  int i = 1;

  while(seqEnum.hasMoreElements()) {
    Sequence seq = (Sequence) seqEnum.nextElement();
    System.out.print("[" + i++ + "]" + " " + seq.toString());
  }
}
 
Example 11
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts a set of 1-Elements into a set of 1-Sequences.
 * 
 * @param elements 		the set of 1-Elements
 * @return 			the set of 1-Sequences
 */
public static FastVector oneElementsToSequences(FastVector elements) {
  FastVector sequences = new FastVector();
  Enumeration elementEnum = elements.elements();

  while (elementEnum.hasMoreElements()) {
    Sequence seq = new Sequence();
    FastVector seqElements = seq.getElements();
    seqElements.addElement(elementEnum.nextElement());
    sequences.addElement(seq);
  }
  return sequences;
}
 
Example 12
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Deletes Sequences of a given set which don't meet the minimum support 
  * count threshold.
  * 
  * @param sequences 		the set Sequences to be checked
  * @param minSupportCount 	the minimum support count
  * @return 			the set of Sequences after deleting
  */
 public static FastVector deleteInfrequentSequences(FastVector sequences, long minSupportCount) {
   FastVector deletedSequences = new FastVector();
   Enumeration seqEnum = sequences.elements();

   while (seqEnum.hasMoreElements()) {
     Sequence currentSeq = (Sequence) seqEnum.nextElement();
     long curSupportCount = currentSeq.getSupportCount();

     if (curSupportCount >= minSupportCount) {
deletedSequences.addElement(currentSeq);
     }
   }
   return deletedSequences;
 }
 
Example 13
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates counters for a set of item sets and a set of instances.
 * 
 * @param itemSets the set of item sets which are to be updated
 * @param instances the instances to be used for updating the counters
 */
public static void upDateCountersTreatZeroAsMissing(FastVector itemSets,
    Instances instances) {
  for (int i = 0; i < instances.numInstances(); i++) {
    Enumeration enu = itemSets.elements();
    while (enu.hasMoreElements())
      ((ItemSet) enu.nextElement()).updateCounterTreatZeroAsMissing(instances
          .instance(i));
  }
}
 
Example 14
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates counters for a set of item sets and a set of instances.
 * 
 * @param itemSets the set of item sets which are to be updated
 * @param instances the instances to be used for updating the counters
 */
public static void upDateCounters(FastVector itemSets, Instances instances) {

  for (int i = 0; i < instances.numInstances(); i++) {
    Enumeration enu = itemSets.elements();
    while (enu.hasMoreElements())
      ((ItemSet) enu.nextElement()).upDateCounter(instances.instance(i));
  }
}
 
Example 15
Source File: Apriori.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method that finds all association rules.
 * 
 * @throws Exception if an attribute is numeric
 */
private void findRulesQuickly() throws Exception {

  FastVector[] rules;
  // Build rules
  for (int j = 1; j < m_Ls.size(); j++) {
    FastVector currentItemSets = (FastVector) m_Ls.elementAt(j);
    Enumeration enumItemSets = currentItemSets.elements();
    while (enumItemSets.hasMoreElements()) {
      AprioriItemSet currentItemSet = (AprioriItemSet) enumItemSets
          .nextElement();
      // AprioriItemSet currentItemSet = new
      // AprioriItemSet((ItemSet)enumItemSets.nextElement());
      rules = currentItemSet.generateRules(m_minMetric, m_hashtables, j + 1);
      for (int k = 0; k < rules[0].size(); k++) {
        m_allTheRules[0].addElement(rules[0].elementAt(k));
        m_allTheRules[1].addElement(rules[1].elementAt(k));
        m_allTheRules[2].addElement(rules[2].elementAt(k));

        if (rules.length > 3) {
          m_allTheRules[3].addElement(rules[3].elementAt(k));
          m_allTheRules[4].addElement(rules[4].elementAt(k));
          m_allTheRules[5].addElement(rules[5].elementAt(k));
        }
      }
    }
  }
}
 
Example 16
Source File: Apriori.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method that finds all association rules and performs significance test.
 * 
 * @throws Exception if an attribute is numeric
 */
private void findRulesBruteForce() throws Exception {

  FastVector[] rules;

  // Build rules
  for (int j = 1; j < m_Ls.size(); j++) {
    FastVector currentItemSets = (FastVector) m_Ls.elementAt(j);
    Enumeration enumItemSets = currentItemSets.elements();
    while (enumItemSets.hasMoreElements()) {
      AprioriItemSet currentItemSet = (AprioriItemSet) enumItemSets
          .nextElement();
      // AprioriItemSet currentItemSet = new
      // AprioriItemSet((ItemSet)enumItemSets.nextElement());
      rules = currentItemSet.generateRulesBruteForce(m_minMetric,
          m_metricType, m_hashtables, j + 1, m_instances.numInstances(),
          m_significanceLevel);
      for (int k = 0; k < rules[0].size(); k++) {
        m_allTheRules[0].addElement(rules[0].elementAt(k));
        m_allTheRules[1].addElement(rules[1].elementAt(k));
        m_allTheRules[2].addElement(rules[2].elementAt(k));

        m_allTheRules[3].addElement(rules[3].elementAt(k));
        m_allTheRules[4].addElement(rules[4].elementAt(k));
        m_allTheRules[5].addElement(rules[5].elementAt(k));
      }
    }
  }
}
 
Example 17
Source File: LabeledItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates counter of a specific item set
 * 
 * @param itemSets an item sets
 * @param instancesNoClass instances without the class attribute
 * @param instancesClass the values of the class attribute sorted according to
 *          instances
 */
public static void upDateCounters(FastVector itemSets,
    Instances instancesNoClass, Instances instancesClass) {

  for (int i = 0; i < instancesNoClass.numInstances(); i++) {
    Enumeration enu = itemSets.elements();
    while (enu.hasMoreElements())
      ((LabeledItemSet) enu.nextElement()).upDateCounter(
          instancesNoClass.instance(i), instancesClass.instance(i));
  }

}
 
Example 18
Source File: Apriori.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns an enumeration describing the available options.
 * 
 * @return an enumeration of all the available options.
 */
@Override
public Enumeration listOptions() {

  String string1 = "\tThe required number of rules. (default = " + m_numRules
      + ")", string2 = "\tThe minimum confidence of a rule. (default = "
      + m_minMetric + ")", string3 = "\tThe delta by which the minimum support is decreased in\n", string4 = "\teach iteration. (default = "
      + m_delta + ")", string5 = "\tThe lower bound for the minimum support. (default = "
      + m_lowerBoundMinSupport + ")", string6 = "\tIf used, rules are tested for significance at\n", string7 = "\tthe given level. Slower. (default = no significance testing)", string8 = "\tIf set the itemsets found are also output. (default = no)", string9 = "\tIf set class association rules are mined. (default = no)", string10 = "\tThe class index. (default = last)", stringType = "\tThe metric type by which to rank rules. (default = "
      + "confidence)", stringZeroAsMissing = "\tTreat zero (i.e. first value of nominal attributes) as "
      + "missing", stringToStringDelimiters = "\tIf used, two characters to use as rule delimiters\n"
      + "\tin the result of toString: the first to delimit fields,\n"
      + "\tthe second to delimit items within fields.\n"
      + "\t(default = traditional toString result)";

  FastVector newVector = new FastVector(14);

  newVector.addElement(new Option(string1, "N", 1,
      "-N <required number of rules output>"));
  newVector.addElement(new Option(stringType, "T", 1,
      "-T <0=confidence | 1=lift | " + "2=leverage | 3=Conviction>"));
  newVector.addElement(new Option(string2, "C", 1,
      "-C <minimum metric score of a rule>"));
  newVector.addElement(new Option(string3 + string4, "D", 1,
      "-D <delta for minimum support>"));
  newVector.addElement(new Option("\tUpper bound for minimum support. "
      + "(default = 1.0)", "U", 1, "-U <upper bound for minimum support>"));
  newVector.addElement(new Option(string5, "M", 1,
      "-M <lower bound for minimum support>"));
  newVector.addElement(new Option(string6 + string7, "S", 1,
      "-S <significance level>"));
  newVector.addElement(new Option(string8, "I", 0, "-I"));
  newVector.addElement(new Option("\tRemove columns that contain "
      + "all missing values (default = no)", "R", 0, "-R"));
  newVector.addElement(new Option("\tReport progress iteratively. (default "
      + "= no)", "V", 0, "-V"));
  newVector.addElement(new Option(string9, "A", 0, "-A"));
  newVector.addElement(new Option(stringZeroAsMissing, "Z", 0, "-Z"));
  newVector.addElement(new Option(stringToStringDelimiters, "B", 1,
      "-B <toString delimiters>"));
  newVector.addElement(new Option(string10, "c", 1, "-c <the class index>"));

  return newVector.elements();
}
 
Example 19
Source File: MauiFilter.java    From maui-2 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().
 * @exception Exception
 *                if the input instance was not of the correct format or if
 *                there was a problem with the filtering.
 */
@SuppressWarnings("unchecked")
public boolean input(Instance instance) throws Exception {

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

	if (debugMode) {
		System.err.println("-- Reading instance");
	}

	phraseFilter.input(instance);
	phraseFilter.batchFinished();
	instance = phraseFilter.output();

	if (vocabularyName.equals("none")) {
		numbersFilter.input(instance);
		numbersFilter.batchFinished();
		instance = numbersFilter.output();
	}

	if (globalDictionary == null) {

		bufferInput(instance);
		return false;

	} else {

		FastVector vector = convertInstance(instance, false);
		Enumeration<Instance> en = vector.elements();
		while (en.hasMoreElements()) {
			Instance inst = en.nextElement();
			push(inst);
		}
		return true;
	}

}
 
Example 20
Source File: MauiFilter.java    From maui-2 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets output format and converts pending input instances.
 */
@SuppressWarnings("unchecked")
private void convertPendingInstances() throws Exception {

	if (debugMode) {
		System.err.println("--- Converting pending instances");
	}

	// Create output format for filter
	FastVector atts = new FastVector();
	for (int i = 1; i < getInputFormat().numAttributes(); i++) {
		if (i == documentAtt) {
			atts.addElement(new Attribute("Candidate_name",
					(FastVector) null)); // 0
			atts.addElement(new Attribute("Candidate_original",
					(FastVector) null)); // 1
			atts.addElement(new Attribute("Term_frequency")); // 2
			atts.addElement(new Attribute("IDF")); // 3
			atts.addElement(new Attribute("TFxIDF")); // 4
			atts.addElement(new Attribute("First_occurrence")); // 5
			atts.addElement(new Attribute("Last_occurrence")); // 6
			atts.addElement(new Attribute("Spread")); // 7
			atts.addElement(new Attribute("Domain_keyphraseness")); // 8
			atts.addElement(new Attribute("Length")); // 9
			atts.addElement(new Attribute("Generality")); // 10
			atts.addElement(new Attribute("Node_degree")); // 11
			atts.addElement(new Attribute("Semantic_relatedness")); // 12
			atts.addElement(new Attribute("Wikipedia_keyphraseness")); // 13
			atts.addElement(new Attribute("Inverse_Wikip_frequency")); // 14
			atts.addElement(new Attribute("Total_Wikip_keyphraseness")); // 15

			atts.addElement(new Attribute("Probability")); // 16
			atts.addElement(new Attribute("Rank")); // 17

		} else if (i == keyphrasesAtt) {
			if (nominalClassValue) {
				FastVector vals = new FastVector(2);
				vals.addElement("False");
				vals.addElement("True");
				atts.addElement(new Attribute("Keyphrase?", vals));
			} else {
				atts.addElement(new Attribute("Keyphrase?"));
			}
		} else {
			atts.addElement(getInputFormat().attribute(i));
		}
	}

	Instances outFormat = new Instances("mauidata", atts, 0);
	setOutputFormat(outFormat);
	
	// Convert pending input instances into output data
	for (int i = 0; i < getInputFormat().numInstances(); i++) {
		Instance current = getInputFormat().instance(i);
		FastVector vector = convertInstance(current, true);
		Enumeration en = vector.elements();
		while (en.hasMoreElements()) {
			Instance inst = (Instance) en.nextElement();
			push(inst);
		}
	}
}