Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.Random;
- import weka.classifiers.Classifier;
- import weka.classifiers.Evaluation;
- import weka.classifiers.bayes.NaiveBayes;
- import weka.classifiers.functions.MultilayerPerceptron;
- import weka.classifiers.functions.SMO;
- import weka.classifiers.rules.JRip;
- import weka.classifiers.rules.ZeroR;
- import weka.classifiers.trees.J48;
- import weka.core.Instances;
- public class Main {
- private static int folds = 10;
- private static int runs = 1;
- public static void main(String[] args) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("dane.arff"));
- Instances data = new Instances(reader);
- reader.close();
- Instances trainData = new Instances(data);
- double[][] confMatrix = null;
- trainData.setClassIndex(trainData.numAttributes() - 1);
- // Classifier cls = (Classifier) new ZeroR();
- // Classifier cls = (Classifier) new JRip();
- // Classifier cls = (Classifier) new J48();
- // Classifier cls = (Classifier) new SMO();
- // Classifier cls = (Classifier) new MultilayerPerceptron();
- Classifier cls = (Classifier) new NaiveBayes();
- for (int i = 0; i < runs; i++) {
- // randomize data
- int seed = i + 1;
- Random rand = new Random(seed);
- trainData.randomize(rand);
- trainData.stratify(folds);
- Evaluation eval = new Evaluation(trainData);
- for (int n = 0; n < folds; n++) {
- Instances train = trainData.trainCV(folds, n);
- Instances test = trainData.testCV(folds, n);
- // build and evaluate classifier
- Classifier clsCopy = Classifier.makeCopy(cls);
- clsCopy.buildClassifier(train);
- eval.evaluateModel(clsCopy, test);
- }
- confMatrix = addMatrixes(confMatrix, eval.confusionMatrix());
- }
- confMatrix = divideMatrixBy(confMatrix, runs);
- System.out.println("Classifier: " + cls.getClass().getName() + " ");
- System.out.println("Folds: " + folds);
- System.out.println("Runs: " + runs);
- System.out.println("Accuracy: " + getAccuracy(confMatrix));
- System.out.println("TpRate: " + getTpRate(confMatrix));
- System.out.println("TnRate: " + getTnRate(confMatrix));
- System.out.println("GMean: " + getGMean(confMatrix));
- System.out.println("AUC: " + getAUC(getTpRate(confMatrix),getFpRate(confMatrix)));
- System.out.println(matrixToString(confMatrix));
- }
- public static double[][] addMatrixes(double[][] matrix, double[][] matrixTemp) {
- if (matrix == null) {
- return matrixTemp;
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- matrix[i][j] = matrix[i][j] + matrixTemp[i][j];
- }
- }
- return matrix;
- }
- public static double[][] divideMatrixBy(double[][] matrix, double d) {
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- matrix[i][j] = matrix[i][j] / d;
- }
- }
- return matrix;
- }
- public static double getAccuracy(double[][] matrix) {
- double accuracy = (matrix[0][0] + matrix[1][1])
- / (matrix[0][0] + matrix[0][1] + matrix[1][0] + matrix[1][1]);
- return accuracy;
- }
- public static double getTnRate(double[][] matrix) {
- double tnRate = (matrix[1][1]) / (matrix[1][0] + matrix[1][1]);
- return tnRate;
- }
- public static double getTpRate(double[][] matrix) {
- double tpRate = (matrix[0][0]) / (matrix[0][1] + matrix[0][0]);
- return tpRate;
- }
- public static double getFpRate(double[][] matrix) {
- double fpRate = (matrix[1][0]) / (matrix[1][0] + matrix[0][0]);
- return fpRate;
- }
- public static double getGMean(double[][] matrix) {
- double gMean = Math.sqrt( getTpRate( matrix) * getTnRate(matrix));
- return gMean;
- }
- public static double getAUC(double tpRate, double fpRate) {
- double auc = (1 + tpRate - fpRate)/2;
- return auc;
- }
- public static StringBuffer matrixToString(double [][] matrix){
- StringBuffer text = new StringBuffer("Confusion matrix: \n");
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- text.append(" "+matrix[i][j]);
- }
- text.append("\n");
- }
- return text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement