public static void writeFile(Instances dataSet, String path, String fileName, String extension) {
   /*
   path : "dataSet\\"
   filename : "accelerometer_instances"
   extension : "csv"
   */
   try {
     if (extension.equals("arff")) {
       ArffSaver arffSaver = new ArffSaver();
       arffSaver.setInstances(dataSet);
       arffSaver.setFile(new File(path + fileName + "." + extension));
       arffSaver.writeBatch();
     } else if (extension.equals("csv")) {
       CSVSaver csvSaver = new CSVSaver();
       csvSaver.setInstances(dataSet);
       csvSaver.setFile(new File(path + fileName + "." + extension));
       csvSaver.writeBatch();
     } else {
       System.out.println("arff or csv only!");
       System.exit(-1);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 public static void saveInstances(Instances dataSet, String fileName) throws Exception {
   ArffSaver saver = new ArffSaver();
   saver.setInstances(dataSet);
   saver.setFile(new File(fileName));
   // saver.setDestination(new File("./data/test.arff"));   // **not** necessary in 3.5.4 and later
   saver.writeBatch();
 }
 public void saveInstances() {
   if (isExternalStorageAvailable()) {
     try {
       ArffSaver saver = new ArffSaver();
       saver.setInstances(instances);
       saver.setFile(file);
       saver.writeBatch();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
  public static void main(String[] args) throws Exception {

    if (args.length != 2) {
      System.err.println("Usage: FeatureGenerator input-badges-file features-file");
      System.exit(-1);
    }
    Instances data = readData(args[0]);
    ArffSaver saver = new ArffSaver();
    saver.setInstances(data);
    saver.setFile(new File(args[1]));
    saver.writeBatch();
  }
  protected static void pca(Instances data) throws Exception {
    PrincipalComponents pca = new PrincipalComponents();
    pca.setInputFormat(data);
    pca.setMaximumAttributes(4);

    Instances newData = Filter.useFilter(data, pca);
    System.out.println("newdata" + newData);

    ArffSaver saver = new ArffSaver();
    saver.setInstances(newData);
    saver.setFile(new File("PCAresult.arff"));
    saver.writeBatch();
  }
  public static void main(String[] args) throws Exception {
    if (args.length != 3)
      throw new IllegalArgumentException(
          "Required parameters: <input> <attribute_indices> <output>");

    System.out.println("Loading input data: " + args[0]);
    Instances input = DataSource.read(args[0]);

    System.out.println("Applying filter using indices: " + args[1]);
    MekaClassAttributes filter = new MekaClassAttributes();
    filter.setAttributeIndices(args[1]);
    filter.setInputFormat(input);
    Instances output = Filter.useFilter(input, filter);

    System.out.println("Saving filtered data to: " + args[2]);
    ArffSaver saver = new ArffSaver();
    saver.setFile(new File(args[2]));
    DataSink.write(saver, output);
  }
  public QSARModel train(Instances data) throws QSARException {

    // GET A UUID AND DEFINE THE TEMPORARY FILE WHERE THE TRAINING DATA
    // ARE STORED IN ARFF FORMAT PRIOR TO TRAINING.
    final String rand = java.util.UUID.randomUUID().toString();
    final String temporaryFilePath = ServerFolders.temp + "/" + rand + ".arff";
    final File tempFile = new File(temporaryFilePath);

    // SAVE THE DATA IN THE TEMPORARY FILE
    try {
      ArffSaver dataSaver = new ArffSaver();
      dataSaver.setInstances(data);
      dataSaver.setDestination(new FileOutputStream(tempFile));
      dataSaver.writeBatch();
      if (!tempFile.exists()) {
        throw new IOException("Temporary File was not created");
      }
    } catch (final IOException ex) {
      /*
       * The content of the dataset cannot be
       * written to the destination file due to
       * some communication issue.
       */
      tempFile.delete();
      throw new RuntimeException(
          "Unexpected condition while trying to save the " + "dataset in a temporary ARFF file",
          ex);
    }

    NaiveBayes classifier = new NaiveBayes();

    String[] generalOptions = {
      "-c",
      Integer.toString(data.classIndex() + 1),
      "-t",
      temporaryFilePath,
      /// Save the model in the following directory
      "-d",
      ServerFolders.models_weka + "/" + uuid
    };

    try {
      Evaluation.evaluateModel(classifier, generalOptions);
    } catch (final Exception ex) {
      tempFile.delete();
      throw new QSARException(
          Cause.XQReg350,
          "Unexpected condition while trying to train "
              + "an SVM model. Possible explanation : {"
              + ex.getMessage()
              + "}",
          ex);
    }

    QSARModel model = new QSARModel();

    model.setParams(getParameters());
    model.setCode(uuid.toString());
    model.setAlgorithm(YaqpAlgorithms.NAIVE_BAYES);
    model.setDataset(datasetUri);
    model.setModelStatus(ModelStatus.UNDER_DEVELOPMENT);

    ArrayList<Feature> independentFeatures = new ArrayList<Feature>();
    for (int i = 0; i < data.numAttributes(); i++) {
      Feature f = new Feature(data.attribute(i).name());
      if (data.classIndex() != i) {
        independentFeatures.add(f);
      }
    }

    Feature dependentFeature = new Feature(data.classAttribute().name());
    Feature predictedFeature = dependentFeature;
    model.setDependentFeature(dependentFeature);
    model.setIndependentFeatures(independentFeatures);
    model.setPredictionFeature(predictedFeature);
    tempFile.delete();
    return model;
  }