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

Class Model

object --+
         |
        Model

Model()

Classification model. Use its Model.load or Model.train methods to construct a new instance

Instance Methods
Model
load(cls, file, mmap=False)
Create Model instance from a file (previously created by Model.save())
iterable
predict(self, matrix, label_only=True, probability=False)
Run the model on matrix and predict labels.
 
save(self, file)
Save Model instance to a file.
Model
train(cls, matrix, solver=None, bias=None)
Create model instance from a training run

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

Properties
double bias
Bias used to create the model
bool is_probability
Is model a probability model?
bool is_regression
Is model a regression model?
str solver_type
Solver type used to create the model

Inherited from object: __class__

Method Details

load(cls, file, mmap=False)

 

Create Model instance from a file (previously created by Model.save())

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.
  • mmap (bool) - Load the model into a file-backed memory area? Default: false
Returns: Model
New model instance
Raises:
  • IOError - Error reading the file
  • ValueError - Error parsing the file

predict(self, matrix, label_only=True, probability=False)

 
Run the model on matrix and predict labels.
Parameters:
  • matrix (pyliblinear.FeatureMatrix or iterable) - Either a feature matrix or a simple iterator over feature vectors to inspect and predict upon.
  • label_only (bool) - Return the label only? If false, the decision dict for all labels is returned as well.
  • probability (bool) - Use probability estimates?
Returns: iterable
Result iterator. Either over labels or over label/decision dict tuples.

save(self, file)

 

Save Model instance to a file.

After some basic information about solver type, dimensions and labels the model matrix is stored as a sequence of doubles per line. The matrix is transposed, so the height is the number of features (including the bias feature) and the width is the number of classes.

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

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

train(cls, matrix, solver=None, bias=None)

 
Create model instance from a training run
Parameters:
  • matrix (pyliblinear.FeatureMatrix) - Feature matrix to use for training
  • solver (pyliblinear.Solver) - Solver instance. If omitted or None, a default solver is picked.
  • bias (float) - Bias to the hyperplane. Of omitted or None, no bias is applied. bias >= 0.
Returns: Model
New model instance

Property Details

bias

Bias used to create the model

None if no bias was applied.

Type:
double

is_probability

Is model a probability model?
Type:
bool

is_regression

Is model a regression model?
Type:
bool

solver_type

Solver type used to create the model
Type:
str