jsat.io
Class CSV
- java.lang.Object
-
- jsat.io.CSV
-
public class CSV extends java.lang.ObjectProvides a reader and writer for CSV style datasets. This CSV reader supports comments in CSVs (must begin with a single character) and categorical features (columns must be specified when calling). Any number of newlines will be treated as a single newline separating two rows.
When reading and writing a CSV, if the delimiter or comment markers are not specified - the defaults will be usedDEFAULT_DELIMITERandDEFAULT_COMMENTrespectively.
The CSV loader will treat empty columns as missing values for both numeric and categorical features. A value of "NaN" in a numeric column will also be treated as a missing value. Once loaded, missing values for numeric features are encoded asDouble.NaNand as -1 for categorical features.
-
-
Field Summary
Fields Modifier and Type Field and Description static charDEFAULT_COMMENTstatic charDEFAULT_DELIMITER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static DataWritergetWriter(java.io.OutputStream out, CategoricalData[] catInfo, int dim, CategoricalData predicting, DataWriter.DataSetType type)Returns a DataWriter object which can be used to stream a set of arbitrary datapoints into the given output stream.static DataWritergetWriter(java.io.OutputStream out, CategoricalData[] catInfo, int dim, CategoricalData predicting, DataWriter.DataSetType type, char delimiter)Returns a DataWriter object which can be used to stream a set of arbitrary datapoints into the given output stream.static SimpleDataSetread(java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in the given CSV dataset as a simple CSV filestatic SimpleDataSetread(java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in the given CSV dataset as a simple CSV filestatic SimpleDataSetread(java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in the given CSV dataset as a simple CSV filestatic SimpleDataSetread(java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in the given CSV dataset as a simple CSV filestatic ClassificationDataSetreadC(int classification_target, java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a classification dataset.static ClassificationDataSetreadC(int classification_target, java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a classification dataset.static ClassificationDataSetreadC(int classification_target, java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a classification dataset.static ClassificationDataSetreadC(int classification_target, java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a classification dataset.static RegressionDataSetreadR(int numeric_target_column, java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a regression dataset.static RegressionDataSetreadR(int numeric_target_column, java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a regression dataset.static RegressionDataSetreadR(int numeric_target_column, java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a regression dataset.static RegressionDataSetreadR(int numeric_target_column, java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols)Reads in a CSV dataset as a regression dataset.static voidwrite(DataSet<?> data, java.nio.file.Path path)Writes out the given dataset as a CSV file.static voidwrite(DataSet<?> data, java.nio.file.Path path, char delimiter)Writes out the given dataset as a CSV file.static voidwrite(DataSet<?> data, java.io.Writer writer)Writes out the given dataset as a CSV file.static voidwrite(DataSet<?> data, java.io.Writer writer, char delimiter)Writes out the given dataset as a CSV file.
-
-
-
Field Detail
-
DEFAULT_DELIMITER
public static final char DEFAULT_DELIMITER
- See Also:
- Constant Field Values
-
DEFAULT_COMMENT
public static final char DEFAULT_COMMENT
- See Also:
- Constant Field Values
-
-
Method Detail
-
readR
public static RegressionDataSet readR(int numeric_target_column, java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a regression dataset.- Parameters:
numeric_target_column- the column index (starting from zero) of the feature that will be the target regression valuepath- the reader for the CSV contentlines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- the regression dataset from the given CSV file
- Throws:
java.io.IOException
-
readR
public static RegressionDataSet readR(int numeric_target_column, java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a regression dataset.- Parameters:
numeric_target_column- the column index (starting from zero) of the feature that will be the target regression valuereader- the reader for the CSV contentlines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- the regression dataset from the given CSV file
- Throws:
java.io.IOException
-
readR
public static RegressionDataSet readR(int numeric_target_column, java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a regression dataset.- Parameters:
numeric_target_column- the column index (starting from zero) of the feature that will be the target regression valuepath- the CSV file to readdelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- the regression dataset from the given CSV file
- Throws:
java.io.IOException
-
readR
public static RegressionDataSet readR(int numeric_target_column, java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a regression dataset.- Parameters:
numeric_target_column- the column index (starting from zero) of the feature that will be the target regression valuereader- the reader for the CSV contentdelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- the regression dataset from the given CSV file
- Throws:
java.io.IOException
-
readC
public static ClassificationDataSet readC(int classification_target, java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a classification dataset. Comments assumed to start with the "#" symbol.- Parameters:
classification_target- the column index (starting from zero) of the feature that will be the categorical target valuepath- the CSV file to readlines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- the classification dataset from the given CSV file
- Throws:
java.io.IOException
-
readC
public static ClassificationDataSet readC(int classification_target, java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a classification dataset. Comments assumed to start with the "#" symbol.- Parameters:
classification_target- the column index (starting from zero) of the feature that will be the categorical target valuereader- the reader for the CSV contentlines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- the classification dataset from the given CSV file
- Throws:
java.io.IOException
-
readC
public static ClassificationDataSet readC(int classification_target, java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a classification dataset.- Parameters:
classification_target- the column index (starting from zero) of the feature that will be the categorical target valuereader- the reader for the CSV contentdelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- the classification dataset from the given CSV file
- Throws:
java.io.IOException
-
readC
public static ClassificationDataSet readC(int classification_target, java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in a CSV dataset as a classification dataset.- Parameters:
classification_target- the column index (starting from zero) of the feature that will be the categorical target valuepath- the CSV filedelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- the classification dataset from the given CSV file
- Throws:
java.io.IOException
-
read
public static SimpleDataSet read(java.nio.file.Path path, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in the given CSV dataset as a simple CSV file- Parameters:
path- the CSV filelines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- a simple dataset of the given CSV file
- Throws:
java.io.IOException
-
read
public static SimpleDataSet read(java.io.Reader reader, int lines_to_skip, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in the given CSV dataset as a simple CSV file- Parameters:
reader- the reader for the CSV contentlines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)cat_cols- a set of the indices to treat as categorical features.- Returns:
- a simple dataset of the given CSV file
- Throws:
java.io.IOException
-
read
public static SimpleDataSet read(java.nio.file.Path path, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in the given CSV dataset as a simple CSV file- Parameters:
path- the CSV file to readdelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- a simple dataset of the given CSV file
- Throws:
java.io.IOException
-
read
public static SimpleDataSet read(java.io.Reader reader, char delimiter, int lines_to_skip, char comment, java.util.Set<java.lang.Integer> cat_cols) throws java.io.IOException
Reads in the given CSV dataset as a simple CSV file- Parameters:
reader- the reader for the CSV contentdelimiter- the delimiter to separate columns, usually a commalines_to_skip- the number of lines to skip when reading in the CSV (used to skip header information)comment- the character used to indicate the start of a comment. Once this character is reached, anything at and after the character will be ignored.cat_cols- a set of the indices to treat as categorical features.- Returns:
- a simple dataset of the given CSV file
- Throws:
java.io.IOException
-
write
public static void write(DataSet<?> data, java.nio.file.Path path) throws java.io.IOException
Writes out the given dataset as a CSV file. If the given dataset is a regression or classification dataset, the target feature that is being predicted will always be written out as the first index in the CSV.
After that, all numeric features will be written out in order, followed by the categorical features.- Parameters:
data- the dataset object to save as a CSV filepath- the path to write the CSV to- Throws:
java.io.IOException
-
write
public static void write(DataSet<?> data, java.io.Writer writer) throws java.io.IOException
Writes out the given dataset as a CSV file. If the given dataset is a regression or classification dataset, the target feature that is being predicted will always be written out as the first index in the CSV.
After that, all numeric features will be written out in order, followed by the categorical features.- Parameters:
data- the dataset object to save as a CSV filewriter- the output writer to write the CSV to- Throws:
java.io.IOException
-
write
public static void write(DataSet<?> data, java.nio.file.Path path, char delimiter) throws java.io.IOException
Writes out the given dataset as a CSV file. If the given dataset is a regression or classification dataset, the target feature that is being predicted will always be written out as the first index in the CSV.
After that, all numeric features will be written out in order, followed by the categorical features.- Parameters:
data- the dataset object to save as a CSV filepath- the path to write the CSV todelimiter- the delimiter between column values, normally a comma- Throws:
java.io.IOException
-
write
public static void write(DataSet<?> data, java.io.Writer writer, char delimiter) throws java.io.IOException
Writes out the given dataset as a CSV file. If the given dataset is a regression or classification dataset, the target feature that is being predicted will always be written out as the first index in the CSV.
After that, all numeric features will be written out in order, followed by the categorical features.- Parameters:
data- the dataset object to save as a CSV filewriter- the output writer to write the CSV todelimiter- the delimiter between column values, normally a comma- Throws:
java.io.IOException
-
getWriter
public static DataWriter getWriter(java.io.OutputStream out, CategoricalData[] catInfo, int dim, CategoricalData predicting, DataWriter.DataSetType type) throws java.io.IOException
Returns a DataWriter object which can be used to stream a set of arbitrary datapoints into the given output stream. This works in a thread safe manner. Uses the default delimiterDEFAULT_DELIMITER- Parameters:
out- the location to store all the datacatInfo- information about the categorical features to be writtendim- information on how many numeric features existpredicting- information on the class label, may benullif not a classification datasettype- what type of data set (simple, classification, regression) to be written- Returns:
- the DataWriter that the actual points can be streamed through
- Throws:
java.io.IOException
-
getWriter
public static DataWriter getWriter(java.io.OutputStream out, CategoricalData[] catInfo, int dim, CategoricalData predicting, DataWriter.DataSetType type, char delimiter) throws java.io.IOException
Returns a DataWriter object which can be used to stream a set of arbitrary datapoints into the given output stream. This works in a thread safe manner.- Parameters:
out- the location to store all the datacatInfo- information about the categorical features to be writtendim- information on how many numeric features existpredicting- information on the class label, may benullif not a classification datasettype- what type of data set (simple, classification, regression) to be writtendelimiter- the character delimiter between features- Returns:
- the DataWriter that the actual points can be streamed through
- Throws:
java.io.IOException
-
-
DataMelt 3.0 © DataMelt by jWork.ORG