?? assocrulemining.java
字號:
1) For each record in the data array. Create an empty new itemSet array.
2) Place into this array any column numbers in record that are
supported at the index contained in the conversion array.
3) Assign new itemSet back into to data array */
public void recastInputDataAndPruneUnsupportedAtts() {
short[] itemSet;
int attribute;
// Step through data array using loop construct
for(int rowIndex=0;rowIndex<dataArray.length;rowIndex++) {
// Check for empty row
if (dataArray[rowIndex]!= null) {
itemSet = null;
// For each element in the current record find if supported with
// reference to the conversion array. If so add to "itemSet".
for(int colIndex=0;colIndex<dataArray[rowIndex].length;colIndex++) {
attribute = dataArray[rowIndex][colIndex];
// Check support
if (conversionArray[attribute][1] >= minSupport) {
itemSet = reallocInsert(itemSet,
(short) conversionArray[attribute][0]);
}
}
// Return new item set to data array
dataArray[rowIndex] = itemSet;
}
}
// Set isPrunedFlag (used with GUI interface)
isPrunedFlag=true;
// Reset number of one item sets field
numOneItemSets = getNumSupOneItemSets();
}
/* GET NUM OF SUPPORTE ONE ITEM SETS */
/** Gets number of supported single item sets (note this is not necessarily
the same as the number of columns/attributes in the input set).
@return Number of supported 1-item sets */
protected int getNumSupOneItemSets() {
int counter = 0;
// Step through conversion array incrementing counter for each
// supported element found
for (int index=1;index < conversionArray.length;index++) {
if (conversionArray[index][1] >= minSupport) counter++;
}
// Return
return(counter);
}
/* RESIZE INPUT DATA */
/** Recasts the input data sets so that only N percent is used.
@param percentage the percentage of the current input data that is to form
the new input data set (number between 0 and 100). */
public void resizeInputData(double percentage) {
// Redefine number of rows
numRows = (int) ((double) numRows*(percentage/100.0));
System.out.println("Recast input data, new num rows = " + numRows);
// Dimension and populate training set.
short[][] trainingSet = new short[numRows][];
for (int index=0;index<numRows;index++)
trainingSet[index] = dataArray[index];
// Assign training set label to input data set label.
dataArray = trainingSet;
// Determine new minimum support threshold value
minSupport = (numRows * support)/100.0;
}
/** Reconverts given item set according to contents of reconversion array.
@param itemSet the fgiven itemset.
@return the reconverted itemset. */
protected short[] reconvertItemSet(short[] itemSet) {
// If no conversion return orginal item set
if (reconversionArray==null) return(itemSet);
// If item set null return null
if (itemSet==null) return(null);
// Define new item set
short[] newItemSet = new short[itemSet.length];
// Copy
for(int index=0;index<newItemSet.length;index++) {
newItemSet[index] = reconversionArray[itemSet[index]];
}
// Return
return(newItemSet);
}
/** Reconvert single item if appropriate.
@param item the given item (attribute).
@return the reconvered item. */
protected short reconvertItem(short item) {
// If no conversion return orginal item
if (reconversionArray==null) return(item);
// Otherwise rerturn reconvert item
return(reconversionArray[item]);
}
/* -------------------------------------------------------------- */
/* */
/* RULE LINKED LIST ORDERED ACCORDING TO CONFIDENCE */
/* */
/* -------------------------------------------------------------- */
/* Methods for inserting rules into a linked list of rules ordered
according to confidence (most confident first). Each rule described in
terms of 3 fields: 1) Antecedent (an item set), 2) a consequent (an item
set), 3) a confidence value (double). <P> The support field is not used. */
/* INSERT (ASSOCIATION/CLASSIFICATION) RULE INTO RULE LINKED LIST (ORDERED
ACCORDING CONFIDENCE). */
/** Inserts an (association/classification) rule into the linkedlist of
rules pointed at by <TT>startRulelist</TT>. <P> The list is ordered so that
rules with highest confidence are listed first. If two rules have the same
confidence the new rule will be placed after the existing rule. Thus, if
using an Apriori approach to generating rules, more general rules will
appear first in the list with more specific rules (i.e. rules with a larger
antecedent) appearing later as the more general rules will be generated
first.
@param antecedent the antecedent (LHS) of the rule.
@param consequent the consequent (RHS) of the rule.
@param confidenceForRule the associated confidence value. */
protected void insertRuleintoRulelist(short[] antecedent,
short[] consequent, double confidenceForRule) {
// Create new node
RuleNode newNode = new RuleNode(antecedent,consequent,
confidenceForRule);
// Empty list situation
if (startRulelist == null) {
startRulelist = newNode;
return;
}
// Add new node to start
if (confidenceForRule > startRulelist.confidenceForRule) {
newNode.next = startRulelist;
startRulelist = newNode;
return;
}
// Add new node to middle
RuleNode markerNode = startRulelist;
RuleNode linkRuleNode = startRulelist.next;
while (linkRuleNode != null) {
if (confidenceForRule > linkRuleNode.confidenceForRule) {
markerNode.next = newNode;
newNode.next = linkRuleNode;
return;
}
markerNode = linkRuleNode;
linkRuleNode = linkRuleNode.next;
}
// Add new node to end
markerNode.next = newNode;
}
/* ----------------------------------------------- */
/* */
/* ITEM SET INSERT AND ADD METHODS */
/* */
/* ----------------------------------------------- */
/* REALLOC INSERT */
/** Resizes given item set so that its length is increased by one
and new element inserted.
@param oldItemSet the original item set
@param newElement the new element/attribute to be inserted
@return the combined item set */
protected short[] reallocInsert(short[] oldItemSet, short newElement) {
// No old item set
if (oldItemSet == null) {
short[] newItemSet = {newElement};
return(newItemSet);
}
// Otherwise create new item set with length one greater than old
// item set
int oldItemSetLength = oldItemSet.length;
short[] newItemSet = new short[oldItemSetLength+1];
// Loop
int index1;
for (index1=0;index1 < oldItemSetLength;index1++) {
if (newElement < oldItemSet[index1]) {
newItemSet[index1] = newElement;
// Add rest
for(int index2 = index1+1;index2<newItemSet.length;index2++)
newItemSet[index2] = oldItemSet[index2-1];
return(newItemSet);
}
else newItemSet[index1] = oldItemSet[index1];
}
// Add to end
newItemSet[newItemSet.length-1] = newElement;
// Return new item set
return(newItemSet);
}
/* REALLOC 1 */
/** Resizes given item set so that its length is increased by one
and appends new element (identical to append method)
@param oldItemSet the original item set
@param newElement the new element/attribute to be appended
@return the combined item set */
protected short[] realloc1(short[] oldItemSet, short newElement) {
// No old item set
if (oldItemSet == null) {
short[] newItemSet = {newElement};
return(newItemSet);
}
// Otherwise create new item set with length one greater than old
// item set
int oldItemSetLength = oldItemSet.length;
short[] newItemSet = new short[oldItemSetLength+1];
// Loop
int index;
for (index=0;index < oldItemSetLength;index++)
newItemSet[index] = oldItemSet[index];
newItemSet[index] = newElement;
// Return new item set
return(newItemSet);
}
/* REALLOC 2 */
/** Resizes given array so that its length is increased by one element
and new element added to front
@param oldItemSet the original item set
@param newElement the new element/attribute to be appended
@return the combined item set */
protected short[] realloc2(short[] oldItemSet, short newElement) {
// No old array
if (oldItemSet == null) {
short[] newItemSet = {newElement};
return(newItemSet);
}
// Otherwise create new array with length one greater than old array
int oldItemSetLength = oldItemSet.length;
short[] newItemSet = new short[oldItemSetLength+1];
// Loop
newItemSet[0] = newElement;
for (int index=0;index < oldItemSetLength;index++)
newItemSet[index+1] = oldItemSet[index];
// Return new array
return(newItemSet);
}
/* --------------------------------------------- */
/* */
/* ITEM SET DELETE METHODS */
/* */
/* --------------------------------------------- */
/* REMOVE ELEMENT N */
/** Removes the nth element/attribute from the given item set.
@param oldItemSet the given item set.
@param n the index of the element to be removed (first index is 0).
@return Revised item set with nth element removed. */
protected short[] removeElementN(short [] oldItemSet, int n) {
if (oldItemSet.length <= n) return(oldItemSet);
else {
short[] newItemSet = new short[oldItemSet.length-1];
for (int index=0;index<n;index++) newItemSet[index] =
oldItemSet[index];
for (int index=n+1;index<oldItemSet.length;index++)
newItemSet[index-1] = oldItemSet[index];
return(newItemSet);
}
}
/* ---------------------------------------------------------------- */
/* */
/* METHODS TO RETURN SUBSETS OF ITEMSETS */
/* */
/* ---------------------------------------------------------------- */
/* COMPLEMENT */
/** Returns complement of first itemset with respect to second itemset.
@param itemSet1 the first given item set.
@param itemSet2 the second given item set.
@return complement if <TT>itemSet1</TT> in <TT>itemSet2</TT>. */
protected short[] complement(short[] itemSet1, short[] itemSet2) {
int lengthOfComp = itemSet2.length-itemSet1.length;
// Return null if no complement
if (lengthOfComp<1) return(null);
// Otherwsise define combination array and determine complement
short[] complement = new short[lengthOfComp];
int complementIndex = 0;
for(int index=0;index<itemSet2.length;index++) {
// Add to combination if not in first itemset
if (notMemberOf(itemSet2[index],itemSet1)) {
complement[complementIndex] = itemSet2[index];
complementIndex++;
}
}
// Return
return(complement);
}
/* --------------------------------------- */
/* */
/* SORT ITEM SET */
/* */
/* --------------------------------------- */
/* SORT ITEM SET: Given an unordered itemSet, sort the set */
/** Sorts an unordered item set.
@param itemSet the given item set. */
protected void sortItemSet(short[] itemSet) {
short temp;
boolean isOrdered;
int index;
do {
isOrdered = true;
index = 0;
while (index < (itemSet.length-1)) {
if (itemSet[index] <= itemSet[index+1]) index++;
else {
isOrdered=false;
// Swap
temp = itemSet[index];
itemSet[index] = itemSet[index+1];
itemSet[index+1] = temp;
// Increment index
index++;
}
}
} while (isOrdered==false);
}
/* ----------------------------------------------------- */
/* */
/* BOOLEAN ITEM SET METHODS ETC. */
/* */
/* ----------------------------------------------------- */
/* NOT MEMBER OF */
/** Checks whether a particular element/attribute identified by a
column number is not a member of the given item set.
@param number the attribute identifier (column number).
@param itemSet the given item set.
@return true if first argument is not a member of itemSet, and false
otherwise */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -