jsat.io
Class LIBSVMLoader
- java.lang.Object
-
- jsat.io.LIBSVMLoader
-
public class LIBSVMLoader extends java.lang.ObjectLoads a LIBSVM data file into aDataSet. LIVSM files do not indicate whether or not the target variable is supposed to be numerical or categorical, so two different loading methods are provided. For a LIBSVM file to be loaded correctly, it must match the LIBSVM spec without extensions.
Each line should begin with a numeric value. This is either a regression target or a class label.
Then, for each non zero value in the data set, a space should precede an integer value index starting from 1 followed by a colon ":" followed by a numeric feature value.
The single space at the beginning should be the only space. There should be no double spaces in the file.
LIBSVM files do not explicitly specify the length of data vectors. This can be problematic if loading a testing and training data set, if the data sets do not include the same highest index as a non-zero value, the data sets will have incompatible vector lengths. To resolve this issue, use the loading methods that include the optionalvectorLengthparameter to specify the length before hand.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static DataWritergetWriter(java.io.OutputStream out, int dim, DataWriter.DataSetType type)Returns a DataWriter object which can be used to stream a set of arbitrary datapoints into the given output stream.static ClassificationDataSetloadC(java.io.File file)Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target valuestatic ClassificationDataSetloadC(java.io.File file, double sparseRatio)Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target valuestatic ClassificationDataSetloadC(java.io.File file, double sparseRatio, int vectorLength)Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target valuestatic ClassificationDataSetloadC(java.io.InputStreamReader isr, double sparseRatio)Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target valuestatic ClassificationDataSetloadC(java.io.Reader reader, double sparseRatio, int vectorLength)Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target valuestatic RegressionDataSetloadR(java.io.File file)Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predictstatic RegressionDataSetloadR(java.io.File file, double sparseRatio)Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predictstatic RegressionDataSetloadR(java.io.File file, double sparseRatio, int vectorLength)Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predictstatic RegressionDataSetloadR(java.io.InputStreamReader isr, double sparseRatio)Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predictstatic RegressionDataSetloadR(java.io.Reader reader, double sparseRatio, int vectorLength)Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict.static voidwrite(ClassificationDataSet data, java.io.OutputStream os)Writes out the given classification data set as a LIBSVM data filestatic voidwrite(RegressionDataSet data, java.io.OutputStream os)Writes out the given regression data set as a LIBSVM data file
-
-
-
Method Detail
-
loadR
public static RegressionDataSet loadR(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict- Parameters:
file- the file to load- Returns:
- a regression data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadR
public static RegressionDataSet loadR(java.io.File file, double sparseRatio) throws java.io.FileNotFoundException, java.io.IOException
Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict- Parameters:
file- the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparse- Returns:
- a regression data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadR
public static RegressionDataSet loadR(java.io.File file, double sparseRatio, int vectorLength) throws java.io.FileNotFoundException, java.io.IOException
Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict- Parameters:
file- the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparsevectorLength- the pre-determined length of each vector. If given a negative value, the largest non-zero index observed in the data will be used as the length.- Returns:
- a regression data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadR
public static RegressionDataSet loadR(java.io.InputStreamReader isr, double sparseRatio) throws java.io.IOException
Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict- Parameters:
isr- the input stream for the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparse- Returns:
- a regression data set
- Throws:
java.io.IOException- if an error occurred reading the input stream
-
loadR
public static RegressionDataSet loadR(java.io.Reader reader, double sparseRatio, int vectorLength) throws java.io.IOException
Loads a new regression data set from a LIBSVM file, assuming the label is a numeric target value to predict.- Parameters:
reader- the reader for the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparsevectorLength- the pre-determined length of each vector. If given a negative value, the largest non-zero index observed in the data will be used as the length.- Returns:
- a regression data set
- Throws:
java.io.IOException
-
loadC
public static ClassificationDataSet loadC(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target value- Parameters:
file- the file to load- Returns:
- a classification data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadC
public static ClassificationDataSet loadC(java.io.File file, double sparseRatio) throws java.io.FileNotFoundException, java.io.IOException
Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target value- Parameters:
file- the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparse- Returns:
- a classification data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadC
public static ClassificationDataSet loadC(java.io.File file, double sparseRatio, int vectorLength) throws java.io.FileNotFoundException, java.io.IOException
Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target value- Parameters:
file- the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparsevectorLength- the pre-determined length of each vector. If given a negative value, the largest non-zero index observed in the data will be used as the length.- Returns:
- a classification data set
- Throws:
java.io.FileNotFoundException- if the file was not foundjava.io.IOException- if an error occurred reading the input stream
-
loadC
public static ClassificationDataSet loadC(java.io.InputStreamReader isr, double sparseRatio) throws java.io.IOException
Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target value- Parameters:
isr- the input stream for the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparse- Returns:
- a classification data set
- Throws:
java.io.IOException- if an error occurred reading the input stream
-
loadC
public static ClassificationDataSet loadC(java.io.Reader reader, double sparseRatio, int vectorLength) throws java.io.IOException
Loads a new classification data set from a LIBSVM file, assuming the label is a nominal target value- Parameters:
reader- the input stream for the file to loadsparseRatio- the fraction of non zero values to qualify a data point as sparsevectorLength- the pre-determined length of each vector. If given a negative value, the largest non-zero index observed in the data will be used as the length.- Returns:
- a classification data set
- Throws:
java.io.IOException- if an error occurred reading the input stream
-
write
public static void write(ClassificationDataSet data, java.io.OutputStream os)
Writes out the given classification data set as a LIBSVM data file- Parameters:
data- the data set to write to a fileos- the output stream to write to. The stream will not be closed or flushed by this method
-
write
public static void write(RegressionDataSet data, java.io.OutputStream os)
Writes out the given regression data set as a LIBSVM data file- Parameters:
data- the data set to write to a fileos- the output stream to write to. The stream will not be closed or flushed by this method
-
getWriter
public static DataWriter getWriter(java.io.OutputStream out, int dim, 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.
Categorical information dose not need to be specified since LIBSVM files can't store categorical features.- Parameters:
out- the location to store all the datadim- information on how many numeric features existtype- 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
-
-
DataMelt 3.0 © DataMelt by jWork.ORG