Package pyliblinear :: Module _liblinear :: Class FeatureMatrix
[frames] | no frames]

Class FeatureMatrix

object --+
         |
        FeatureMatrix

FeatureMatrix

Feature matrix to be used for training or prediction.

Instance Methods
iterable
features(self)
Return the features as iterator of dicts.
FeatureMatrix
from_iterables(cls, labels, features)
Create FeatureMatrix instance from a two separated iterables - labels and features.
iterable
labels(self)
Return the labels as iterator.
FeatureMatrix
load(cls, file)
Create FeatureMatrix instance from a file.
 
save(self, file)
Save FeatureMatrix instance to a file.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods
FeatureMatrix
__new__(cls, iterable, assign_labels=None)
Create FeatureMatrix instance from a single iterable.
Properties
int height
The matrix height (number of labels and vectors).
int width
The matrix width (number of features).

Inherited from object: __class__

Method Details

__new__(cls, iterable, assign_labels=None)
Static Method

 
Create FeatureMatrix instance from a single iterable. If assign_labels is omitted or None, the iterable is expected to provide 2-tuples, containing the label and the accompanying feature vector. If assign_labels is passed and not None, the iterable should only provide the feature vectors. All labels are then assigned to the value of assign_labels.
Parameters:
  • iterable (iterable) - Iterable providing the feature vectors and/or tuples of label and feature vector. See description.
  • assign_labels (int) - Value to be assigned to all labels. In this case the iterable is expected to provide only the feature vectors.
Returns: FeatureMatrix
New feature matrix instance
Overrides: object.__new__

features(self)

 
Return the features as iterator of dicts.
Returns: iterable
The feature vectors

from_iterables(cls, labels, features)

 
Create FeatureMatrix instance from a two separated iterables - labels and features.
Parameters:
  • labels (iterable) - Iterable providing the labels per feature vector (assigned by order)
  • features (iterable) - Iterable providing the feature vector per label (assigned by order)
Returns: FeatureMatrix
New feature matrix instance
Raises:
  • ValueError - The lengths of the iterables differ

labels(self)

 
Return the labels as iterator.
Returns: iterable
The labels

load(cls, file)

 

Create FeatureMatrix instance from a file.

Each line of the file contains the label and the accompanying sparse feature vector, separated by a space/tab sequence. The feature vector consists of index/value pairs. The index and the value are separated by a colon (:). The pairs are separated by space/tab sequences. Accepted line endings are \r, \n and \r\n.

All numbers are represented as strings parsable either as ints (for indexes) or doubles (for values and labels).

Note that the exact I/O exceptions depend on the stream passed in.

Parameters:
  • file (file or str) - Either a readable stream or a filename. If the passed object provides a read attribute/method, it's treated as readable file stream, as a filename otherwise. If it's a stream, the stream is read from the current position and remains open after hitting EOF. In case of a filename, the accompanying file is opened in text mode, read from the beginning and closed afterwards.
Returns: FeatureMatrix
New feature matrix instance
Raises:
  • IOError - Error reading the file
  • ValueError - Error parsing the file

save(self, file)

 

Save FeatureMatrix instance to a file.

Each line of the line of the file contains the label and the accompanying sparse feature vector, separated by a space. The feature vector consists of index/value pairs. The index and the value are separated by a colon (:). The pairs are separated by a space again. The line ending is \n.

All numbers are represented as strings parsable either as ints (for indexes) or doubles (for values and labels).

Note that the exact I/O exceptions depend on the stream passed in.

Parameters:
  • file (file or str) - Either a writeable stream or a filename. If the passed object provides a write attribute/method, it's treated as writeable stream, as a filename otherwise. If it's a stream, the stream is written to the current position and remains open when done. In case of a filename, the accompanying file is opened in text mode, truncated, written from the beginning and closed afterwards.
Raises:
  • IOError - Error writing the file

Property Details

height

The matrix height (number of labels and vectors).
Type:
int

width

The matrix width (number of features).
Type:
int