Skip to content

Instantly share code, notes, and snippets.

@aiscool
Created May 13, 2016 12:56
Show Gist options
  • Save aiscool/9c5f756e71e36945b14588d11a08b0fb to your computer and use it in GitHub Desktop.
Save aiscool/9c5f756e71e36945b14588d11a08b0fb to your computer and use it in GitHub Desktop.
j48 algorithm used for training and testing altered state of consciousness dataset. the result got 94.12% accuracy.
package javaapplication1;
/**
*
* @author commander
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.evaluation.NominalPrediction;
import weka.classifiers.rules.DecisionTable;
import weka.classifiers.rules.PART;
import weka.classifiers.trees.DecisionStump;
import weka.classifiers.trees.J48;
import weka.core.FastVector;
import weka.core.Instances;
import weka.experiment.RandomSplitResultProducer;
public class JavaApplication1 {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
public static Evaluation classify(Classifier model, Instances trainingSet, Instances testingSet) throws Exception {
Evaluation evaluation = new Evaluation(trainingSet);
model.buildClassifier(trainingSet);
evaluation.evaluateModel(model, testingSet);
return evaluation;
}
public static double calculateAccuracy(FastVector predictions) {
double correct = 0;
for (int i = 0; i < predictions.size(); i++) {
NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
if (np.predicted() == np.actual()) {
correct++;
}
}
return 100 * correct / predictions.size();
}
public static void main(String[] args) throws Exception {
BufferedReader datafile = readDataFile("C:\\Users\\commander\\Desktop\\fyp\\DATASET\\ASC_STATUS_BALANCE_SIMPLECART.csv.arff");
Instances data = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1);
data.randomize(new java.util.Random(1));
int trainSize = (int) Math.round(data.numInstances() * 0.9);
System.out.println("Training size : "+trainSize);
int testSize = data.numInstances() - trainSize;
Instances train = new Instances(data, 0, trainSize);
Instances test = new Instances(data, trainSize, testSize);
J48 j48 = new J48();
FastVector predictions = new FastVector();
String[] options;
options = weka.core.Utils.splitOptions("-C 0.25 -M 2");
j48.setOptions(options);
Evaluation validation = classify(j48, train, test);
predictions.appendElements(validation.predictions());
// Calculate overall accuracy of current classifier on all splits
double accuracy = calculateAccuracy(predictions);
// Print current classifier's name and accuracy in a complicated,
// but nice-looking way.
System.out.println("Accuracy of " + j48.getClass().getSimpleName() + ": "
+ String.format("%.2f%%", accuracy)
+ "\n---------------------------------");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment