weka.core.converters.CSVSaver Java Examples

The following examples show how to use weka.core.converters.CSVSaver. 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 check out the related API usage on the sidebar.
Example #1
Source File: Arff2CSV.java    From Hands-On-Artificial-Intelligence-with-Java-for-Beginners with MIT License 6 votes vote down vote up
/**
 * @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 #2
Source File: AbstractTextEmbeddingIterator.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: AbstractTextEmbeddingIterator.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #4
Source File: DataIOFile.java    From bestconf with 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 File: SaveCSV.java    From meka with 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 File: SavePredictions.java    From meka with 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);
			}
		}
	};
}