<a id="skfolio-seriation-hierarchicalseriation"></a>

# skfolio.seriation.HierarchicalSeriation

<a id="skfolio.seriation.HierarchicalSeriation"></a>

### *class* skfolio.seriation.HierarchicalSeriation(\*, hierarchical_clustering_estimator=None, optimal_ordering=True)

Order assets by the leaves of a hierarchical clustering tree.

* **Parameters:**
  **hierarchical_clustering_estimator** *HierarchicalClustering, optional*
  : Hierarchical clustering estimator, including compatible subclasses.
    Each `fit` trains a fresh clone when at least two assets are investable.
    The default (`None`) uses [`HierarchicalClustering`](https://skfolio.org/generated/skfolio.cluster.HierarchicalClustering.html.md#skfolio.cluster.HierarchicalClustering)
    with Ward linkage.

  **optimal_ordering** *bool, default=True*
  : If True, minimize distances between adjacent leaves without changing
    the tree [[1]](#r6dae3571cd8c-1). If False, use the leaf order produced by the clustering tree.
* **Attributes:**
  **ordering_** *ndarray of shape (n_investable_assets,)*
  : Positions in the original input matrix, listed in the computed order.
    Each investable asset appears exactly once. Empty when no assets are
    investable. A single investable asset produces its original position.

  **investable_mask_** *ndarray of shape (n_assets,)*
  : Boolean mask selecting investable assets for the current ordering.

  **hierarchical_clustering_estimator_** *HierarchicalClustering or None*
  : Fitted clustering estimator, or None with fewer than two investable assets.

  **ordered_linkage_matrix_** *ndarray of shape (max(n_investable_assets - 1, 0), 4)*
  : Linkage matrix with the selected leaf ordering. Leaf indices refer to
    the compact input order, given by `np.flatnonzero(investable_mask_)`.

  **n_features_in_** *int*
  : Number of assets in the full schema.

  **feature_names_in_** *ndarray of shape (`n_features_in_`,)*
  : Asset names, defined when the input names are all strings.

### Methods

| [`fit`](#skfolio.seriation.HierarchicalSeriation.fit)(X[, y])            | Start a new hierarchical ordering from a distance snapshot.   |
|-------------------------------------------------------------------------|---------------------------------------------------------------|
| [`get_metadata_routing`](#skfolio.seriation.HierarchicalSeriation.get_metadata_routing)() | Route fitting metadata to the clustering estimator.           |
| [`get_params`](#skfolio.seriation.HierarchicalSeriation.get_params)([deep])     | Get parameters for this estimator.                            |
| [`set_params`](#skfolio.seriation.HierarchicalSeriation.set_params)(\*\*params) | Set the parameters of this estimator.                         |

### References

* <a id='r6dae3571cd8c-1'>**[1]**</a> “Fast optimal leaf ordering for hierarchical clustering”. Ziv Bar-Joseph, David K. Gifford and Tommi S. Jaakkola, Bioinformatics (2001).

<a id="skfolio.seriation.HierarchicalSeriation.fit"></a>

#### fit(X, y=None, \*\*fit_params)

Start a new hierarchical ordering from a distance snapshot.

* **Parameters:**
  **X** *array-like of shape (n_assets, n_assets)*
  : Distance snapshot. NaN diagonal entries mark non-investable assets.

  **y** *Ignored*
  : Not used, present for API consistency by convention.

  **\*\*fit_params** *dict*
  : Parameters to pass to the clustering estimator.
    Only available if `enable_metadata_routing=True`, which can be
    set by using `sklearn.set_config(enable_metadata_routing=True)`.
    See [Metadata Routing User Guide](https://skfolio.org/user_guide/metadata_routing.html.md#metadata-routing) for
    more details.
* **Returns:**
  **self** *HierarchicalSeriation*
  : Fitted estimator.

<a id="skfolio.seriation.HierarchicalSeriation.get_metadata_routing"></a>

#### get_metadata_routing()

Route fitting metadata to the clustering estimator.

<a id="skfolio.seriation.HierarchicalSeriation.get_params"></a>

#### get_params(deep=True)

Get parameters for this estimator.

* **Parameters:**
  **deep** *bool, default=True*
  : If True, will return the parameters for this estimator and
    contained subobjects that are estimators.
* **Returns:**
  **params** *dict*
  : Parameter names mapped to their values.

<a id="skfolio.seriation.HierarchicalSeriation.set_params"></a>

#### 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:**
  **\*\*params** *dict*
  : Estimator parameters.
* **Returns:**
  **self** *estimator instance*
  : Estimator instance.

