-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiniIndex.java
158 lines (139 loc) · 4.57 KB
/
GiniIndex.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.io.*;
import java.util.*;
class GiniIndex
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("C://Users/it/Desktop/data.csv"));
String []str=br.readLine().trim().split(",");
int n=str.length;
ArrayList<ArrayList<String>> l=new ArrayList<>();
while(true)
{
String [] s=new String[n];
try{
s=br.readLine().trim().split(",");
}
catch(NullPointerException e)
{
break;
}
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<n;i++)
{
temp.add(s[i]);
}
l.add(temp);
}
String[][] array = new String[l.size()][];
for (int i = 0; i < l.size(); i++)
{
ArrayList<String> row = l.get(i);
array[i] = row.toArray(new String[row.size()]);
}
Map<String,Integer> m=new HashMap<>();
int p=array[0].length;
for(int j=0;j<array.length;j++)
{
String key=array[j][p-1];
if(!m.containsKey(key))
{
m.put(key,1);
}
else
{
m.put(key,m.get(key)+1);
}
}
//finding the class gini Index
int tot=0;
for(Map.Entry<String,Integer> e:m.entrySet())
{
tot+=e.getValue();
}
ArrayList<Double> l1=new ArrayList<>();
for(Map.Entry<String,Integer> e:m.entrySet())
{
l1.add(1.0*e.getValue()/tot);
}
double class_gini=1;
for(int i=0;i<l1.size();i++)
{
class_gini-=l1.get(i)*l1.get(i);
}
System.out.println("Class Gini Index is-->"+class_gini);
//finding the entropy of attr
StringBuilder builder = new StringBuilder();
builder.append("col_name"+","+"info gain");
builder.append("\n");
for(int i=0;i<array[0].length-1;i++)
{
double ans=findAttributeGini(array,i);
System.out.print("Gini Index for"+str[i]+"is---->");
builder.append(str[i]+","+ans);
builder.append("\n");
System.out.println(ans);
}
BufferedWriter writer = new BufferedWriter(new FileWriter("output15.csv"));
writer.write(builder.toString());
//save the string representation of the board
writer.close();
}
public static double findAttributeGini(String [][] array,int col_no)
{
Map<String,Map<String,Integer>> m=new HashMap<>();
int p=array[0].length;
for(int i=0;i<array.length;i++)
{
String key=array[i][col_no];
if(!m.containsKey(key))
{
m.put(key,new HashMap<>());
}
Map<String,Integer>temp=m.get(key);
String key2=array[i][p-1];
if(!temp.containsKey(key2))
{
temp.put(key2,1);
}
else
{
temp.put(key2,temp.get(key2)+1);
}
}
int i=0,k=0;
double attr_gini[]=new double[100];
for(Map.Entry<String,Map<String,Integer>> e1:m.entrySet())
{
// System.out.print(e1.getKey()+" ");
Map<String,Integer> m3=e1.getValue();
int tot=0;
for(Map.Entry<String,Integer>e3:m3.entrySet())
{
tot+=e3.getValue();
}
ArrayList<Double>l1=new ArrayList<>();
for(Map.Entry<String,Integer> e3:m3.entrySet())
{
l1.add(1.0*e3.getValue()/tot);
}
//System.out.println(l1);
for( i=0;i<l1.size();i++)
{
double ans=l1.get(i)*l1.get(i);
attr_gini[k++]+=ans;
}
for(i=0;i<k;i++)
{
attr_gini[i]=1-attr_gini[i];
attr_gini[i]=(1.0*tot/array.length)*attr_gini[i];
}
}
double total_attr_gini=0.0;
for(i=0;i<k;i++)
{
total_attr_gini+=attr_gini[i];
}
return total_attr_gini;
}
}