Ejemplo n.º 1
0
  public static void toLibSVM(List<TrainingSample<BxZoneLabel>> trainingElements, String filePath)
      throws IOException {
    BufferedWriter svmDataFile = null;
    try {
      FileWriter fstream = new FileWriter(filePath);
      svmDataFile = new BufferedWriter(fstream);
      for (TrainingSample<BxZoneLabel> elem : trainingElements) {
        if (elem.getLabel() == null) {
          continue;
        }
        svmDataFile.write(String.valueOf(elem.getLabel().ordinal()));
        svmDataFile.write(" ");

        Integer featureCounter = 1;
        for (Double value : elem.getFeatureVector().getValues()) {
          StringBuilder sb = new StringBuilder();
          Formatter formatter = new Formatter(sb, Locale.US);
          formatter.format("%d:%.5f", featureCounter++, value);
          svmDataFile.write(sb.toString());
          svmDataFile.write(" ");
        }
        svmDataFile.write("\n");
      }
      svmDataFile.close();
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
      return;
    } finally {
      if (svmDataFile != null) {
        svmDataFile.close();
      }
    }

    System.out.println("Done.");
  }
Ejemplo n.º 2
0
  public static void toLibSVM(
      TrainingSample<BxZoneLabel> trainingElement, BufferedWriter fileWriter) throws IOException {
    if (trainingElement.getLabel() == null) {
      return;
    }
    fileWriter.write(String.valueOf(trainingElement.getLabel().ordinal()));
    fileWriter.write(" ");

    Integer featureCounter = 1;
    for (Double value : trainingElement.getFeatureVector().getValues()) {
      StringBuilder sb = new StringBuilder();
      Formatter formatter = new Formatter(sb, Locale.US);
      formatter.format("%d:%.5f", featureCounter++, value);
      fileWriter.write(sb.toString());
      fileWriter.write(" ");
    }
    fileWriter.write("\n");
  }