tick.preprocessing.LongitudinalFeaturesLagger

class tick.preprocessing.LongitudinalFeaturesLagger(n_lags, n_jobs=-1)[source]

Transforms longitudinal exposure features to add columns representing lagged features.

This preprocessor transform an input list of n_cases numpy ndarrays or scipy.sparse.csr_matrices of shape (n_intervals, n_features) so as to add columns representing the lagged exposures. It outputs a list of n_cases numpy arrays or csr_matrices of shape (n_intervals, n_features + sum(n_lags + 1)).

Exposure can take two forms: - short repeated exposures: in that case, each column of the numpy arrays or csr matrices can contain multiple ones, each one representing an exposure for a particular time bucket. - infinite unique exposures: in that case, each column of the numpy arrays or csr matrices can only contain a single one, corresponding to the starting date of the exposure.

Parameters:

n_lags : numpy.ndarray, shape=(n_features,), dtype=”uint64”

Number of lags to compute: the preprocessor adds columns representing lag = 1, …, n_lags[i] for each feature [i]. If n_lags is a null vector, this preprocessor does nothing. n_lags must be non-negative.

Examples

>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> from tick.preprocessing.longitudinal_features_lagger import LongitudinalFeaturesLagger
>>> features = [csr_matrix([[0, 1, 0],
...                         [0, 0, 0],
...                         [0, 0, 1]], dtype="float64"),
...             csr_matrix([[1, 1, 0],
...                         [0, 0, 1],
...                         [0, 0, 0]], dtype="float64")
...             ]
>>> censoring = np.array([3, 2], dtype="uint64")
>>> n_lags = np.array([2, 1, 0], dtype='uint64')
>>> lfl = LongitudinalFeaturesLagger(n_lags)
>>> product_features, _, _ = lfl.fit_transform(features)
>>> # output comes as a list of sparse matrices or 2D numpy arrays
>>> product_features.__class__
<class 'list'>
>>> [x.toarray() for x in product_features]
[array([[0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 1.]]), array([[1., 0., 0., 1., 0., 0.],
       [0., 1., 0., 0., 1., 1.],
       [0., 0., 1., 0., 0., 0.]])]