Java Code Examples for weka.core.converters.CSVSaver

The following examples show how to use weka.core.converters.CSVSaver. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Load the embedding from a given arff file. First converts the ARFF to a temporary CSV file and
 * continues the loading mechanism with the CSV file afterwards
 *
 * @param path Path to the ARFF file
 */
private void loadEmbeddingFromArff(String path) {
  // Try loading ARFF file
  try {
    Instances insts = new Instances(new FileReader(path));
    CSVSaver saver = new CSVSaver();
    saver.setFieldSeparator(" ");
    saver.setInstances(insts);
    final File tmpFile =
        Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString() + ".csv")
            .toFile();
    saver.setFile(tmpFile);
    saver.setNoHeaderRow(true);
    saver.writeBatch();
    loadEmbeddingFromCSV(tmpFile);
    tmpFile.delete();
  } catch (Exception e) {
    throw new RuntimeException(
        "ARFF file could not be read (" + wordVectorLocation.getAbsolutePath() + ")", e);
  }
}
 
Example 2
/**
 * Load the embedding from a given arff file. First converts the ARFF to a temporary CSV file and
 * continues the loading mechanism with the CSV file afterwards
 *
 * @param path Path to the ARFF file
 */
private void loadEmbeddingFromArff(String path) {
  // Try loading ARFF file
  try {
    Instances insts = new Instances(new FileReader(path));
    CSVSaver saver = new CSVSaver();
    saver.setFieldSeparator(" ");
    saver.setInstances(insts);
    final File tmpFile =
        Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString() + ".csv")
            .toFile();
    saver.setFile(tmpFile);
    saver.setNoHeaderRow(true);
    saver.writeBatch();
    loadEmbeddingFromCSV(tmpFile);
    tmpFile.delete();
  } catch (Exception e) {
    throw new RuntimeException(
        "ARFF file could not be read (" + wordVectorLocation.getAbsolutePath() + ")", e);
  }
}
 
Example 3
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    ArffLoader loader = new ArffLoader();
    loader.setSource(new File("/Users/admin/Documents/NetBeansProjects/Datasets/weather.arff"));
    Instances data = loader.getDataSet();
    
    CSVSaver saver = new CSVSaver();
    saver.setInstances(data);
    
    saver.setFile(new File("weather.csv"));
    saver.writeBatch();
}
 
Example 4
Source Project: bestconf   Source File: DataIOFile.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Save @param data to the CSV file at @param path
 */
public static void saveDataToCsvFile(String path, Instances data) throws IOException{
	    System.out.println("\nSaving to file " + path + "...");
	    CSVSaver saver = new CSVSaver();
	    saver.setInstances(data);
	    saver.setFile(new File(path));
	    saver.writeBatch();
}
 
Example 5
Source Project: meka   Source File: SaveCSV.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);

	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			Instances performance = (Instances) result.getMeasurement(IncrementalPerformance.RESULTS_SAMPLED_OVER_TIME);

			int retVal = getFileChooser().showSaveDialog(null);
			if (retVal != MekaFileChooser.APPROVE_OPTION)
				return;
			File file = getFileChooser().getSelectedFile();


			try {

				CSVSaver saver = new CSVSaver();
				saver.setInstances(performance);
				saver.setFile(getFileChooser().getSelectedFile());
				saver.writeBatch();
			} catch (Exception ex) {
				String msg = "Failed to write to '" + file + "'!";
				System.err.println(msg);
				ex.printStackTrace();
				JOptionPane.showMessageDialog( null, msg + "\n" + e);
			}
		}
	};
}
 
Example 6
Source Project: meka   Source File: SavePredictions.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the action lister to use in the menu.
 *
 * @param history   the current history
 * @param index     the selected history item
 * @return          the listener
 */
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
	final Result result = history.getResultAt(index);
	return new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			int retVal = getFileChooser().showSaveDialog(null);
			if (retVal != MekaFileChooser.APPROVE_OPTION)
				return;
			File file = getFileChooser().getSelectedFile();

			try {

				CSVSaver saver = new CSVSaver();
				Instances performance = Result.getPredictionsAsInstances(result);
				saver.setInstances(performance);
				saver.setFile(getFileChooser().getSelectedFile());
				saver.writeBatch();
			} catch (Exception ex) {
				String msg = "Failed to write to '" + file + "'!";
				System.err.println(msg);
				ex.printStackTrace();
				JOptionPane.showMessageDialog( null, msg + "\n" + e);
			}
		}
	};
}