Click or drag to resize
Accord.NET (logo)

SparseReader Class

Reader for data files containing samples in libsvm's sparse format.
Inheritance Hierarchy
SystemObject
  Accord.IOSparseReader

Namespace:  Accord.IO
Assembly:  Accord.IO (in Accord.IO.dll) Version: 3.8.0
Syntax
public class SparseReader : IDisposable
Request Example View Source

The SparseReader type exposes the following members.

Constructors
  NameDescription
Public methodSparseReader(Stream)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(StreamReader)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(String)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(Stream, Int32)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(Stream, Encoding)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(StreamReader, Int32)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(String, Int32)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(String, Encoding)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(Stream, Encoding, Int32)
Initializes a new instance of the SparseReader class.
Public methodSparseReader(String, Encoding, Int32)
Initializes a new instance of the SparseReader class.
Top
Properties
  NameDescription
Public propertyBaseStream
Returns the underlying stream.
Public propertyDimensions Obsolete.
Obsolete. Please use NumberOfInputs instead.
Public propertyEndOfStream
Gets a value that indicates whether the current stream position is at the end of the stream.
Public propertyIntercept
Gets or sets whether to include an intercept term (bias) value at the beginning of each new sample. Default is null (don't include anything).
Public propertyNumberOfInputs
Gets the number of features present in this dataset. Please note that, when using the sparse representation, it is not strictly necessary to know this value.
Public propertySampleDescriptions
Gets the description associated with the last read values.
Top
Methods
  NameDescription
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Releases unmanaged resources and performs other cleanup operations before the SparseReader is reclaimed by garbage collection.
(Overrides ObjectFinalize.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRead(SparseDouble, Boolean)
Public methodRead(SparseDouble, Double)
Public methodRead(SparseDouble, Int32)
Public methodRead(Int32, SparseDouble, Boolean)
Reads count samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodRead(Int32, SparseDouble, Double)
Reads count samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodRead(Int32, SparseDouble, Int32)
Reads count samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadDense
Reads a sample from the file and returns it as a dense vector, together with its associated output value.
Public methodReadDense(Int32)
Reads count samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadDenseToEnd
Reads all samples from the file and returns them as a dense vector, together with their associated output values.
Public methodReadLine
Reads one line from the feature file, returning the array of values for the sparse vector and its corresponding label.
Public methodReadSparse
Public methodReadSparse(Int32)
Reads count samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadSparseToEnd
Reads all samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadToEnd(SparseDouble, Boolean)
Reads all samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadToEnd(SparseDouble, Double)
Reads all samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodReadToEnd(SparseDouble, Int32)
Reads all samples from the file and returns them as a SparseT sparse vector, together with their associated output values.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Examples

The following example shows how to read all sparse samples from a file and retrieve them as a dense multidimensional vector.

// Suppose we are going to read a sparse sample file containing
//  samples which have an actual dimension of 4. Since the samples
//  are in a sparse format, each entry in the file will probably
//  have a much lesser number of elements.
// 
int sampleSize = 4;

// Create a new Sparse Sample Reader to read any given file,
//  passing the correct dense sample size in the constructor
// 
SparseReader reader = new SparseReader(file, Encoding.Default, sampleSize);

// Declare a vector to obtain the label
//  of each of the samples in the file
// 
int[] labels = null;

// Declare a vector to obtain the description (or comments)
//  about each of the samples in the file, if present.
// 
string[] descriptions = null;

// Read the sparse samples and store them in a dense vector array
double[][] samples = reader.ReadToEnd(out labels, out descriptions);

Additionally, it is also possible to read each sample individually and sequentially. For this, we can use a while loop until we reach the end of the stream.

// Suppose we are going to read a sparse sample file containing
//  samples which have an actual dimension of 4. Since the samples
//  are in a sparse format, each entry in the file will probably
//  have a much lesser number of elements.
// 
int sampleSize = 4;

// Create a new Sparse Sample Reader to read any given file,
//  passing the correct dense sample size in the constructor
// 
SparseReader reader = new SparseReader(file, Encoding.Default, sampleSize);

// Declare some variables to receive each sample
// 
int label = 0;
string description;
double[] sample;

// Read a single sample from the file
sample = reader.ReadDense(out label, out description);

// Read all other samples from the file
while (!reader.EndOfStream)
{
    sample = reader.ReadDense(out label, out description);
}
See Also