Monday, March 30, 2009

/* COUNT SINGLES */


/** Counts number of occurrences of each single attribute in the
input data.
@return 2-D array where first row represents column numbers
and second row represents support counts. */

protected int[][] countSingles() {
// Dimension and initialize count array
int[][] countArray = new int[numCols+1][2];
for (int index=0;index countArray[index][0] = index;
countArray[index][1] = 0;
}
// Step through input data array counting singles and incrementing
// appropriate element in the count array
for(int rowIndex=0;rowIndex if (dataArray[rowIndex] != null) {
for (int colIndex=0;colIndex countArray[dataArray[rowIndex][colIndex]][1]++;
}
}
// Return
return(countArray);
}

/** sorts count array produced by countSingles method
@param countArray The 2-D array returned by the countSingles
method. */

private void orderCountArray(int[][] countArray) {
int attribute, quantity;
boolean isOrdered;
int index;
do {
isOrdered = true;
index = 1;
while (index < (countArray.length-1)) {
if (countArray[index][1] >= countArray[index+1][1]) index++;
else {
isOrdered=false;
// Swap
attribute = countArray[index][0];
quantity = countArray[index][1];
countArray[index][0] = countArray[index+1][0];
countArray[index][1] = countArray[index+1][1];
countArray[index+1][0] = attribute;
countArray[index+1][1] = quantity;
// Increment index
index++;
}
}
} while (isOrdered==false);
}


/** Defines conversion and reconversion arrays.
@param countArray The 2-D array sorted by the ordercountArray
method.*/

protected void defConvertArrays(int[][] countArray) {
// Dimension arrays
conversionArray = new int[numCols+1][2];
reconversionArray = new short[numCols+1];
// Assign values
for(int index=1;index conversionArray[countArray[index][0]][0] = index;
conversionArray[countArray[index][0]][1] = countArray[index][1];
reconversionArray[index] = (short) countArray[index][0];
}

}



/** Recasts the contents of the data array so that each record is
ordered according to ColumnCounts array and excludes non-supported
elements.*/

public void prune() {
short[] itemSet;
int attribute;
// Step through data array using loop construct
for(int rowIndex=0;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 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;
}
}

isPrunedFlag=true;
// Reset number of one item sets field
numOneItemSets = getNumSupOneItemSets();
}

/** Sets the number of one item sets field (numOneItemSets to
the number of supported one item sets. */

public void setNumOneItemSets() {
numOneItemSets=getNumSupOneItemSets();
}

/** Gets number of supported single item sets .
@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);
}

/** Reconverts given item set according to contents of reconversion array.
@param itemSet the given 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[index] = reconversionArray[itemSet[index]];
}
// Return
return(newItemSet);
}

/** Reconvert single item if appropriate.
@param item the given item
@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]);
}

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

/** Resizes given item set so that its length is increased by one
and appends new element
@param oldItemSet the original item set
@param newElement the new 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);
}

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

/** Removes the nth attribute from the given item set.
@param oldItemSet the given item set.
@param n the index of the element to be removed .
@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 for (int index=n+1;index newItemSet[index-1] = oldItemSet[index];
return(newItemSet);
}
}


/** Given an unordered itemSet, sort the 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);
}



/** Invokes combinations method to calculate all possible
combinations of a given item set.
@param inputSet the given item set.
@return array of arrays representing all possible combinations (may be null
if no combinations). */

protected short[][] combinations(short[] inputSet) {
if (inputSet == null) return(null);
else {
short[][] outputSet = new short[getCombinations(inputSet)][];
combinations(inputSet,0,null,outputSet,0);
return(outputSet);
}
}

/** Recursively calculates all possible combinations of a given item
set.
@param inputSet the given item set.
@param inputIndex the index within the input set marking current
element under consideration .
@param sofar the part of a combination determined sofar during the
recursion .
@param outputSet the combinations collected so far, will hold all
combinations when recursion ends.
@param outputIndex the current location in the output set.
@return revised output index. */

private int combinations(short[] inputSet, int inputIndex, short[] sofar, short[][] outputSet, int outputIndex) {
short[] tempSet;
int index=inputIndex;
// Loop through input array
while(index < inputSet.length) {
tempSet = realloc1(sofar,inputSet[index]);
outputSet[outputIndex] = tempSet;
outputIndex = combinations(inputSet,index+1,copyItemSet(tempSet),outputSet,outputIndex+1);
index++;
}
// Return
return(outputIndex);
}

/** Gets the number of possible combinations of a given item set.
@param set the given item set.
@return number of possible combinations. */

private int getCombinations(short[] set) {
int counter=0, numComb;
numComb = (int) Math.pow(2.0,set.length)-1;
// Return
return(numComb);
}

/** Makes a copy of a given itemSet.
@param itemSet the given item set.
@return copy of given item set. */

protected short[] copyItemSet(short[] itemSet) {
// Check whether there is a itemSet to copy
if (itemSet == null) return(null);
// Do copy and return
short[] newItemSet = new short[itemSet.length];
for(int index=0;index newItemSet[index] = itemSet[index];
}
// Return
return(newItemSet);
}

/** Outputs stored input data set; initially read from input data file, but
may be reordered or pruned if desired by a particular application. */

public void outputDataArray() {
if (isPrunedFlag) System.out.println("DATA SET (Ordered and Pruned)\n" +
"-----------------------------");
else {
if (isOrderedFlag) System.out.println("DATA SET (Ordered)\n" +
"------------------");
else System.out.println("DATA SET\n" + "--------");
}
// Loop through data array
for(int index=0;index outputItemSet(dataArray[index]);
System.out.println();
}
}

/** Outputs a given item set.
@param itemSet the given item set. */

protected void outputItemSet(short[] itemSet) {
// Check for empty set
if (itemSet == null) System.out.print(" null ");
// Process
else {
// Reconvert where input dataset has been reordered and possible
// pruned.
short[] tempItemSet = reconvertItemSet(itemSet);
// Loop through item set elements
int counter = 0;
for (int index=0;index if (counter == 0) {
counter++;
System.out.print(" {");
}
else System.out.print(" ");
System.out.print(tempItemSet[index]);
}
System.out.print("} ");
}
}


/** Outputs conversion array */
public void outputConversionArrays() {
System.out.println("Conversion Array = ");
for(int index=1;index System.out.println("(" + index + ") " + conversionArray[index][0] +
" = " + conversionArray[index][1]);
}
// Reconversion array
System.out.println("Reonversion Array = ");
for(int index=1;index System.out.println("(" + index + ") " + reconversionArray[index]);
}
}

protected void outputMenu() {
System.out.println();
System.out.println("-C = Confidence (default 80%)");
System.out.println("-F = File name");
System.out.println("-N = Number of classes (Optional)");
System.out.println("-S = Support (default 20%)");
System.out.println();
// Exit
System.exit(1);
}

/** Outputs command line values provided by user. */

protected void outputSettings() {
System.out.println("SETTINGS\n--------");
System.out.println("File name = " + fileName);
System.out.println("Support (default 20%) = " + support);
System.out.println("Confidence (default 80%) = " + confidence);
System.out.println();
}


/** Outputs current support and confidence settings. */

public void outputSuppAndConf() {
System.out.println("Support = " + twoDecPlaces(support) + " ,Confidence="+twoDecPlaces(confidence));
}

No comments:





my traffic rate




Hire Me Direct
 
ss_blog_claim=47b8cd0f86a684cfdfc7f370edf4619d