Improved code style
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
parent
e1c54fd15b
commit
64fd37e53d
|
@ -1,24 +1,19 @@
|
|||
package blatt8;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class Apriori {
|
||||
|
||||
|
||||
static List<int[]> itemsets = new ArrayList<int[]>();
|
||||
static float minSup = 0.01f;
|
||||
static String path = "transactions.txt";
|
||||
static int numItems=0;
|
||||
static int numTransactions=0;
|
||||
private static List<int[]> itemsets = new ArrayList<int[]>();
|
||||
private static float minSup = 0.01f;
|
||||
private static String path = "transactions.txt";
|
||||
private static int numItems = 0;
|
||||
private static int numTransactions = 0;
|
||||
|
||||
private static void analyzeTransactionFile() {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
||||
|
@ -26,14 +21,13 @@ public class Apriori {
|
|||
while ((line = br.readLine()) != null) {
|
||||
numTransactions++;
|
||||
String[] products = line.split("\\s+");
|
||||
for(int i=0;i<products.length;i++){
|
||||
if(Integer.parseInt(products[i])+1>numItems){
|
||||
numItems = Integer.parseInt(products[i])+1;
|
||||
for (String product : products) {
|
||||
if (Integer.parseInt(product) + 1 > numItems) {
|
||||
numItems = Integer.parseInt(product) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(IOException e){
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Num Transactions:" + numTransactions);
|
||||
|
@ -46,8 +40,7 @@ public class Apriori {
|
|||
//Calculate L1
|
||||
System.out.println(k + "-Itemsets");
|
||||
find_frequent_1_itemsets();
|
||||
for (k=2;itemsets.size()>0;k++)
|
||||
{
|
||||
for (k = 2; itemsets.size() > 0; k++) {
|
||||
System.out.println("\n" + k + "-Itemsets");
|
||||
generateCandidates();
|
||||
|
||||
|
@ -59,26 +52,20 @@ public class Apriori {
|
|||
private static void generateCandidates() {
|
||||
int k = itemsets.get(0).length + 1;
|
||||
List<int[]> candidates = new ArrayList<int[]>();
|
||||
for(int i=0; i<itemsets.size(); i++)
|
||||
{
|
||||
for(int j=0; j<itemsets.size(); j++)
|
||||
{
|
||||
for (int i = 0; i < itemsets.size(); i++) {
|
||||
for (int[] itemset : itemsets) {
|
||||
int[] I1 = itemsets.get(i);
|
||||
int[] I2 = itemsets.get(j);
|
||||
boolean match = true;
|
||||
for (int l = 0; l < k - 2; l++) {
|
||||
if(I1[l]!=I2[l]){
|
||||
if (I1[l] != itemset[l]) {
|
||||
match = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
if(I1[k-2]<I2[k-2]){
|
||||
if (I1[k - 2] < itemset[k - 2]) {
|
||||
int[] c = new int[k];
|
||||
for(int l=0;l<k-1;l++){
|
||||
c[l]=I1[l];
|
||||
}
|
||||
c[k-1]=I2[k-2];
|
||||
System.arraycopy(I1, 0, c, 0, k - 1);
|
||||
c[k - 1] = itemset[k - 2];
|
||||
if (!prune(c)) {
|
||||
candidates.add(c);
|
||||
}
|
||||
|
@ -87,8 +74,11 @@ public class Apriori {
|
|||
}
|
||||
}
|
||||
itemsets = new ArrayList<int[]>(candidates);
|
||||
if(itemsets.size()>0)System.out.println(itemsets.size()+" Candidates for k="+itemsets.get(0).length);
|
||||
else System.out.println("0 Candidates");
|
||||
if (itemsets.size() > 0) {
|
||||
System.out.println(itemsets.size() + " Candidates for k=" + itemsets.get(0).length);
|
||||
} else {
|
||||
System.out.println("0 Candidates");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -119,10 +109,10 @@ public class Apriori {
|
|||
}
|
||||
|
||||
private static boolean contains(int[] a, int[] b) {
|
||||
for(int j=0;j<b.length;j++){
|
||||
for (int aB : b) {
|
||||
boolean match = false;
|
||||
for(int i=0;i<a.length;i++){
|
||||
if(b[j]==a[i]){
|
||||
for (int anA : a) {
|
||||
if (aB == anA) {
|
||||
match = true;
|
||||
}
|
||||
}
|
||||
|
@ -142,8 +132,7 @@ public class Apriori {
|
|||
|
||||
private static void find_frequent_1_itemsets() {
|
||||
itemsets = new ArrayList<int[]>();
|
||||
for(int i=0; i<numItems; i++)
|
||||
{
|
||||
for (int i = 0; i < numItems; i++) {
|
||||
int[] cand = {i};
|
||||
itemsets.add(cand);
|
||||
}
|
||||
|
@ -151,8 +140,7 @@ public class Apriori {
|
|||
calculateFrequentItemsets();
|
||||
}
|
||||
|
||||
private static void calculateFrequentItemsets()
|
||||
{
|
||||
private static void calculateFrequentItemsets() {
|
||||
List<int[]> frequent = new ArrayList<int[]>();
|
||||
int[] counter = new int[itemsets.size()];
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
||||
|
@ -169,8 +157,7 @@ public class Apriori {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(IOException e){
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i < counter.length; i++) {
|
||||
|
@ -181,15 +168,18 @@ public class Apriori {
|
|||
}
|
||||
}
|
||||
itemsets = frequent;
|
||||
if(itemsets.size()>0)System.out.println("Total Number Frequent "+itemsets.get(0).length+"-Itemsets: "+itemsets.size());
|
||||
else System.out.println("0 Frequent");
|
||||
if (itemsets.size() > 0) {
|
||||
System.out.println("Total Number Frequent " + itemsets.get(0).length + "-Itemsets: " + itemsets.size());
|
||||
} else {
|
||||
System.out.println("0 Frequent");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void printItemset(int[] is) {
|
||||
String print="";
|
||||
for(int i=0;i<is.length;i++){
|
||||
print+=is[i]+" - ";
|
||||
StringBuilder print = new StringBuilder();
|
||||
for (int i1 : is) {
|
||||
print.append(i1).append(" - ");
|
||||
}
|
||||
System.out.println(print);
|
||||
}
|
||||
|
@ -198,8 +188,6 @@ public class Apriori {
|
|||
public static void main(String[] args) {
|
||||
analyzeTransactionFile();
|
||||
AprioriAlgorithm();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue