-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerceptron.java
More file actions
200 lines (180 loc) · 4.15 KB
/
Copy pathPerceptron.java
File metadata and controls
200 lines (180 loc) · 4.15 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.ArrayList;
import java.util.Arrays;
//
// Yunyun Chen
// Platform: MacOS
// Language/Environment: java
//
// In accordance with the class policies and Georgetown's Honor Code,
// I certify that, with the exceptions of the class resources and those
// items noted below, I have neither given nor received any assistance
// on this project.
//
public class Perceptron extends Classifier implements OptionHandler{
static int x = 10;//x-fold cross validation
static boolean hasTrainingSet = false;
static boolean hasTestSet = false;
static DataSet train;
public static int classIndex;
static String[] options;
int index_of_pos = 0,index_of_neg =1;
Double[] w;
Boolean converged = false;
Double eta = 0.5;
public static void main(String args[]) throws Exception
{
Perceptron p = new Perceptron(args);
TrainTestSets TTS = new TrainTestSets(options);
Evaluator evaluator = new Evaluator(p,TTS);
//System.out.println(TTS);
if(hasTestSet)
{
evaluator.holdOut();
}
else{
evaluator.kFold(x);
}
}
Perceptron(String[] args)
{
setOptions(args);
}
public void train(DataSet dataset) throws Exception
{
train=dataset;
classIndex = dataset.attributes.classIndex;
Initialize_w(dataset.attributes.classIndex);//initialize w = 0, other initializations of weight
//vector are possible, so keep it as a function.
converged = false;
Double y_i = 0.0;
while(!converged)
{
//System.out.println("========while=========");
converged = true;
for(int i = 0;i<train.examples.size();i++)
{
y_i = getActualSign(train.examples.get(i));
//System.out.println("y_i= "+y_i);
// get y_i
if(y_i*dotProduct(w,train.examples.get(i))<=0)
{
add_w(y_i,train.examples.get(i));
converged = false;
//System.out.println("false");
//System.out.println("=====================");
}
}
}
}
void add_w(Double y_i,Example e)
{
for(int i=0;i<w.length;i++)
{
if(i == classIndex)// add 1 dimension
{
w[i] += eta*y_i*1;
}
if(i != classIndex)
w[i] += eta*y_i*e.get(i);
}
// System.out.println(new ArrayList<Double>(Arrays.asList(w)));
}
Double dotProduct(Double[] w, Example e)
{
//get w dot x
Double result = 0.0;
for(int i=0;i<e.size();i++)
{
if(i == classIndex)
{
result += w[i];
}
if(i != classIndex)
{
result += w[i]*e.get(i);
}
}
// System.out.println(result);
return result;
}
Double getActualSign(Example e)
{
//get y_i
classIndex = train.attributes.classIndex;
int index = e.get(classIndex).intValue();
if(index == index_of_pos)
return +1.0;
else
return -1.0;
// ok
}
void Initialize_w(int length)
{
//initialize w = 0
w = new Double[length+1];
for(int i =0;i<w.length;i++)
w[i] = 0.0;
}
int classify(Example e) throws Exception
{
Double result = dotProduct(w,e);
//System.out.println(index_of_neg);
if(result>0)
return index_of_pos;
else
return index_of_neg;
}
Performance classify(DataSet dataset) throws Exception
{
classIndex = dataset.attributes.classIndex;
int[] result = new int[dataset.examples.size()];
for(int i=0;i<dataset.examples.size();i++)
{
result[i] = classify(dataset.examples.get(i));
}
Performance p= new Performance(dataset, classIndex, result);
// System.out.println(p.getAccuracy());
return p;
}
static void setx(String a)
{
x = Integer.valueOf(a);
}
public void setOptions(String args[])
{
int count = 0;
String temp[] = new String[args.length];
for(int i = 0 ; i< args.length-1; i+=2)
{
if(args[i].compareTo("-t")==0)
{
temp[i] = args[i];
temp[i+1] = args[i+1];
count = i+1;
hasTrainingSet = true;
}
if(args[i].compareTo("-T")==0)
{
temp[i] = args[i];
temp[i+1] = args[i+1];
count = i+1;
hasTestSet = true;
}
if(args[i].compareTo("-eta")==0)
{
eta = Double.valueOf(args[i+1]);
}
if(args[i].compareTo("-x")==0)
{
setx(args[i+1]);
}
}
options = new String[count+1];
for(int i=0;i<count+1;i++)
{
options[i]=temp[i];
// System.out.println(options[i]);
}
}
}