示例#1
0
 public void testSVMs(Vector<Classifier> SVMsToTest, Dataset dataHigh, Dataset dataLow) {
   Map<Object, PerformanceMeasure> pmHigh =
       EvaluateDataset.testDataset(SVMsToTest.get(0), dataHigh);
   Map<Object, PerformanceMeasure> pmLow = EvaluateDataset.testDataset(SVMsToTest.get(1), dataLow);
   for (Object o : pmHigh.keySet()) {
     System.out.println(o + ": " + pmHigh.get(o).tp + " " + pmHigh.get(o).fp);
     System.out.println("f measure: " + pmHigh.get(o).getFMeasure());
   }
   for (Object o : pmLow.keySet()) {
     System.out.println(o + ": " + pmLow.get(o).tp + " " + pmLow.get(o).fp);
     System.out.println("f measure: " + pmLow.get(o).getFMeasure());
   }
 }
示例#2
0
  /** Shows the default usage of the KNN algorithm. */
  public static void main(String[] args) throws Exception {

    /* Load a data set */
    Dataset data = FileHandler.loadDataset(new File(DATASET), 4, ",");
    /*
     * Contruct a KNN classifier that uses 5 neighbors to make a decision.
     */
    Classifier knn = new KNearestNeighbors(5);
    knn.buildClassifier(data);

    Classifier kdtKnn = new KDtreeKNN(5);
    kdtKnn.buildClassifier(data);

    /*
     * Load a data set for evaluation, this can be a different one, but we
     * will use the same one.
     */
    Dataset dataForClassification = FileHandler.loadDataset(new File(DATASET), 4, ",");
    /* Counters for correct and wrong predictions. */
    int correct = 0, wrong = 0;
    /* Classify all instances and check with the correct class values */
    for (Instance inst : dataForClassification) {
      Object predictedClassValue = knn.classify(inst);
      Object realClassValue = inst.classValue();
      // System.out.println("predicted=" + predictedClassValue.toString() + "
      // real="+realClassValue.toString());
      if (predictedClassValue.equals(realClassValue)) {
        correct++;
      } else {
        wrong++;
      }
    }
    System.out.println("Correct predictions  " + correct);
    System.out.println("Wrong predictions " + wrong);

    /* Performance
     *
     */
    System.out.println("Performance ...");
    Map<Object, PerformanceMeasure> pm = EvaluateDataset.testDataset(knn, dataForClassification);
    printPerfMeasure(pm);
    /*
     * Cross validation
     */
    System.out.println("Cross validation ...");
    /* Construct new cross validation instance with the KNN classifier, */
    CrossValidation cv = new CrossValidation(knn);
    /* 5-fold CV with fixed random generator */
    Map<Object, PerformanceMeasure> p0 = cv.crossValidation(data, 5, new Random(1));
    Map<Object, PerformanceMeasure> p1 = cv.crossValidation(data, 5, new Random(1));
    Map<Object, PerformanceMeasure> p2 = cv.crossValidation(data, 5, new Random(25));
    printPerfMeasure(p0);
    printPerfMeasure(p1);
    printPerfMeasure(p2);

    /*
     * Create Weka classifier
     */
    System.out.println("Weka classifier ...");
    SMO smo = new SMO();
    /* Wrap Weka classifier in bridge */
    Classifier javamlsmo = new WekaClassifier(smo);
    /* Initialize cross-validation */
    CrossValidation wekaCV = new CrossValidation(javamlsmo);
    /* Perform cross-validation */
    Map<Object, PerformanceMeasure> wekaPm = wekaCV.crossValidation(data);
    /* Output results
     * see http://en.wikipedia.org/wiki/Precision_and_recall
     */
    System.out.println("see http://en.wikipedia.org/wiki/Precision_and_recall" + wekaPm);
    printPerfMeasure(wekaPm);

    /*
     * Feature scoring
     */
    System.out.println("Feature scoring ");
    GainRatio ga = new GainRatio();
    /* Apply the algorithm to the data set */
    ga.build(data);
    /* Print out the score of each attribute */
    for (int i = 0; i < ga.noAttributes(); i++) {
      System.out.println("Attribute[" + i + "] relevance" + ga.score(i));
    }
  }