Improved code style
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
parent
e1c54fd15b
commit
64fd37e53d
|
@ -1,205 +1,193 @@
|
||||||
package blatt8;
|
package blatt8;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class Apriori {
|
public class Apriori {
|
||||||
|
|
||||||
|
|
||||||
static List<int[]> itemsets = new ArrayList<int[]>();
|
private static List<int[]> itemsets = new ArrayList<int[]>();
|
||||||
static float minSup = 0.01f;
|
private static float minSup = 0.01f;
|
||||||
static String path = "transactions.txt";
|
private static String path = "transactions.txt";
|
||||||
static int numItems=0;
|
private static int numItems = 0;
|
||||||
static int numTransactions=0;
|
private static int numTransactions = 0;
|
||||||
|
|
||||||
private static void analyzeTransactionFile(){
|
private static void analyzeTransactionFile() {
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
numTransactions++;
|
numTransactions++;
|
||||||
String[] products = line.split("\\s+");
|
String[] products = line.split("\\s+");
|
||||||
for(int i=0;i<products.length;i++){
|
for (String product : products) {
|
||||||
if(Integer.parseInt(products[i])+1>numItems){
|
if (Integer.parseInt(product) + 1 > numItems) {
|
||||||
numItems = Integer.parseInt(products[i])+1;
|
numItems = Integer.parseInt(product) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
System.out.println("Num Transactions:" + numTransactions);
|
||||||
System.out.println("Num Transactions:"+numTransactions);
|
System.out.println("Num Items:" + numItems);
|
||||||
System.out.println("Num Items:"+numItems);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private static void AprioriAlgorithm() {
|
||||||
private static void AprioriAlgorithm(){
|
int k = 1;
|
||||||
int k=1;
|
//Calculate L1
|
||||||
//Calculate L1
|
System.out.println(k + "-Itemsets");
|
||||||
System.out.println(k+"-Itemsets");
|
find_frequent_1_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");
|
||||||
{
|
|
||||||
System.out.println("\n"+k+"-Itemsets");
|
|
||||||
generateCandidates();
|
generateCandidates();
|
||||||
|
|
||||||
calculateFrequentItemsets();
|
calculateFrequentItemsets();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void generateCandidates() {
|
private static void generateCandidates() {
|
||||||
int k = itemsets.get(0).length+1;
|
int k = itemsets.get(0).length + 1;
|
||||||
List<int[]> candidates = new ArrayList<int[]>();
|
List<int[]> candidates = new ArrayList<int[]>();
|
||||||
for(int i=0; i<itemsets.size(); i++)
|
for (int i = 0; i < itemsets.size(); i++) {
|
||||||
{
|
for (int[] itemset : itemsets) {
|
||||||
for(int j=0; j<itemsets.size(); j++)
|
int[] I1 = itemsets.get(i);
|
||||||
{
|
boolean match = true;
|
||||||
int[] I1 = itemsets.get(i);
|
for (int l = 0; l < k - 2; l++) {
|
||||||
int[] I2 = itemsets.get(j);
|
if (I1[l] != itemset[l]) {
|
||||||
boolean match=true;
|
match = false;
|
||||||
for(int l=0;l<k-2;l++){
|
}
|
||||||
if(I1[l]!=I2[l]){
|
|
||||||
match = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(match){
|
if (match) {
|
||||||
if(I1[k-2]<I2[k-2]){
|
if (I1[k - 2] < itemset[k - 2]) {
|
||||||
int[] c = new int[k];
|
int[] c = new int[k];
|
||||||
for(int l=0;l<k-1;l++){
|
System.arraycopy(I1, 0, c, 0, k - 1);
|
||||||
c[l]=I1[l];
|
c[k - 1] = itemset[k - 2];
|
||||||
}
|
if (!prune(c)) {
|
||||||
c[k-1]=I2[k-2];
|
candidates.add(c);
|
||||||
if(!prune(c)){
|
}
|
||||||
candidates.add(c);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
itemsets=new ArrayList<int[]>(candidates);
|
itemsets = new ArrayList<int[]>(candidates);
|
||||||
if(itemsets.size()>0)System.out.println(itemsets.size()+" Candidates for k="+itemsets.get(0).length);
|
if (itemsets.size() > 0) {
|
||||||
else System.out.println("0 Candidates");
|
System.out.println(itemsets.size() + " Candidates for k=" + itemsets.get(0).length);
|
||||||
}
|
} else {
|
||||||
|
System.out.println("0 Candidates");
|
||||||
|
|
||||||
private static boolean prune(int[] c) {
|
|
||||||
// if(new Random().nextInt(50)==0)return false;
|
|
||||||
return false;
|
|
||||||
// for(int skip=0;skip<c.length;skip++){
|
|
||||||
// int[] subset = new int[c.length-1];
|
|
||||||
// for(int i=0;i<c.length-1;i++){
|
|
||||||
// if(i>=skip)
|
|
||||||
// subset[i]=c[i+1];
|
|
||||||
// else
|
|
||||||
// subset[i]=c[i];
|
|
||||||
// }
|
|
||||||
// boolean match=false;
|
|
||||||
// for(int i=0;i<itemsets.size();i++){
|
|
||||||
// if(compare(itemsets.get(i),subset)){
|
|
||||||
// match=true;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if(!match)
|
|
||||||
// return true;
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean contains(int[] a, int[] b){
|
|
||||||
for(int j=0;j<b.length;j++){
|
|
||||||
boolean match=false;
|
|
||||||
for(int i=0;i<a.length;i++){
|
|
||||||
if(b[j]==a[i]){
|
|
||||||
match = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!match)return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean compare(int[] a, int[] b){
|
|
||||||
for(int i=0;i<a.length;i++){
|
|
||||||
if(a[i]!=b[i]){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void find_frequent_1_itemsets() {
|
|
||||||
itemsets = new ArrayList<int[]>();
|
|
||||||
for(int i=0; i<numItems; i++)
|
|
||||||
{
|
|
||||||
int[] cand = {i};
|
|
||||||
itemsets.add(cand);
|
|
||||||
}
|
}
|
||||||
System.out.println(itemsets.size()+" Candidates for k="+itemsets.get(0).length);
|
|
||||||
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))) {
|
|
||||||
String line;
|
|
||||||
while ((line = br.readLine()) != null) {
|
|
||||||
String[] products = line.split("\\s+");
|
|
||||||
int trans[] = new int[products.length];
|
|
||||||
for(int i=0;i<trans.length;i++){
|
|
||||||
trans[i]=Integer.parseInt(products[i]);
|
|
||||||
}
|
|
||||||
for(int cand=0;cand<itemsets.size();cand++){
|
|
||||||
if(contains(trans,itemsets.get(cand))){
|
|
||||||
counter[cand]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
for(int i=0;i<counter.length;i++){
|
|
||||||
|
|
||||||
if((float)counter[i]/(float)numTransactions>=minSup){
|
|
||||||
frequent.add(itemsets.get(i));
|
|
||||||
printItemset(itemsets.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printItemset(int[] is) {
|
|
||||||
String print="";
|
private static boolean prune(int[] c) {
|
||||||
for(int i=0;i<is.length;i++){
|
// if(new Random().nextInt(50)==0)return false;
|
||||||
print+=is[i]+" - ";
|
return false;
|
||||||
}
|
// for(int skip=0;skip<c.length;skip++){
|
||||||
System.out.println(print);
|
// int[] subset = new int[c.length-1];
|
||||||
}
|
// for(int i=0;i<c.length-1;i++){
|
||||||
|
// if(i>=skip)
|
||||||
|
// subset[i]=c[i+1];
|
||||||
public static void main(String[] args) {
|
// else
|
||||||
analyzeTransactionFile();
|
// subset[i]=c[i];
|
||||||
AprioriAlgorithm();
|
// }
|
||||||
|
// boolean match=false;
|
||||||
|
// for(int i=0;i<itemsets.size();i++){
|
||||||
}
|
// if(compare(itemsets.get(i),subset)){
|
||||||
|
// match=true;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if(!match)
|
||||||
|
// return true;
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean contains(int[] a, int[] b) {
|
||||||
|
for (int aB : b) {
|
||||||
|
boolean match = false;
|
||||||
|
for (int anA : a) {
|
||||||
|
if (aB == anA) {
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!match) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean compare(int[] a, int[] b) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
if (a[i] != b[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void find_frequent_1_itemsets() {
|
||||||
|
itemsets = new ArrayList<int[]>();
|
||||||
|
for (int i = 0; i < numItems; i++) {
|
||||||
|
int[] cand = {i};
|
||||||
|
itemsets.add(cand);
|
||||||
|
}
|
||||||
|
System.out.println(itemsets.size() + " Candidates for k=" + itemsets.get(0).length);
|
||||||
|
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))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
String[] products = line.split("\\s+");
|
||||||
|
int trans[] = new int[products.length];
|
||||||
|
for (int i = 0; i < trans.length; i++) {
|
||||||
|
trans[i] = Integer.parseInt(products[i]);
|
||||||
|
}
|
||||||
|
for (int cand = 0; cand < itemsets.size(); cand++) {
|
||||||
|
if (contains(trans, itemsets.get(cand))) {
|
||||||
|
counter[cand]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
for (int i = 0; i < counter.length; i++) {
|
||||||
|
|
||||||
|
if ((float) counter[i] / (float) numTransactions >= minSup) {
|
||||||
|
frequent.add(itemsets.get(i));
|
||||||
|
printItemset(itemsets.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printItemset(int[] is) {
|
||||||
|
StringBuilder print = new StringBuilder();
|
||||||
|
for (int i1 : is) {
|
||||||
|
print.append(i1).append(" - ");
|
||||||
|
}
|
||||||
|
System.out.println(print);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
analyzeTransactionFile();
|
||||||
|
AprioriAlgorithm();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue