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

The following examples show how to use weka.core.FastVector#size() . 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: 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 2
Source File: Reorder.java    From tsml with GNU General Public License v3.0 6 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 a problem occurs setting the input format
  */
 public boolean setInputFormat(Instances instanceInfo) throws Exception {
   super.setInputFormat(instanceInfo);
   
   FastVector attributes = new FastVector();
   int outputClass = -1;
   m_SelectedAttributes = determineIndices(instanceInfo.numAttributes());
   for (int i = 0; i < m_SelectedAttributes.length; i++) {
     int current = m_SelectedAttributes[i];
     if (instanceInfo.classIndex() == current) {
outputClass = attributes.size();
     }
     Attribute keep = (Attribute)instanceInfo.attribute(current).copy();
     attributes.addElement(keep);
   }
   
   initInputLocators(instanceInfo, m_SelectedAttributes);

   Instances outputFormat = new Instances(instanceInfo.relationName(),
				   attributes, 0); 
   outputFormat.setClassIndex(outputClass);
   setOutputFormat(outputFormat);
   
   return true;
 }
 
Example 3
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 4
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 5
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** align set of nodes with the right most node in the list
 * @param nodes list of indexes of nodes to align
 */
public void alignRight(FastVector nodes) {
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new alignRightAction(nodes));
	}
	int nMaxX = -1;
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nX = getPositionX((Integer) nodes.elementAt(iNode));
		if (nX > nMaxX || iNode == 0) {
			nMaxX = nX;
		}
	}
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		m_nPositionX.setElementAt(nMaxX, nNode);
	}
}
 
Example 6
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 7
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/** align set of nodes with the top most node in the list
 * @param nodes list of indexes of nodes to align
 */
public void alignTop(FastVector nodes) {
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new alignTopAction(nodes));
	}
	int nMinY = -1;
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nY = getPositionY((Integer) nodes.elementAt(iNode));
		if (nY < nMinY || iNode == 0) {
			nMinY = nY;
		}
	}
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		int nNode = (Integer) nodes.elementAt(iNode);
		m_nPositionY.setElementAt(nMinY, nNode);
	}
}
 
Example 8
Source File: ADNode.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** create sub tree
 * @param iNode index of the lowest node in the tree
 * @param nRecords set of records in instances to be considered
 * @param instances data set
        * @return VaryNode representing part of an ADTree
	 **/
public static VaryNode makeVaryNode(int iNode, FastVector nRecords, Instances instances) {
	VaryNode _VaryNode = new VaryNode(iNode);
	int nValues = instances.attribute(iNode).numValues();
               

	// reserve memory and initialize
	FastVector [] nChildRecords = new FastVector[nValues];
	for (int iChild = 0; iChild < nValues; iChild++) {
		nChildRecords[iChild] = new FastVector();
	}
	// divide the records among children
	for (int iRecord = 0; iRecord < nRecords.size(); iRecord++) {
		int iInstance = ((Integer) nRecords.elementAt(iRecord)).intValue();
		nChildRecords[(int) instances.instance(iInstance).value(iNode)].addElement(new Integer(iInstance));
	}

	// find most common value
	int nCount = nChildRecords[0].size();
	int nMCV = 0; 
	for (int iChild = 1; iChild < nValues; iChild++) {
		if (nChildRecords[iChild].size() > nCount) {
			nCount = nChildRecords[iChild].size();
			nMCV = iChild;
		}
	}
               _VaryNode.m_nMCV = nMCV;

               // determine child nodes
               _VaryNode.m_ADNodes = new ADNode[nValues];
	for (int iChild = 0; iChild < nValues; iChild++) {
		if (iChild == nMCV || nChildRecords[iChild].size() == 0) {
			_VaryNode.m_ADNodes[iChild] = null;
		} else {
			_VaryNode.m_ADNodes[iChild] = makeADTree(iNode + 1, nChildRecords[iChild], instances);
		}
	}
	return _VaryNode;
}
 
Example 9
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 10
Source File: GeneralizedSequentialPatterns.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the total number of extracted frequent sequences.
 * 
 * @return 			the total number of frequent sequences
 */
protected int calcFreqSequencesTotal() {
  int total = 0;
  Enumeration allSeqPatternsEnum = m_AllSequentialPatterns.elements();

  while (allSeqPatternsEnum.hasMoreElements()) {
    FastVector kSequences = (FastVector) allSeqPatternsEnum.nextElement();
    total += kSequences.size();
  }

  return total;
}
 
Example 11
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 12
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 13
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
SetGroupPositionAction(FastVector nodes, int dX, int dY) {
	m_nodes = new FastVector(nodes.size());
	for (int iNode = 0; iNode < nodes.size(); iNode++) {
		m_nodes.addElement(nodes.elementAt(iNode));
	}
	m_dX = dX;
	m_dY = dY;
}
 
Example 14
Source File: AprioriItemSet.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.
 * 
 * @param itemSets the set of (k-1)-item sets
 * @param size the value of (k-1)
 * @param totalTrans the total number of transactions in the data
 * @return the generated (k)-item sets
 */
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 AprioriItemSet(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;
}
 
Example 15
Source File: BFTree.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Generate successor nodes for a node and put them into BestFirstElements 
  * according to gini gain or information gain in a descending order.
  *
  * @param BestFirstElements 	list to store BestFirst nodes
  * @param data 		training instance
  * @param subsetSortedIndices	sorted indices of instances of successor nodes
  * @param subsetWeights 	weights of instances of successor nodes
  * @param dists 		class distributions of successor nodes
  * @param att 		attribute used to split the node
  * @param useHeuristic 	if use heuristic search for nominal attributes in multi-class problem
  * @param useGini 		if use Gini index as splitting criterion
  * @throws Exception 		if something goes wrong 
  */
 protected void makeSuccessors(FastVector BestFirstElements,Instances data,
     int[][][] subsetSortedIndices, double[][][] subsetWeights,
     double[][][] dists,
     Attribute att, boolean useHeuristic, boolean useGini) throws Exception {

   m_Successors = new BFTree[2];

   for (int i=0; i<2; i++) {
     m_Successors[i] = new BFTree();
     m_Successors[i].m_isLeaf = true;

     // class probability and distribution for this successor node
     m_Successors[i].m_ClassProbs = new double[data.numClasses()];
     m_Successors[i].m_Distribution = new double[data.numClasses()];
     System.arraycopy(dists[att.index()][i], 0, m_Successors[i].m_ClassProbs,
  0,m_Successors[i].m_ClassProbs.length);
     System.arraycopy(dists[att.index()][i], 0, m_Successors[i].m_Distribution,
  0,m_Successors[i].m_Distribution.length);
     if (Utils.sum(m_Successors[i].m_ClassProbs)!=0)
Utils.normalize(m_Successors[i].m_ClassProbs);

     // split information for this successor node
     double[][] props = new double[data.numAttributes()][2];
     double[][][] subDists = new double[data.numAttributes()][2][data.numClasses()];
     double[][] totalSubsetWeights = new double[data.numAttributes()][2];
     FastVector splitInfo = m_Successors[i].computeSplitInfo(m_Successors[i], data,
  subsetSortedIndices[i], subsetWeights[i], subDists, props,
  totalSubsetWeights, useHeuristic, useGini);

     // branch proportion for this successor node
     int splitIndex = ((Attribute)splitInfo.elementAt(1)).index();
     m_Successors[i].m_Props = new double[2];
     System.arraycopy(props[splitIndex], 0, m_Successors[i].m_Props, 0,
  m_Successors[i].m_Props.length);

     // sorted indices and weights of each attribute for this successor node
     m_Successors[i].m_SortedIndices = new int[data.numAttributes()][0];
     m_Successors[i].m_Weights = new double[data.numAttributes()][0];
     for (int j=0; j<m_Successors[i].m_SortedIndices.length; j++) {
m_Successors[i].m_SortedIndices[j] = subsetSortedIndices[i][j];
m_Successors[i].m_Weights[j] = subsetWeights[i][j];
     }

     // distribution of each attribute for this successor node
     m_Successors[i].m_Dists = new double[data.numAttributes()][2][data.numClasses()];
     for (int j=0; j<subDists.length; j++) {
m_Successors[i].m_Dists[j] = subDists[j];
     }

     // total weights for this successor node. 
     m_Successors[i].m_TotalWeight = Utils.sum(totalSubsetWeights[splitIndex]);

     // insert this successor node into BestFirstElements according to gini gain or information gain
     //  descendingly
     if (BestFirstElements.size()==0) {
BestFirstElements.addElement(splitInfo);
     } else {
double gGain = ((Double)(splitInfo.elementAt(3))).doubleValue();
int vectorSize = BestFirstElements.size();
FastVector lastNode = (FastVector)BestFirstElements.elementAt(vectorSize-1);

// If gini gain is less than that of last node in FastVector
if (gGain<((Double)(lastNode.elementAt(3))).doubleValue()) {
  BestFirstElements.insertElementAt(splitInfo, vectorSize);
} else {
  for (int j=0; j<vectorSize; j++) {
    FastVector node = (FastVector)BestFirstElements.elementAt(j);
    double nodeGain = ((Double)(node.elementAt(3))).doubleValue();
    if (gGain>=nodeGain) {
      BestFirstElements.insertElementAt(splitInfo, j);
      break;
    }
  }
}
     }
   }
 }
 
Example 16
Source File: BIFReader.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** buildStructure parses the BIF document in the DOM tree contained
	 * in the doc parameter and specifies the the network structure and 
	 * probability tables.
	 * It assumes that buildInstances has been called before
	 * @param doc DOM document containing BIF document in DOM tree
	 * @throws Exception if building of structure fails
	 */
    void buildStructure(Document doc)  throws Exception {
        // Get the name of the network
		// initialize conditional distribution tables
		m_Distributions = new Estimator[m_Instances.numAttributes()][];
        for (int iNode = 0; iNode < m_Instances.numAttributes(); iNode++) {
        	// find definition that goes with this node
        	String sName = m_Instances.attribute(iNode).name();
			Element definition = getDefinition(doc, sName);
/*
	        if (nodelist.getLength() == 0) {
	        	throw new Exception("No definition found for node " + sName);
	        }
	        if (nodelist.getLength() > 1) {
	        	System.err.println("More than one definition found for node " + sName + ". Using first definition.");
	        }
	        Element definition = (Element) nodelist.item(0);
*/	        
	        
	        // get the parents for this node
	        // resolve structure
	        FastVector nodelist = getParentNodes(definition);
	        for (int iParent = 0; iParent < nodelist.size(); iParent++) {
	        	Node parentName = ((Node) nodelist.elementAt(iParent)).getFirstChild();
	        	String sParentName = ((CharacterData) (parentName)).getData();
	        	int nParent = getNode(sParentName);
	        	m_ParentSets[iNode].addParent(nParent, m_Instances);
	        }
	        // resolve conditional probability table
		        int nCardinality = m_ParentSets[iNode].getCardinalityOfParents();
	        int nValues = m_Instances.attribute(iNode).numValues();
	        m_Distributions[iNode] = new Estimator[nCardinality];
			for (int i = 0; i < nCardinality; i++) {
				m_Distributions[iNode][i] = new DiscreteEstimatorBayes(nValues, 0.0f);
			}

/*
	        StringBuffer sTable = new StringBuffer();
	        for (int iText = 0; iText < nodelist.getLength(); iText++) {
	        	sTable.append(((CharacterData) (nodelist.item(iText))).getData());
	        	sTable.append(' ');
	        }
	        StringTokenizer st = new StringTokenizer(sTable.toString());
*/
	        String sTable = getTable(definition);
			StringTokenizer st = new StringTokenizer(sTable.toString());
	        
	        
			for (int i = 0; i < nCardinality; i++) {
				DiscreteEstimatorBayes d = (DiscreteEstimatorBayes) m_Distributions[iNode][i];
				for (int iValue = 0; iValue < nValues; iValue++) {
					String sWeight = st.nextToken();
					d.addValue(iValue, new Double(sWeight).doubleValue());
				}
			}
         }
    }
 
Example 17
Source File: EditableBayesNet.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** Delete node value from a node. Distributions for the node are scaled
 * up proportional to existing distribution
 * (or made uniform if zero probability is assigned to remainder of values).
.* Child nodes delete CPTs conditioned on the new value.
 * @param nTargetNode index of the node to delete value from
 * @param sValue name of the value to delete
 */
public void delNodeValue(int nTargetNode, String sValue) throws Exception {
	// update undo stack
	if (m_bNeedsUndoAction) {
		addUndoAction(new DelValueAction(nTargetNode, sValue));
	}
	Attribute att = m_Instances.attribute(nTargetNode);
	int nCardinality = att.numValues();
	FastVector values = new FastVector(nCardinality);
	int nValue = -1;
	for (int iValue = 0; iValue < nCardinality; iValue++) {
		if (att.value(iValue).equals(sValue)) {
			nValue = iValue;
		} else {
			values.addElement(att.value(iValue));
		}
	}
	if (nValue < 0) {
		// could not find value
		throw new Exception("Node " + nTargetNode + " does not have value (" + sValue + ")");
	}
	replaceAtt(nTargetNode, att.name(), values);

	// update distributions
	Estimator[] distributions = m_Distributions[nTargetNode];
	int nCard = values.size();
	for (int iParent = 0; iParent < distributions.length; iParent++) {
		DiscreteEstimatorBayes distribution = new DiscreteEstimatorBayes(nCard, 0);
		double sum = 0;
		for (int iValue = 0; iValue < nCard; iValue++) {
			sum += distributions[iParent].getProbability(iValue);
		}
		if (sum > 0) {
			for (int iValue = 0; iValue < nCard; iValue++) {
				distribution.addValue(iValue, distributions[iParent].getProbability(iValue) / sum);
			}
		} else {
			for (int iValue = 0; iValue < nCard; iValue++) {
				distribution.addValue(iValue, 1.0 / nCard);
			}
		}
		distributions[iParent] = distribution;
	}

	// update distributions of all children
	for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {
		if (m_ParentSets[iNode].contains(nTargetNode)) {
			ParentSet parentSet = m_ParentSets[iNode];
			distributions = m_Distributions[iNode];
			Estimator[] newDistributions = new Estimator[distributions.length * nCard / (nCard + 1)];
			int iCurrentDist = 0;

			int nParents = parentSet.getNrOfParents();
			int[] values2 = new int[nParents];
			// fill in the values
			int nParentCard = parentSet.getFreshCardinalityOfParents(m_Instances) * (nCard + 1) / nCard;
			int iTargetNode = 0;
			while (parentSet.getParent(iTargetNode) != nTargetNode) {
				iTargetNode++;
			}
			int[] nCards = new int[nParents];
			for (int iParent = 0; iParent < nParents; iParent++) {
				nCards[iParent] = getCardinality(parentSet.getParent(iParent));
			}
			nCards[iTargetNode]++;
			for (int iPos = 0; iPos < nParentCard; iPos++) {
				if (values2[iTargetNode] != nValue) {
					newDistributions[iCurrentDist++] = distributions[iPos];
				}
				// update values
				int i = 0;
				values2[i]++;
				while (i < nParents && values2[i] == nCards[i]) {
					values2[i] = 0;
					i++;
					if (i < nParents) {
						values2[i]++;
					}
				}
			}

			m_Distributions[iNode] = newDistributions;
		}
	}
	// update evidence
	if (getEvidence(nTargetNode) > nValue) {
		setEvidence(nTargetNode, getEvidence(nTargetNode) - 1);
	}
}
 
Example 18
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;
}
 
Example 19
Source File: CaRuleGeneration.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Generates all rules for an item set. The item set is the premise.
  * @param numRules the number of association rules the use wants to mine.
  * This number equals the size <i>n</i> of the list of the
  * best rules.
  * @param midPoints the mid points of the intervals
  * @param priors Hashtable that contains the prior probabilities
  * @param expectation the minimum value of the expected predictive accuracy
  * that is needed to get into the list of the best rules
  * @param instances the instances for which association rules are generated
  * @param best the list of the <i>n</i> best rules.
  * The list is implemented as a TreeSet
  * @param genTime the maximum time of generation
  * @return all the rules with minimum confidence for the given item set
  */
 public TreeSet generateRules(int numRules, double[] midPoints, Hashtable priors, double expectation, Instances instances, TreeSet best, int genTime) {

   boolean redundant = false;
   FastVector consequences = new FastVector();
   ItemSet premise;
   RuleItem current = null, old = null;

   Hashtable hashtable;

   m_change = false;
   m_midPoints = midPoints;
   m_priors = priors;
   m_best = best;
   m_expectation = expectation;
   m_count = genTime;
   m_instances = instances;

   //create rule body
   premise =null;
   premise = new ItemSet(m_totalTransactions);
   int[] premiseItems = new int[m_items.length];
   System.arraycopy(m_items, 0, premiseItems, 0, m_items.length);
   premise.setItem(premiseItems);
   premise.setCounter(m_counter);

   consequences = singleConsequence(instances);

   //create n best rules
   do{
     if(premise == null || consequences.size() == 0)
return m_best;
     m_minRuleCount = 1;
     while(expectation((double)m_minRuleCount,premise.counter(),m_midPoints,m_priors) <= m_expectation){
m_minRuleCount++;
if(m_minRuleCount > premise.counter())
  return m_best;
     }
     redundant = false;

     //create possible heads  
     FastVector allRuleItems = new FastVector();
     int h = 0;
     while(h < consequences.size()){
RuleItem dummie = new RuleItem();
m_count++;
current = dummie.generateRuleItem(premise,(ItemSet)consequences.elementAt(h),instances,m_count,m_minRuleCount,m_midPoints,m_priors);
if(current != null)
  allRuleItems.addElement(current);
h++;
     }

     //update best
     for(h =0; h< allRuleItems.size();h++){
current = (RuleItem)allRuleItems.elementAt(h);
if(m_best.size() < numRules){
  m_change =true;
  redundant = removeRedundant(current);  
}
else{
  m_expectation = ((RuleItem)(m_best.first())).accuracy();
  if(current.accuracy() > m_expectation){
    boolean remove = m_best.remove(m_best.first());
    m_change = true;
    redundant = removeRedundant(current);
    m_expectation = ((RuleItem)(m_best.first())).accuracy();
    while(expectation((double)m_minRuleCount, (current.premise()).counter(),m_midPoints,m_priors) < m_expectation){
      m_minRuleCount++;
      if(m_minRuleCount > (current.premise()).counter())
	break;
    } 
  }  
}   
     }   
   }while(redundant); 
   return m_best;
 }
 
Example 20
Source File: CostCurve.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Calculates the performance stats for the default class and return 
 * results as a set of Instances. The
 * structure of these Instances is as follows:<p> <ul> 
 * <li> <b>Probability Cost Function </b>
 * <li> <b>Normalized Expected Cost</b>
 * <li> <b>Threshold</b> contains the probability threshold that gives
 * rise to the previous performance values. 
 * </ul> <p>
 *
 * @see TwoClassStats
 * @param predictions the predictions to base the curve on
 * @return datapoints as a set of instances, null if no predictions
 * have been made.
 */
public Instances getCurve(FastVector predictions) {

  if (predictions.size() == 0) {
    return null;
  }
  return getCurve(predictions, 
                  ((NominalPrediction)predictions.elementAt(0))
                  .distribution().length - 1);
}