Friday, 4 October 2013

DM FINDING MISSING VALUE CODE IN JAVA

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Finder {

/**
* @param args
*/
public static void main(String[] args) {
String csvFile = "E:/Documents and Settings/student/Desktop/sample data 1.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
String[] reader;
File file = new File("E:/Documents and Settings/student/Desktop/sample data without nulls.csv");
FileWriter fw = null;
int count=0;
try {
fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
        // use comma as separator
reader = line.split(cvsSplitBy);
//System.out.println("Age " + country[1]  + "Marital Status" + country[3]);
for(int i=0;i<5;i++){
if(reader[i].length()==0) {
reader[i]="missing";
count++;
}
}
bw.write(reader[0]+","+reader[1]+","+reader[2]+","+reader[3]+","+reader[4]+"\n");
}
bw.close();
System.out.println("Number of missing values="+count+"Done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

 finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 }
}
}


DM APRIORI ALGORITHM CODE IN JAVA


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class AprioriAlgo {

/**
* author: shwetha

* Execution:
* Enter Minimum Support.....
* 3
* Enter Transactions.....Bread,Milk
* Bread,Diaper,Beer,Eggs
* Milk,Diaper,Beer,Cola
* Bread,Milk,Diaper,Beer
* Bread,Milk,Diaper,Cola

*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> out=new ArrayList<String>();
ArrayList<String> dup=new ArrayList<String>();
ArrayList<String> mutex=new ArrayList<String>();
int n=1,y=0,cnt;
int[] len=new int[10];
int minsupport;
String str;
String[] temp,temp1;
String[][] splitstr = new String[10][10];
int x=0,k=0,z=0,p=0;
System.out.println("Enter Minimum Support.....");
minsupport=in.nextInt();
System.out.print("Enter Transactions.....");
//in.reset();
in.nextLine();
while((str=in.nextLine()).length()!=0){
temp=str.split(",");
len[y++]=temp.length;
for(int i=0;i<temp.length;i++){
splitstr[x][i]=temp[i];
if(!al.contains(temp[i]))
al.add(temp[i]);
out.add(temp[i]);
}
x++;
}
//minsupport=(int) ((x-1)*0.7);
//minsupport=3;
Collections.sort(al);
boolean test=true;
while(test){
System.out.println("\nCandidate "+n+" Item set");
System.out.print("\nItems\t Support");
out.clear();
for(String s:al){
temp=s.split(",");
System.out.print("\n"+s);
y=0;
for(int i=0;i<x;i++){
z=0;
for(int j=0;j<temp.length;j++){
for(k=0;k<len[i];k++){
if(temp[j].equals(splitstr[i][k])) z++;
}
}
if(z==temp.length)y++;
}
if(y>=minsupport){
out.add(s);
if(n==1)
dup.add(s);
}
else
mutex.add(s);
System.out.print("\t"+y);
}
n++;
al.clear();
for(int i=0;i<out.size();i++){
for(int j=i;j<dup.size();j++){
if(!out.get(i).contains(dup.get(j))){
al.add(out.get(i)+","+dup.get(j));
}
}
}
for(int i=0;i<al.size();i++){
temp=al.get(i).split(",");
for(int j=i+1;j<al.size();j++){
cnt=0;
temp1=al.get(j).split(",");
for(k=0;k<temp.length;k++){
if(al.get(i).contains(temp1[k])) cnt++;
}
if(cnt==3){
al.remove(j);
}
}
}
//System.out.println(al);

for(int i=0;i<mutex.size();i++){
temp=mutex.get(i).split(",");
for(int j=0;j<al.size();j++){
temp1=al.get(j).split(",");
p=0;
for(k=0;k<temp1.length;k++){
for(z=0;z<temp.length;z++){
if(temp1[k].equals(temp[z])) {
p++;
break;
}
}
//if(p==temp.length)break;
}
if(p==temp.length){al.remove(j);
//System.out.println("yay");
}
}
}
//System.out.println(mutex);
if(out.size()==0)
test=false;
}
}

}