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

The following examples show how to use weka.core.FastVector#elementAt() . 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: RuleStats.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Static utility function to count the data covered by the 
  * rules after the given index in the given rules, and then
  * remove them.  It returns the data not covered by the
  * successive rules.
  *
  * @param data the data to be processed
  * @param rules the ruleset
  * @param index the given index
  * @return the data after processing
  */
 public static Instances rmCoveredBySuccessives(Instances data, FastVector rules, int index){
   Instances rt = new Instances(data, 0);

   for(int i=0; i < data.numInstances(); i++){
     Instance datum = data.instance(i);
     boolean covered = false;	    
    
     for(int j=index+1; j<rules.size();j++){
Rule rule = (Rule)rules.elementAt(j);
if(rule.covers(datum)){
  covered = true;
  break;
}
     }

     if(!covered)
rt.add(datum);
   }	
   return rt;
 }
 
Example 2
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** space out set of nodes evenly between top and bottom most node in the list
 * @param nodes list of indexes of nodes to space out
 */
public void spaceVertical(FastVector nodes) {
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new spaceVerticalAction(nodes));
	}
	int nMinY = -1;
	int nMaxY = -1;
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nY = getPositionY((Integer) nodes.elementAt(iNode));
		if (nY < nMinY || iNode == 0) {
			nMinY = nY;
		}
		if (nY > nMaxY || iNode == 0) {
			nMaxY = nY;
		}
	}
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		m_nPositionY.setElementAt((int) (nMinY + iNode * (nMaxY - nMinY) / (nodes.size() - 1.0)), nNode);
	}
}
 
Example 3
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** center set of nodes half way between left and right most node in the list
 * @param nodes list of indexes of nodes to center
 */
public void centerHorizontal(FastVector nodes) {
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new centerHorizontalAction(nodes));
	}
	int nMinY = -1;
	int nMaxY = -1;
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nY = getPositionY((Integer) nodes.elementAt(iNode));
		if (nY < nMinY || iNode == 0) {
			nMinY = nY;
		}
		if (nY > nMaxY || iNode == 0) {
			nMaxY = nY;
		}
	}
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		m_nPositionY.setElementAt((nMinY + nMaxY) / 2, nNode);
	}
}
 
Example 4
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
DelValueAction(int nTargetNode, String sValue) {
	try {
		m_nTargetNode = nTargetNode;
		m_sValue = sValue;
		m_att = m_Instances.attribute(nTargetNode);
		SerializedObject so = new SerializedObject(m_Distributions[nTargetNode]);
		m_CPT = (Estimator[]) so.getObject();
		;
		m_children = new FastVector();
		for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {
			if (m_ParentSets[iNode].contains(nTargetNode)) {
				m_children.addElement(iNode);
			}
		}
		m_childAtts = new Estimator[m_children.size()][];
		for (int iChild = 0; iChild < m_children.size(); iChild++) {
			int nChild = (Integer) m_children.elementAt(iChild);
			m_childAtts[iChild] = m_Distributions[nChild];
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: BIFReader.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
Element getDefinition(Document doc, String sName) throws Exception {
	//NodeList nodelist = selectNodeList(doc, "//DEFINITION[normalize-space(FOR/text())=\"" + sName + "\"]");

	NodeList nodelist = doc.getElementsByTagName("DEFINITION");
	for (int iNode = 0; iNode < nodelist.getLength(); iNode++) {
		Node node = nodelist.item(iNode);
		FastVector list = selectElements(node, "FOR");
		if (list.size() > 0) {
			Node forNode = (Node) list.elementAt(0);
			if (getContent((Element) forNode).trim().equals(sName)) {
				return (Element) node;
			}
		}
	}
	throw new Exception("Could not find definition for ((" + sName + "))");
}
 
Example 6
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Prunes a set of (k)-item sets using the given (k-1)-item sets.
 * 
 * @param toPrune the set of (k)-item sets to be pruned
 * @param kMinusOne the (k-1)-item sets to be used for pruning
 * @return the pruned set of item sets
 */
public static FastVector pruneItemSets(FastVector toPrune, Hashtable kMinusOne) {

  FastVector newVector = new FastVector(toPrune.size());
  int help, j;

  for (int i = 0; i < toPrune.size(); i++) {
    ItemSet current = (ItemSet) toPrune.elementAt(i);
    for (j = 0; j < current.m_items.length; j++)
      if (current.m_items[j] != -1) {
        help = current.m_items[j];
        current.m_items[j] = -1;
        if (kMinusOne.get(current) == null) {
          current.m_items[j] = help;
          break;
        } else {
          current.m_items[j] = help;
        }
      }
    if (j == current.m_items.length)
      newVector.addElement(current);
  }
  return newVector;
}
 
Example 7
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
AddArcAction(int nParent, FastVector children) {
	try {
		m_nParent = nParent;
		m_children = new FastVector();
		m_CPT = new Estimator[children.size()][];
		for (int iChild = 0; iChild < children.size(); iChild++) {
			int nChild = (Integer) children.elementAt(iChild);
			m_children.addElement(nChild);
			SerializedObject so = new SerializedObject(m_Distributions[nChild]);
			m_CPT[iChild] = (Estimator[]) so.getObject();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 8
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Delete nodes with indexes in selection from the network, updating instances, parentsets,
 * distributions Conditional distributions are condensed by taking the
 * values for the target node to be its first value. Used for manual
 * manipulation of the Bayesian network.
 *
 * @param nodes
 *            array of indexes of nodes to delete.
 * @throws Exception
 */
public void deleteSelection(FastVector nodes) {
	// sort before proceeding
	for (int i = 0; i < nodes.size(); i++) {
		for (int j = i + 1; j < nodes.size(); j++) {
			if ((Integer) nodes.elementAt(i) > (Integer) nodes.elementAt(j)) {
				int h = (Integer) nodes.elementAt(i);
				nodes.setElementAt(nodes.elementAt(j), i);
				nodes.setElementAt(h, j);
			}
		}
	}
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new DeleteSelectionAction(nodes));
	}
	boolean bNeedsUndoAction = m_bNeedsUndoAction;
	m_bNeedsUndoAction = false;
	try {
		for (int iNode = nodes.size() - 1; iNode >= 0; iNode--) {
			deleteNode((Integer) nodes.elementAt(iNode));
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	m_bNeedsUndoAction = bNeedsUndoAction;
}
 
Example 9
Source File: ADTree.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Continues single (two-class optimized) search by investigating a random path.
 *
 * @param currentNode the root of the subtree to be searched
 * @param posInstances the positive-class instances that apply at this node
 * @param negInstances the negative-class instances that apply at this node
 * @exception Exception if search fails
 */
private void goDownRandomPathSingle(PredictionNode currentNode,
		      Instances posInstances, Instances negInstances)
  throws Exception {

  FastVector children = currentNode.getChildren();
  Splitter split = (Splitter) children.elementAt(getRandom(children.size()));
  int branch = getRandom(split.getNumOfBranches());
  searchForBestTestSingle(split.getChildForBranch(branch),
	    split.instancesDownBranch(branch, posInstances),
	    split.instancesDownBranch(branch, negInstances));
}
 
Example 10
Source File: ThresholdCurve.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 * @param predictions the predictions to use
 * @param classIndex the class index
 * @return the probabilities
 */
private double [] getProbabilities(FastVector predictions, int classIndex) {

  // sort by predicted probability of the desired class.
  double [] probs = new double [predictions.size()];
  for (int i = 0; i < probs.length; i++) {
    NominalPrediction pred = (NominalPrediction)predictions.elementAt(i);
    probs[i] = pred.distribution()[classIndex];
  }
  return probs;
}
 
Example 11
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Checks if two Sequences are equal.
  * 
  * @return 			true, if the two Sequences are equal, else false
  */
 public boolean equals(Object obj) {
   Sequence seq2 = (Sequence) obj;
   FastVector seq2Elements = seq2.getElements();

   for (int i = 0; i < m_Elements.size(); i++) {
     Element thisElement = (Element) m_Elements.elementAt(i);
     Element seq2Element = (Element) seq2Elements.elementAt(i);
     if (!thisElement.equals(seq2Element)) {
return false;
     }
   }
   return true;
 }
 
Example 12
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 13
Source File: Sequence.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Generates candidate k-Sequences on the basis of a given (k-1)-Sequence set.
  * 
  * @param kMinusOneSequences 	the set of (k-1)-Sequences
  * @return 			the set of candidate k-Sequences
  * @throws CloneNotSupportedException
  */
 protected static FastVector generateKCandidates(FastVector kMinusOneSequences) throws CloneNotSupportedException {
   FastVector candidates = new FastVector();
   FastVector mergeResult = new FastVector();

   for (int i = 0; i < kMinusOneSequences.size(); i++) {
     for (int j = 0; j < kMinusOneSequences.size(); j++) {
Sequence originalSeq1 = (Sequence) kMinusOneSequences.elementAt(i);
Sequence seq1 = originalSeq1.clone();
Sequence originalSeq2 = (Sequence) kMinusOneSequences.elementAt(j);
Sequence seq2 = originalSeq2.clone();
Sequence subseq1 = seq1.deleteEvent("first");
Sequence subseq2 = seq2.deleteEvent("last");

if (subseq1.equals(subseq2)) {
  //seq1 and seq2 are 1-sequences
  if ((subseq1.getElements().size() == 0) && (subseq2.getElements().size() == 0)) {
    if (i >= j) {
      mergeResult = merge(seq1, seq2, true, true);
    } else {
      mergeResult = merge(seq1, seq2, true, false);
    }
    //seq1 and seq2 are k-sequences
  } else {
    mergeResult = merge(seq1, seq2, false, false);
  }
  candidates.appendElements(mergeResult);
}
     }
   }
   return candidates;
 }
 
Example 14
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return a hashtable filled with the given item sets.
 * 
 * @param itemSets the set of item sets to be used for filling the hash table
 * @param initialSize the initial size of the hashtable
 * @return the generated hashtable
 */
public static Hashtable getHashtable(FastVector itemSets, int initialSize) {

  Hashtable hashtable = new Hashtable(initialSize);

  for (int i = 0; i < itemSets.size(); i++) {
    ItemSet current = (ItemSet) itemSets.elementAt(i);
    hashtable.put(current, new Integer(current.m_counter));
  }
  return hashtable;
}
 
Example 15
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes all item sets that don't have minimum support.
 * 
 * @return the reduced set of item sets
 * @param maxSupport the maximum support
 * @param itemSets the set of item sets to be pruned
 * @param minSupport the minimum number of transactions to be covered
 */
public static FastVector deleteItemSets(FastVector itemSets, int minSupport,
    int maxSupport) {

  FastVector newVector = new FastVector(itemSets.size());

  for (int i = 0; i < itemSets.size(); i++) {
    ItemSet current = (ItemSet) itemSets.elementAt(i);
    if ((current.m_counter >= minSupport)
        && (current.m_counter <= maxSupport))
      newVector.addElement(current);
  }
  return newVector;
}
 
Example 16
Source File: LabeledItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Prunes a set of (k)-item sets using the given (k-1)-item sets.
 * 
 * @param toPrune the set of (k)-item sets to be pruned
 * @param kMinusOne the (k-1)-item sets to be used for pruning
 * @return the pruned set of item sets
 */
public static FastVector pruneItemSets(FastVector toPrune, Hashtable kMinusOne) {

  FastVector newVector = new FastVector(toPrune.size());
  int help, j;

  for (int i = 0; i < toPrune.size(); i++) {
    LabeledItemSet current = (LabeledItemSet) toPrune.elementAt(i);

    for (j = 0; j < current.m_items.length; j++) {
      if (current.m_items[j] != -1) {
        help = current.m_items[j];
        current.m_items[j] = -1;
        if (kMinusOne.get(current) != null
            && (current.m_classLabel == (((Integer) kMinusOne.get(current))
                .intValue())))
          current.m_items[j] = help;
        else {
          current.m_items[j] = help;
          break;
        }
      }
    }
    if (j == current.m_items.length)
      newVector.addElement(current);
  }
  return newVector;
}
 
Example 17
Source File: LabeledItemSet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes all item sets that don't have minimum support and have more than
 * maximum support
 * 
 * @return the reduced set of item sets
 * @param maxSupport the maximum support
 * @param itemSets the set of item sets to be pruned
 * @param minSupport the minimum number of transactions to be covered
 */
public static FastVector deleteItemSets(FastVector itemSets, int minSupport,
    int maxSupport) {

  FastVector newVector = new FastVector(itemSets.size());

  for (int i = 0; i < itemSets.size(); i++) {
    LabeledItemSet current = (LabeledItemSet) itemSets.elementAt(i);
    if ((current.m_ruleSupCounter >= minSupport)
        && (current.m_ruleSupCounter <= maxSupport))
      newVector.addElement(current);
  }
  return newVector;
}
 
Example 18
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add arc between parent node and each of the nodes in a given list.
 * Distributions are updated as above.
 *
 * @param sParent
 *            name of the parent node
 * @param nodes
 *            array of indexes of child nodes
 * @throws Exception
 */
public void addArc(String sParent, FastVector nodes) throws Exception {
	int nParent = getNode(sParent);
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new AddArcAction(nParent, nodes));
	}
	boolean bNeedsUndoAction = m_bNeedsUndoAction;
	m_bNeedsUndoAction = false;
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		addArc(nParent, nNode);
	}
	m_bNeedsUndoAction = bNeedsUndoAction;
}
 
Example 19
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
alignAction(FastVector nodes) {
	m_nodes = new FastVector(nodes.size());
	m_posX = new FastVector(nodes.size());
	m_posY = new FastVector(nodes.size());
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		m_nodes.addElement(nNode);
		m_posX.addElement(getPositionX(nNode));
		m_posY.addElement(getPositionY(nNode));
	}
}
 
Example 20
Source File: ItemSet.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Merges all item sets in the set of (k-1)-item sets to create the (k)-item
 * sets and updates the counters.
 * 
 * @return the generated (k)-item sets
 * @param totalTrans thetotal number of transactions
 * @param itemSets the set of (k-1)-item sets
 * @param size the value of (k-1)
 */
public static FastVector mergeAllItemSets(FastVector itemSets, int size,
    int totalTrans) {

  FastVector newVector = new FastVector();
  ItemSet result;
  int numFound, k;

  for (int i = 0; i < itemSets.size(); i++) {
    ItemSet first = (ItemSet) itemSets.elementAt(i);
    out: for (int j = i + 1; j < itemSets.size(); j++) {
      ItemSet second = (ItemSet) itemSets.elementAt(j);
      result = new ItemSet(totalTrans);
      result.m_items = new int[first.m_items.length];

      // Find and copy common prefix of size 'size'
      numFound = 0;
      k = 0;
      while (numFound < size) {
        if (first.m_items[k] == second.m_items[k]) {
          if (first.m_items[k] != -1)
            numFound++;
          result.m_items[k] = first.m_items[k];
        } else
          break out;
        k++;
      }

      // Check difference
      while (k < first.m_items.length) {
        if ((first.m_items[k] != -1) && (second.m_items[k] != -1))
          break;
        else {
          if (first.m_items[k] != -1)
            result.m_items[k] = first.m_items[k];
          else
            result.m_items[k] = second.m_items[k];
        }
        k++;
      }
      if (k == first.m_items.length) {
        result.m_counter = 0;

        newVector.addElement(result);
      }
    }
  }
  return newVector;
}