tick.preprocessing.LongitudinalFeaturesProduct

class tick.preprocessing.LongitudinalFeaturesProduct(exposure_type='infinite', n_jobs=-1)[source]

Transforms longitudinal exposure features to add the corresponding product features.

This preprocessor transform an input list of n_cases numpy arrays or csr_matrices of shape (n_intervals, n_features) so as to add columns representing the product of combination of two features. It outputs a list of n_cases numpy arrays or csr_matrices of shape (n_intervals, n_features + comb(n_features, 2)).

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

exposure_type : {‘infinite’, ‘short’}, default=’infinite’

Either ‘infinite’ for infinite unique exposures or ‘short’ for short repeated exposures.

n_jobs : int, default=-1

Number of tasks to run in parallel. If set to -1, the number of tasks is set to the number of cores.

Attributes

mapper : dict

Map product features to column indexes of the resulting matrices.

Examples

>>> from pprint import pprint
>>> from scipy.sparse import csr_matrix
>>> from tick.preprocessing.longitudinal_features_product import LongitudinalFeaturesProduct
>>> infinite_exposures = [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")
...                       ]
>>> lfp = LongitudinalFeaturesProduct(exposure_type="infinite")
>>> product_features, _, _ = lfp.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., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 1.]]), array([[1., 1., 0., 1., 0., 0.],
       [0., 0., 1., 0., 1., 1.],
       [0., 0., 0., 0., 0., 0.]])]
__init__(exposure_type='infinite', n_jobs=-1)[source]

Initialize self. See help(type(self)) for accurate signature.

fit(features, labels=None, censoring=None)[source]

Fit the feature product using the features matrices list.

Parameters

features : list of numpy.ndarray or list of scipy.sparse.csr_matrix,

list of length n_cases, each element of the list of shape=(n_intervals, n_features) The list of features matrices.

Returns

output : LongitudinalFeaturesProduct

The fitted current instance.

transform(features, labels=None, censoring=None)[source]

Add the product features to the given features matrices list.

Parameters

features : list of numpy.ndarray or list of scipy.sparse.csr_matrix,

list of length n_cases, each element of the list of shape=(n_intervals, n_features) The list of features matrices.

Returns

output : list of numpy.ndarray or list of scipy.sparse.csr_matrix,

list of length n_cases, each element of the list of shape=(n_intervals, n_new_features) The list of features matrices with added product features. n_new_features = n_features + comb(n_features, 2)