skfolio.utils.tools.input_to_array#

skfolio.utils.tools.input_to_array(items, n_assets, fill_value, dim, assets_names, name, investable_mask=None)[source]#

Convert a collection of items (array-like or dictionary) into a numpy array and verify its shape.

When investable_mask is provided, dictionary inputs are resolved against the full assets_names and then subsetted, while array inputs sized to the full universe are sliced down to the investable assets. This allows callers to pass user-facing parameters (keyed by asset name or sized for the full universe) and transparently obtain arrays sized for the investable subset.

Parameters:
itemsnp.ndarray | dict | list

Items to verify and convert to array.

n_assetsint

Expected number of assets in the output array (i.e. the investable count when investable_mask is provided).

fill_valueAny

When items is a dictionary, elements that are not in asset_names are filled with fill_value in the converted array.

dimint

Dimension of the final array. Possible values are 1 or 2.

assets_namesndarray, optional

Asset names used when items is a dictionary. When investable_mask is provided, this must contain the full-universe names.

namestr

Name of the items used for error messages.

investable_maskndarray of shape (n_full_assets,), optional

Boolean mask selecting investable assets from the full universe. When provided, dictionary inputs are first resolved against the full universe then sliced, and array-like inputs sized to the full universe are sliced along the last axis.

Returns:
valuesndarray of shape (n_assets) for dim=1 or (n_groups, n_assets) for dim=2

Converted array.