Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.util.Random;
  4.  
  5. import weka.classifiers.Classifier;
  6. import weka.classifiers.Evaluation;
  7. import weka.classifiers.bayes.NaiveBayes;
  8. import weka.classifiers.functions.MultilayerPerceptron;
  9. import weka.classifiers.functions.SMO;
  10. import weka.classifiers.rules.JRip;
  11. import weka.classifiers.rules.ZeroR;
  12. import weka.classifiers.trees.J48;
  13. import weka.core.Instances;
  14.  
  15. public class Main {
  16. private static int folds = 10;
  17. private static int runs = 1;
  18.  
  19. public static void main(String[] args) throws Exception {
  20. BufferedReader reader = new BufferedReader(new FileReader("dane.arff"));
  21. Instances data = new Instances(reader);
  22. reader.close();
  23.  
  24. Instances trainData = new Instances(data);
  25. double[][] confMatrix = null;
  26. trainData.setClassIndex(trainData.numAttributes() - 1);
  27.  
  28. // Classifier cls = (Classifier) new ZeroR();
  29. // Classifier cls = (Classifier) new JRip();
  30. // Classifier cls = (Classifier) new J48();
  31. // Classifier cls = (Classifier) new SMO();
  32. // Classifier cls = (Classifier) new MultilayerPerceptron();
  33. Classifier cls = (Classifier) new NaiveBayes();
  34.  
  35. for (int i = 0; i < runs; i++) {
  36. // randomize data
  37. int seed = i + 1;
  38. Random rand = new Random(seed);
  39.  
  40. trainData.randomize(rand);
  41. trainData.stratify(folds);
  42.  
  43. Evaluation eval = new Evaluation(trainData);
  44. for (int n = 0; n < folds; n++) {
  45. Instances train = trainData.trainCV(folds, n);
  46. Instances test = trainData.testCV(folds, n);
  47.  
  48. // build and evaluate classifier
  49. Classifier clsCopy = Classifier.makeCopy(cls);
  50. clsCopy.buildClassifier(train);
  51. eval.evaluateModel(clsCopy, test);
  52. }
  53. confMatrix = addMatrixes(confMatrix, eval.confusionMatrix());
  54.  
  55.  
  56. }
  57. confMatrix = divideMatrixBy(confMatrix, runs);
  58.  
  59. System.out.println("Classifier: " + cls.getClass().getName() + " ");
  60. System.out.println("Folds: " + folds);
  61. System.out.println("Runs: " + runs);
  62. System.out.println("Accuracy: " + getAccuracy(confMatrix));
  63. System.out.println("TpRate: " + getTpRate(confMatrix));
  64. System.out.println("TnRate: " + getTnRate(confMatrix));
  65. System.out.println("GMean: " + getGMean(confMatrix));
  66. System.out.println("AUC: " + getAUC(getTpRate(confMatrix),getFpRate(confMatrix)));
  67. System.out.println(matrixToString(confMatrix));
  68.  
  69. }
  70.  
  71. public static double[][] addMatrixes(double[][] matrix, double[][] matrixTemp) {
  72. if (matrix == null) {
  73. return matrixTemp;
  74. }
  75. for (int i = 0; i < matrix.length; i++) {
  76. for (int j = 0; j < matrix[i].length; j++) {
  77. matrix[i][j] = matrix[i][j] + matrixTemp[i][j];
  78. }
  79. }
  80. return matrix;
  81. }
  82.  
  83. public static double[][] divideMatrixBy(double[][] matrix, double d) {
  84.  
  85. for (int i = 0; i < matrix.length; i++) {
  86. for (int j = 0; j < matrix[i].length; j++) {
  87. matrix[i][j] = matrix[i][j] / d;
  88. }
  89. }
  90. return matrix;
  91. }
  92.  
  93. public static double getAccuracy(double[][] matrix) {
  94.  
  95. double accuracy = (matrix[0][0] + matrix[1][1])
  96. / (matrix[0][0] + matrix[0][1] + matrix[1][0] + matrix[1][1]);
  97.  
  98. return accuracy;
  99. }
  100.  
  101. public static double getTnRate(double[][] matrix) {
  102. double tnRate = (matrix[1][1]) / (matrix[1][0] + matrix[1][1]);
  103.  
  104. return tnRate;
  105. }
  106.  
  107. public static double getTpRate(double[][] matrix) {
  108. double tpRate = (matrix[0][0]) / (matrix[0][1] + matrix[0][0]);
  109.  
  110. return tpRate;
  111. }
  112.  
  113. public static double getFpRate(double[][] matrix) {
  114. double fpRate = (matrix[1][0]) / (matrix[1][0] + matrix[0][0]);
  115.  
  116. return fpRate;
  117. }
  118.  
  119. public static double getGMean(double[][] matrix) {
  120. double gMean = Math.sqrt( getTpRate( matrix) * getTnRate(matrix));
  121.  
  122. return gMean;
  123. }
  124.  
  125. public static double getAUC(double tpRate, double fpRate) {
  126. double auc = (1 + tpRate - fpRate)/2;
  127.  
  128. return auc;
  129. }
  130.  
  131. public static StringBuffer matrixToString(double [][] matrix){
  132. StringBuffer text = new StringBuffer("Confusion matrix: \n");
  133. for (int i = 0; i < matrix.length; i++) {
  134. for (int j = 0; j < matrix[i].length; j++) {
  135. text.append(" "+matrix[i][j]);
  136. }
  137. text.append("\n");
  138. }
  139.  
  140. return text;
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement