示例#1
0
  public static Double runClassify(String trainFile, String testFile) {
    double predictOrder = 0.0;
    double trueOrder = 0.0;
    try {
      String trainWekaFileName = trainFile;
      String testWekaFileName = testFile;

      Instances train = DataSource.read(trainWekaFileName);
      Instances test = DataSource.read(testWekaFileName);

      train.setClassIndex(0);
      test.setClassIndex(0);

      train.deleteAttributeAt(8);
      test.deleteAttributeAt(8);
      train.deleteAttributeAt(6);
      test.deleteAttributeAt(6);
      train.deleteAttributeAt(5);
      test.deleteAttributeAt(5);
      train.deleteAttributeAt(4);
      test.deleteAttributeAt(4);

      // AdditiveRegression classifier = new AdditiveRegression();

      // NaiveBayes classifier = new NaiveBayes();

      RandomForest classifier = new RandomForest();
      // LibSVM classifier = new LibSVM();

      classifier.buildClassifier(train);
      Evaluation eval = new Evaluation(train);
      eval.evaluateModel(classifier, test);

      System.out.println(eval.toSummaryString("\nResults\n\n", true));
      // System.out.println(eval.toClassDetailsString());
      // System.out.println(eval.toMatrixString());
      int k = 892;
      for (int i = 0; i < test.numInstances(); i++) {
        predictOrder = classifier.classifyInstance(test.instance(i));
        trueOrder = test.instance(i).classValue();
        System.out.println((k++) + "," + (int) predictOrder);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
    return predictOrder;
  }
  public void trainSystem() {

    BufferedReader reader = null;
    Instances data = null;
    try {
      reader =
          new BufferedReader(
              new InputStreamReader(getResources().getAssets().open("training_data.arff")));
      data = new Instances(reader);
      reader.close();
      data.setClassIndex(data.numAttributes() - 2);
      classifier = new RandomForest();
      classifier.buildClassifier(data);
    } catch (FileNotFoundException e) {
      Toast.makeText(getApplicationContext(), "File not Found.!", Toast.LENGTH_LONG).show();
      Log.e("GPSTracker", "fnfexception", e);
    } catch (IOException e) {
      Toast.makeText(getApplicationContext(), "IO Exception.!", Toast.LENGTH_LONG).show();
      Log.e("GPSTracker", "ioexception", e);
    } catch (Exception e) {
      Log.e("GPSTracker", "exception", e);
      Toast.makeText(getApplicationContext(), "Exception.!", Toast.LENGTH_LONG).show();
    }
  }