Inference

A PredictSession provides access to making prediction from saved models. Predictions for a single point are stored in the Prediction.

PredictSession

class smurff.PredictSession(h5_fname)

TrainSession for making predictions using a model generated using a TrainSession.

A PredictSession can be made directly from a TrainSession

>>> predict_session  = train_session.makePredictSession()

or from an HDF5 file

>>> predict_session = PredictSession("saved_output.hdf5")
predict(operands, samples=None)

Generate predictions on operands. Parameters specify coordinates of sideinfo/features for each dimension.

Parameters:
  • operands (tuple) –

    A combination of coordindates in the matrix/tensor and/or features you want to use to make predictions. len(operands) should be equal to number of dimensions in the sample. Each element operands can be a:

    • int : a single element in this dimension is selected. For example, a single row or column in a matrix.
    • slice : a slice is selected in this dimension. For example, a number of rows or columns in a matrix.
    • Ellipsis or None: all elements in this dimension are selected. For example, all rows or columns in a matrix.
    • numpy.ndarray : 2D numpy array used as dense sideinfo. Each row vector is used as side-info.
    • scipy.sparse.spmatrix : sparse matrix used as sideinfo. Each row vector is used as side-info.
  • samples (range or None) – Range of samples to use for prediction, or None for all samples
Returns:

list of N numpy.ndarray`s of shape `[ T1 x T2 x ... ] where N is the number of samples in this PredictSession and T1 x T2 x …

Return type:

lis of numpy.ndarray

predict_all()

Computes prediction matrix/tensor for full train data shape.

Returns:N numpy.ndarray`s of shape `[ T1 x T2 x ... ] where N is the number of samples in this PredictSession and T1 x T2 x … is the shape of the train data
Return type:list of numpy.ndarray
predict_sparse(test_matrix)

Computes prediction for all elements in a sparse test matrix

Parameters:test_matrix (scipy sparse matrix) –
Returns:list of N scipy.sparse.sp_matrix objects, where N is the number of samples
Return type:list

Prediction

class smurff.Prediction(coords, val, pred_1sample=nan, pred_avg=nan, var=nan, nsamples=-1)

Stores predictions for a single point in the matrix/tensor

coords

Position of this prediction in the train matrix/tensor

Type:shape
val

True value or “nan” if no true value is known

Type:float
nsamples

Number of samples collected to make this prediction

Type:int
pred_1sample

Predicted value using only the last sample

Type:float
pred_avg

Predicted value using the average prediction across all samples

Type:float
var

Variance amongst predictions across all samples

Type:float
pred_all

List of predictions, one for each sample

Type:list
static fromTestMatrix(test_matrix_or_tensor)

Creates a list of predictions from a scipy sparse matrix”

Parameters:test_matrix (scipy sparse matrix) –
Returns:List of Prediction. Only the coordinate and true value is filled.
Return type:list