skfolio.factor_exposure.OneHotCategoricalFactors#
- class skfolio.factor_exposure.OneHotCategoricalFactors(category, *, family)[source]#
One-hot factor exposures from a categorical field.
Expands a categorical field into one factor per category level. The result is an exposure tensor with shape
(n_observations, n_assets, n_factors), wheren_factorsis the number of category levels.For each observation \(t\), asset \(i\) and category factor \(k\), the exposure is:
\[\begin{split}x_{t,i,k} = \begin{cases} 1 & \text{if asset } i \text{ belongs to category } k \\ 0 & \text{otherwise} \end{cases}\end{split}\]Missing category codes produce NaN exposures for all category factors of that asset-observation pair.
- Parameters:
- categorystr
Name of the categorical field in the AssetPanel to one-hot encode. The field must be a
FieldCategorical.- familystr
The factor family this exposure belongs to (e.g., “industry”, “country”). Factor families group related factors for basket-neutral constraints, neutralization, attribution and reporting.
- Attributes:
- factor_names_ndarray of shape (n_factors,)
The category labels corresponding to each one-hot column.
- n_assets_int
Number of assets seen during fitting.
- asset_names_ndarray of shape (n_assets,)
Asset names seen during fitting.
Methods
fit_transform(X[, y])One-hot encode the categorical field.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
partial_fit_transform(X[, y])Stateless class delegation to
fit_transform.set_params(**params)Set the parameters of this estimator.
- fit_transform(X, y=None, **fit_params)[source]#
One-hot encode the categorical field.
- Parameters:
- XAssetPanel
Input panel containing the categorical field as integer codes.
- yNone
Ignored. Present for compatibility with scikit-learn’s API.
- **fit_paramsdict
Additional fit parameters. They are ignored.
- Returns:
- exposuresndarray of shape (n_observations, n_assets, n_factors)
One-hot encoded exposures. Column order matches
X.fields[category].levels. Entries with missing codes (MISSING_CATEGORY_CODE == -1) are filled with NaN.
- Raises:
- IndexError
If any valid code is >= n_levels (indicates data corruption).
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- partial_fit_transform(X, y=None, **fit_params)#
Stateless class delegation to
fit_transform.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.