Linear

Some words about linear functions.

Available optimizations

There are currently no optimizations available for linear functions. Check out Extending fastinference on how to add an optimization!

The Linear object

class fastinference.models.Linear.Linear(classes, n_features, accuracy=None, name='Model')

A placeholder for all linear models. There is nothing fancy going on here. This class stores the coefficients of the linear function in self.coeff and the bias/intercept in self.intercept.

__init__(classes, n_features, accuracy=None, name='Model')

Constructor of a linear model.

Parameters
  • classes (int) – The class mappings. Each enty maps the given entry to the corresponding index so that the i-th output of the model belongs to class classes[i]. For example with classes = [1,0,2] the second output of the model maps to class 0, the first output to class 1 and the third output to class 2. n_features (list of int): The number of features this model was trained on.

  • model_accuracy (float, optional) – The accuracy of this tree on some test data. Can be used to verify the correctness of the implementation. Defaults to None.

  • name (str, optional) – The name of this model. Defaults to “Model”.

classmethod from_dict(data)

Generates a new linear model from the given dictionary. It is assumed that a linear model has previously been stored with the Linear.to_dict() method.

Parameters

data (dict) – The dictionary from which this linear model should be generated.

Returns

The newly generated linear model.

Return type

Tree

classmethod from_sklearn(sk_model, name='model', accuracy=None)

Generates a new linear model from sklearn.

Parameters
  • sk_model (LinearModel) – A LinearModel trained in sklearn (e.g. SGDClassifier, RidgeClassifier, Perceptron, etc.).

  • name (str, optional) – The name of this model. Defaults to “Model”.

  • accuracy (float, optional) – The accuracy of this tree on some test data. Can be used to verify the correctness of the implementation. Defaults to None.

Returns

The newly generated linear model.

Return type

Linear

predict_proba(X)

Applies this linear model to the given data and provides the predicted probabilities for each example in X.

Parameters

X (numpy.array) – A (N,d) matrix where N is the number of data points and d is the feature dimension. If X has only one dimension then a single example is assumed and X is reshaped via X = X.reshape(1,X.shape[0])

Returns

A (N, c) prediction matrix where N is the number of data points and c is the number of classes

Return type

numpy.array

to_dict()

Stores this linear model as a dictionary which can be loaded with Linear.from_dict().

Returns

The dictionary representation of this linear model.

Return type

dict