-
Notifications
You must be signed in to change notification settings - Fork 0
/
WekaFile.java
111 lines (99 loc) · 5.33 KB
/
WekaFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instance;
import weka.core.Instances;
import java.io.*;
import java.util.HashMap;
import java.util.Random;
public class WekaFile {
private S3 s3;
private HashMap<Integer, String> map;
public WekaFile() throws FileNotFoundException {
s3 = new S3();
map = new HashMap<>();
}
public void createMap () throws IOException {
ResponseBytes<GetObjectResponse> responseBytes = s3.getObjectBytes("Step4/PairFile-r-00000", "ass003");
byte[] objectData = responseBytes.asByteArray();
String path = System.getProperty("user.dir") + "/step4.txt";
File InputFile = new File(path);
OutputStream outputStream = new FileOutputStream(InputFile);
outputStream.write(objectData);
outputStream.flush();
outputStream.close();
try {
BufferedReader reader = new BufferedReader(new FileReader(InputFile));
String line = reader.readLine();
while (line != null) {
//line = 0 abattoir jet no
String [] arr = line.toString().split("\\s+");
map.putIfAbsent(Integer.parseInt(arr[0]), arr[1]+" "+arr[2]+" "+arr[3]);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.out.println("Problem in function: WorkMassages");
e.printStackTrace();
}
}
public void activateWeka() throws IOException {
//downloading the file with all the 24 length vectors from S3
ResponseBytes<GetObjectResponse> responseBytes = s3.getObjectBytes("Step4/WekaFile-r-00000", "ass003");
byte[] objectData = responseBytes.asByteArray();
String path = System.getProperty("user.dir") + "/step4.txt";
File InputFile = new File(path);
OutputStream outputStream = new FileOutputStream(InputFile);
outputStream.write(objectData);
outputStream.flush();
outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
Instances trainSet = new Instances(bufferedReader);
trainSet.setClassIndex(trainSet.numAttributes() - 1); //set the class attribute to our file to be the last one, {yes, no}
bufferedReader.close();
NaiveBayes naiveBayes = new NaiveBayes();
try {
naiveBayes.buildClassifier(trainSet);
Evaluation eval = new Evaluation(trainSet);
eval.crossValidateModel(naiveBayes, trainSet, 10, new Random(1)); //making 10 crossValidation
File result = new File(System.getProperty("user.dir")+"/Final.txt");
FileOutputStream fos = new FileOutputStream(result, true);
fos.write(("\n***The results: ***\n").getBytes());
fos.write(("Correctly Classified Instances: ").getBytes());
fos.write((eval.pctCorrect()+"%\n").getBytes());
fos.write(("Incorrectly Classified Instances ").getBytes());
fos.write((eval.pctIncorrect()+"%\n").getBytes());
String s= "F1: " + eval.fMeasure(1) + " \nPrecision: " + eval.precision(1) + " \nRecall: " + eval.recall(1);
fos.write(s.getBytes());
fos.close();
s3.PutObject(System.getProperty("user.dir")+"/Final.txt","ass003","Final");
//finding out the true-positive, false-positive, true-negative, and false-negative classes
File analysis = new File(System.getProperty("user.dir")+"/Analysis.txt");
FileOutputStream fos2 = new FileOutputStream(analysis, true);
for (int i = 0; i < trainSet.numInstances(); i++) {
String pair = map.get(i);
String [] pArr = pair.split("\\s+");
Instance instance = trainSet.instance(i);
double pred = naiveBayes.classifyInstance(instance);
String predClass = trainSet.classAttribute().value((int) pred);
if (predClass.equals("yes") && pArr[2].equals("yes")){
fos2.write(("The pair: " + pArr[0]+" "+pArr[1] + ", "+ "the predicted Class is: "+ predClass +", the index is: " +i+" *** true-positive ***\n").getBytes());
}
else if (predClass.equals("yes") && pArr[2].equals("no")){
fos2.write(("The pair: " + pArr[0]+" "+pArr[1] + ", "+ "the predicted Class is: "+ predClass +", the index is: " +i+" *** false-positive ***\n").getBytes());
}
else if (predClass.equals("no") && pArr[2].equals("yes")){
fos2.write(("The pair: " + pArr[0]+" "+pArr[1] + ", "+ "the predicted Class is: "+ predClass +", the index is: " +i+" *** false-negative ***\n").getBytes());
}
else if (predClass.equals("no") && pArr[2].equals("no")){
fos2.write(("The pair: " + pArr[0]+" "+pArr[1] + ", "+ "the predicted Class is: "+ predClass +", the index is: " +i+" *** true-negative ***\n").getBytes());
}
}
fos2.close();
s3.PutObject(System.getProperty("user.dir")+"/Analysis.txt","ass003","Analysis");
} catch (Exception e) {
e.printStackTrace();
}
}
}