-
Notifications
You must be signed in to change notification settings - Fork 294
/
logistic_regression.java
75 lines (66 loc) · 2.46 KB
/
logistic_regression.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
package com.gg.ml;
import java.io.File;
import java.io.IOException;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
public class LogisticRegressionDemo {
/** file names are defined*/
public static final String TRAINING_DATA_SET_FILENAME="weather.nominal.arff";
public static final String TESTING_DATA_SET_FILENAME="weather.nominal .test.arff";
public static final String PREDICTION_DATA_SET_FILENAME="weather.nominal-confused.arff";
* This method is to load the data set.
* @param fileName
* @return
* @throws IOException
*/
public static Instances getDataSet(String fileName) throws IOException {
/**
* we can set the file i.e., loader.setFile("finename") to load the data
*/
int classIdx = 1;
/** the arffloader to load the arff file */
ArffLoader loader = new ArffLoader();
//loader.setFile(new File(fileName));
/** load the traing data */
loader.setSource(LogisticRegressionDemo.class.getResourceAsStream("/" + fileName));
/**
* we can also set the file like loader3.setFile(new
* File("test-confused.arff"));
*/
Instances dataSet = loader.getDataSet();
/** set the index based on the data given in the arff files */
dataSet.setClassIndex(classIdx);
return dataSet;
}
* algorithm with testing data
*/
Evaluation eval = new Evaluation(trainingDataSet);
eval.evaluateModel(classifier, testingDataSet);
/** Print the algorithm summary */
/**
* This method is used to process the input and return the statistics.
*
* @throws Exception
*/
public static void process() throws Exception {
Instances trainingDataSet = getDataSet(TRAINING_DATA_SET_FILENAME);
Instances testingDataSet = getDataSet(TESTING_DATA_SET_FILENAME);
/** Classifier here is Linear Regression */
Classifier classifier = new weka.classifiers.functions.Logistic();
/** */
classifier.buildClassifier(trainingDataSet);
/**
* train the alogorithm with the training data and evaluate the
System.out.println("** Linear Regression Evaluation with Datasets **");
System.out.println(eval.toSummaryString());
System.out.print(" the expression for the input data as per alogorithm is ");
System.out.println(classifier);
Instance predicationDataSet = getDataSet(PREDICTION_DATA_SET_FILENAME).lastInstance();
double value = classifier.classifyInstance(predicationDataSet);
/** Prediction Output */
System.out.println(value);
}
}