88import java .io .IOException ;
99import weka .core .*;
1010import weka .core .converters .ConverterUtils .DataSource ;
11+ import weka .core .converters .CSVLoader ;
1112import weka .filters .Filter ;
1213import weka .filters .unsupervised .attribute .NumericToNominal ;
1314
@@ -29,7 +30,8 @@ public class LogisticRegression {
2930 + " the last row of the input file.\n \n "
3031 + "Options:\n \n "
3132 + "-t [string] Optional file containing containing\n "
32- + " test dataset" );
33+ + " test dataset\n "
34+ + "-m [int] Maximum number of iterations\n " );
3335
3436 public static HashMap <Integer , Double > createClassMap (Instances Data ) {
3537 HashMap <Integer , Double > classMap = new HashMap <Integer , Double >();
@@ -69,6 +71,8 @@ public static void main(String args[]) {
6971
7072 // Load input dataset.
7173 DataSource source = new DataSource (regressorsFile );
74+ if (source .getLoader () instanceof CSVLoader )
75+ ((CSVLoader ) source .getLoader ()).setNoHeaderRowPresent (true );
7276 Instances data = source .getDataSet ();
7377
7478 // Transform numeric class to nominal class because the
@@ -81,12 +85,19 @@ public static void main(String args[]) {
8185 nm .setInputFormat (data );
8286 data = Filter .useFilter (data , nm );
8387
88+ boolean hasMaxIters = false ;
89+ int maxIter = Integer .parseInt (Utils .getOption ('m' , args ));
90+ if (maxIter != 0 )
91+ hasMaxIters = true ;
92+
8493 // Did the user pass a test file?
8594 String testFile = Utils .getOption ('t' , args );
8695 Instances testData = null ;
8796 if (testFile .length () != 0 )
8897 {
8998 source = new DataSource (testFile );
99+ if (source .getLoader () instanceof CSVLoader )
100+ ((CSVLoader ) source .getLoader ()).setNoHeaderRowPresent (true );
90101 testData = source .getDataSet ();
91102
92103 // Weka makes the assumption that the structure of the training and test
@@ -122,6 +133,8 @@ public static void main(String args[]) {
122133 // Perform Logistic Regression.
123134 timer .StartTimer ("total_time" );
124135 weka .classifiers .functions .Logistic model = new weka .classifiers .functions .Logistic ();
136+ if (hasMaxIters )
137+ model .setMaxIts (maxIter );
125138 model .buildClassifier (data );
126139
127140 // Use the testdata to evaluate the modell.
@@ -140,7 +153,7 @@ public static void main(String args[]) {
140153 }
141154 FileWriter writer = new FileWriter (probabs .getName (), false );
142155
143- File predictions = new File ("weka_lr_predictions .csv" );
156+ File predictions = new File ("weka_predicted .csv" );
144157 if (!predictions .exists ()) {
145158 predictions .createNewFile ();
146159 }
0 commit comments