@@ -25,19 +25,24 @@ public class RedditClassifier {
2525
2626 public static int GOOD = 0 ;
2727 public static int BAD = 1 ;
28- public static int MIN_SCORE = 10 ;
28+ public static int MIN_SCORE = 7 ;
29+ public static int NUM_OF_FEATURES = 1000 ;
30+
2931 private final AdaptiveLogisticRegression classifier ;
3032 private final FeatureVectorEncoder titleEncoder ;
3133 private final FeatureVectorEncoder domainEncoder ;
3234 private CrossFoldLearner learner ;
35+ private double accuracy ;
3336
3437 private final int [] trainCount = { 0 , 0 };
3538
3639 private final int [] evalCount = { 0 , 0 };
3740
41+ private final int [] correctCount = { 0 , 0 };
42+
3843 public RedditClassifier () {
39- classifier = new AdaptiveLogisticRegression (2 , 4 , new L2 ());
40- classifier .setPoolSize (25 );
44+ classifier = new AdaptiveLogisticRegression (2 , NUM_OF_FEATURES , new L2 ());
45+ classifier .setPoolSize (50 );
4146 titleEncoder = new AdaptiveWordValueEncoder ("title" );
4247 titleEncoder .setProbes (1 );
4348 domainEncoder = new StaticWordValueEncoder ("domain" );
@@ -46,34 +51,17 @@ public RedditClassifier() {
4651
4752 public void trainClassifier (String fileName ) throws IOException {
4853 final List <NamedVector > vectors = extractVectors (readDataFile (fileName ));
54+ final int noOfTraining = (int ) (RedditDataCollector .DATA_SIZE * 0.8 );
55+ final List <NamedVector > trainingData = vectors .subList (0 , noOfTraining );
56+ final List <NamedVector > testData = vectors .subList (noOfTraining , RedditDataCollector .DATA_SIZE );
4957 int category ;
50- for (final NamedVector vector : vectors ) {
58+ for (final NamedVector vector : trainingData ) {
5159 category = (vector .getName () == "GOOD" ) ? GOOD : BAD ;
5260 classifier .train (category , vector );
5361 trainCount [category ]++;
5462 }
55- System .out .println ("Training count ========= " + trainCount [0 ] + "___" + trainCount [1 ]);
56- }
57-
58- public double evaluateClassifier () throws IOException {
59- final List <NamedVector > vectors = extractVectors (readDataFile (RedditDataCollector .TEST_FILE ));
60- int category , result ;
61- int correct = 0 ;
62- int wrong = 0 ;
63- for (final NamedVector vector : vectors ) {
64- category = (vector .getName () == "GOOD" ) ? GOOD : BAD ;
65- result = classify (vector );
66-
67- evalCount [category ]++;
68- if (category == result ) {
69- correct ++;
70- } else {
71- wrong ++;
72- }
73- }
74- System .out .println (correct + " ----- " + wrong );
75- System .out .println ("Eval count ========= " + evalCount [0 ] + "___" + evalCount [1 ]);
76- return correct / (wrong + correct + 0.0 );
63+ System .out .println ("Training count ========= Good = " + trainCount [0 ] + " ___ Bad = " + trainCount [1 ]);
64+ evaluateClassifier (testData );
7765 }
7866
7967 public Vector convertPost (String title , String domain , int hour ) {
@@ -93,8 +81,34 @@ public int classify(Vector features) {
9381 return learner .classifyFull (features ).maxValueIndex ();
9482 }
9583
84+ public double getAccuracy () {
85+ return accuracy ;
86+ }
87+
9688 // ==== Private methods
9789
90+ private void evaluateClassifier (List <NamedVector > vectors ) throws IOException {
91+ int category , result ;
92+ int correct = 0 ;
93+ int wrong = 0 ;
94+ for (final NamedVector vector : vectors ) {
95+ category = (vector .getName () == "GOOD" ) ? GOOD : BAD ;
96+ result = classify (vector );
97+
98+ evalCount [category ]++;
99+ if (category == result ) {
100+ correct ++;
101+ correctCount [result ]++;
102+ } else {
103+ wrong ++;
104+ }
105+ }
106+ System .out .println ("Eval count ========= Good = " + evalCount [0 ] + " ___ Bad = " + evalCount [1 ]);
107+ System .out .println ("Test result ======== Correct prediction = " + correct + " ----- Wrong prediction = " + wrong );
108+ System .out .println ("Test result ======== Correct Good = " + correctCount [0 ] + " ----- Correct Bad = " + correctCount [1 ]);
109+ this .accuracy = correct / (wrong + correct + 0.0 );
110+ }
111+
98112 private List <String > readDataFile (String fileName ) throws IOException {
99113 List <String > lines = Files .readLines (new File (fileName ), Charset .forName ("utf-8" ));
100114 if ((lines == null ) || (lines .size () == 0 )) {
@@ -116,15 +130,18 @@ private List<NamedVector> extractVectors(List<String> lines) {
116130 private NamedVector extractVector (String line ) {
117131 final String [] items = line .split ("," );
118132 final String category = extractCategory (Integer .parseInt (items [0 ]));
119- final NamedVector vector = new NamedVector (new RandomAccessSparseVector (4 ), category );
133+ final NamedVector vector = new NamedVector (new RandomAccessSparseVector (NUM_OF_FEATURES ), category );
120134 final Calendar cal = Calendar .getInstance (TimeZone .getTimeZone ("GMT" ));
121135 cal .setTimeInMillis (Long .parseLong (items [1 ]) * 1000 );
122136
123- titleEncoder .addToVector (items [3 ], vector );
137+ vector .set (0 , cal .get (Calendar .HOUR_OF_DAY )); // hour of day
138+ vector .set (1 , Integer .parseInt (items [2 ])); // number of words in the title
124139 domainEncoder .addToVector (items [4 ], vector );
125- vector .set (2 , cal .get (Calendar .HOUR_OF_DAY )); // hour of day
126- vector .set (3 , Integer .parseInt (items [2 ])); // number of words in the title
127-
140+ final String [] words = items [3 ].split (" " );
141+ // titleEncoder.setProbes(words.length);
142+ for (final String word : words ) {
143+ titleEncoder .addToVector (word , vector );
144+ }
128145 return vector ;
129146 }
130147
0 commit comments