-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
executable file
·69 lines (64 loc) · 2.76 KB
/
Test.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.Vector;
public class Test {
public float NMI(Vector<Integer> Prediction, String TrueCommunityPathTXT) throws Exception {
Vector<Integer> TrueLabel = new Vector<Integer>();
int countGuess = 0, countGold = 0;
float NMI = 0, up = 0, down = 0;
int n = 0;
TrueLabel.add(0);
BufferedReader br = new BufferedReader(new FileReader(TrueCommunityPathTXT));
String line = br.readLine();
while (line != null) {
String[] parts = line.split(" ");
TrueLabel.add(Integer.parseInt(parts[1]));
n++;
line = br.readLine();
}
br.close();
if (n != Prediction.size() - 1)
return -1;
Hashtable<Integer, Integer> temp = new Hashtable<Integer, Integer>();
int k = 1;
for (int i = 1; i <= n; i++) {
if (temp.containsKey((Integer) Prediction.get(i)))
Prediction.set(i, temp.get(Prediction.get(i)));
else {
temp.put(Prediction.get(i), k);
Prediction.set(i, temp.get(Prediction.get(i)));
k++;
}
}
for (int i = 1; i <= n; i++) {
if (Prediction.get(i) > countGuess)
countGuess = Prediction.get(i);
if (TrueLabel.get(i) > countGold)
countGold = TrueLabel.get(i);
}
float NRow[] = new float[countGold];
float NCol[] = new float[countGuess];
float matrix[][] = new float[countGold][countGuess];
for (int i = 0; i < countGold; i++)
matrix[i] = new float[countGuess];
for (int i = 1; i <= n; i++) {
matrix[TrueLabel.get(i) - 1][Prediction.get(i) - 1]++;
NRow[TrueLabel.get(i) - 1]++;
NCol[Prediction.get(i) - 1]++;
}
for (int i = 0; i < countGold; i++)
if (NRow[i] != 0)
down += NRow[i] * (Math.log(NRow[i] / (float) (n)) / Math.log(2));
for (int i = 0; i < countGuess; i++)
if (NCol[i] != 0)
down += NCol[i] * (Math.log(NCol[i] / (float) (n)) / Math.log(2));
for (int i = 0; i < countGold; i++)
for (int j = 0; j < countGuess; j++)
if (matrix[i][j] != 0)
up += matrix[i][j] * (Math.log((matrix[i][j] * (float) (n) / ((NRow[i] * NCol[j])))) / Math.log(2));
up *= (float) -2;
NMI = up / down;
return NMI;
}
}