⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 itemset.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    conv.addElement(new Double(convictionForRule(premise, consequence,
				       premise.m_counter,
				       consequenceUnconditionedCounter)));
	  }
	} else {
	  double tempConf = confidenceForRule(premise, consequence);
	  double tempLift = liftForRule(premise, consequence, 
					consequenceUnconditionedCounter);
	  double tempLev = leverageForRule(premise, consequence,
					   premise.m_counter,
					   consequenceUnconditionedCounter);
	  double tempConv = convictionForRule(premise, consequence,
					      premise.m_counter,
					      consequenceUnconditionedCounter);
	  switch(metricType) {
	  case 1: 
	    metric = tempLift;
	    break;
	  case 2:
	    metric = tempLev;
	    break;
	  case 3: 
	    metric = tempConv;
	    break;
	  default:
	    throw new Exception("ItemSet: Unknown metric type!");
	  }
	  if (!(metric < minMetric)) {
	    premises.addElement(premise);
	    consequences.addElement(consequence);
	    conf.addElement(new Double(tempConf));
	    lift.addElement(new Double(tempLift));
	    lev.addElement(new Double(tempLev));
	    conv.addElement(new Double(tempConv));
	  }
	}
      }
    }
    rules[0] = premises;
    rules[1] = consequences;
    rules[2] = conf;
    rules[3] = lift;
    rules[4] = lev;
    rules[5] = conv;
    return rules;
  }

  /**
   * 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;
  }

  /**
   * Produces a hash code for a item set.
   *
   * @return a hash code for a set of items
   */
  public final int hashCode() {

    long result = 0;

    for (int i = m_items.length-1; i >= 0; i--)
      result += (i * m_items[i]);
    return (int)result;
  }

  /**
   * 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)
   * @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 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;
  }

  /**
   * 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;
  }

  /**
   * Prunes a set of rules.
   *
   * @param rules a two-dimensional array of lists of item sets. The first list
   * of item sets contains the premises, the second one the consequences.
   * @param minConfidence the minimum confidence the rules have to have
   */
  public static void pruneRules(FastVector[] rules, double minConfidence) {

    FastVector newPremises = new FastVector(rules[0].size()),
      newConsequences = new FastVector(rules[1].size()),
      newConf = new FastVector(rules[2].size());

    for (int i = 0; i < rules[0].size(); i++) 
      if (!(((Double)rules[2].elementAt(i)).doubleValue() <
	    minConfidence)) {
	newPremises.addElement(rules[0].elementAt(i));
	newConsequences.addElement(rules[1].elementAt(i));
	newConf.addElement(rules[2].elementAt(i));
      }
    rules[0] = newPremises;
    rules[1] = newConsequences;
    rules[2] = newConf;
  }

  /**
   * Converts the header info of the given set of instances into a set 
   * of item sets (singletons). The ordering of values in the header file 
   * determines the lexicographic order.
   *
   * @param instances the set of instances whose header info is to be used
   * @return a set of item sets, each containing a single item
   * @exception Exception if singletons can't be generated successfully
   */
  public static FastVector singletons(Instances instances) throws Exception {

    FastVector setOfItemSets = new FastVector();
    ItemSet current;

    for (int i = 0; i < instances.numAttributes(); i++) {
      if (instances.attribute(i).isNumeric())
	throw new Exception("Can't handle numeric attributes!");
      for (int j = 0; j < instances.attribute(i).numValues(); j++) {
	current = new ItemSet(instances.numInstances());
	current.m_items = new int[instances.numAttributes()];
	for (int k = 0; k < instances.numAttributes(); k++)
	  current.m_items[k] = -1;
	current.m_items[i] = j;
	setOfItemSets.addElement(current);
      }
    }
    return setOfItemSets;
  }
  
  /**
   * Subtracts an item set from another one.
   *
   * @param toSubtract the item set to be subtracted from this one.
   * @return an item set that only contains items form this item sets that
   * are not contained by toSubtract
   */
  public final ItemSet subtract(ItemSet toSubtract) {

    ItemSet result = new ItemSet(m_totalTransactions);
    
    result.m_items = new int[m_items.length];
    for (int i = 0; i < m_items.length; i++) 
      if (toSubtract.m_items[i] == -1)
	result.m_items[i] = m_items[i];
      else
	result.m_items[i] = -1;
    result.m_counter = 0;
    return result;
  }

  /**
   * Outputs the support for an item set.
   *
   * @return the support
   */
  public final int support() {

    return m_counter;
  }

  /**
   * Returns the contents of an item set as a string.
   *
   * @param instances contains the relevant header information
   * @return string describing the item set
   */
  public final String toString(Instances instances) {

    StringBuffer text = new StringBuffer();

    for (int i = 0; i < instances.numAttributes(); i++)
      if (m_items[i] != -1) {
	text.append(instances.attribute(i).name()+'=');
	text.append(instances.attribute(i).value(m_items[i])+' ');
      }
    text.append(m_counter);
    return text.toString();
  }

  /**
   * Updates counter of item set with respect to given transaction.
   *
   * @param instance the instance to be used for ubdating the counter
   */
  public final void upDateCounter(Instance instance) {

    if (containedBy(instance))
      m_counter++;
  }

  /**
   * 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 em = itemSets.elements();
      while (em.hasMoreElements()) 
	((ItemSet)em.nextElement()).upDateCounter(instances.instance(i));
    }
  }

  /**
   * Generates rules with more than one item in the consequence.
   *
   * @param rules all the rules having (k-1)-item sets as consequences
   * @param numItemsInSet the size of the item set for which the rules
   * are to be generated
   * @param numItemsInConsequence the value of (k-1)
   * @param minConfidence the minimum confidence a rule has to have
   * @param hashtables the hashtables containing all(!) previously generated
   * item sets
   * @return all the rules having (k)-item sets as consequences
   */
  private final FastVector[] moreComplexRules(FastVector[] rules, 
					      int numItemsInSet, 
					      int numItemsInConsequence,
					      double minConfidence, 
					      FastVector hashtables) {

    ItemSet newPremise;
    FastVector[] result, moreResults;
    FastVector newConsequences, newPremises = new FastVector(), 
      newConf = new FastVector();
    Hashtable hashtable;

    if (numItemsInSet > numItemsInConsequence + 1) {
      hashtable =
	(Hashtable)hashtables.elementAt(numItemsInSet - numItemsInConsequence - 2);
      newConsequences = mergeAllItemSets(rules[1], 
					 numItemsInConsequence - 1,
					 m_totalTransactions);
      Enumeration em = newConsequences.elements();
      while (em.hasMoreElements()) {
	ItemSet current = (ItemSet)em.nextElement();
	current.m_counter = m_counter;
	newPremise = subtract(current);
	newPremise.m_counter = ((Integer)hashtable.get(newPremise)).intValue();
	newPremises.addElement(newPremise);
	newConf.addElement(new Double(confidenceForRule(newPremise, current)));
      }
      result = new FastVector[3];
      result[0] = newPremises;
      result[1] = newConsequences;
      result[2] = newConf;
      pruneRules(result, minConfidence);
      moreResults = moreComplexRules(result,numItemsInSet,numItemsInConsequence+1,
				     minConfidence, hashtables);
      if (moreResults != null) 
	for (int i = 0; i < moreResults[0].size(); i++) {
	  result[0].addElement(moreResults[0].elementAt(i));
	  result[1].addElement(moreResults[1].elementAt(i));
	  result[2].addElement(moreResults[2].elementAt(i));
	}
      return result;
    } else
      return null;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -