milia.recommend.baselines#

Baselines a retrieval method has to beat.

The population mean is scikit-learn’s DummyRegressor(strategy="mean"). GroupMean predicts the mean outcome of the query’s group, the groups coming from a clusterer (archetypes) or from a column of X.

Classes

GroupMean([clusterer, column])

Mean outcome of the query's group, falling back to the overall mean for unseen groups.

class milia.recommend.baselines.GroupMean(clusterer=None, column=None)#

Bases: RegressorMixin, BaseEstimator

Mean outcome of the query’s group, falling back to the overall mean for unseen groups.

Parameters:
  • clusterer (BaseEstimator | None) – Clusterer whose labels define the groups (fitted on the training rows, then predict assigns queries). Used when column is None; a 5-cluster k-means by default.

  • column (int | None) – Column of X holding a group label, instead of a clusterer.

Examples

>>> X = np.array([[0.0], [0.2], [9.0], [9.2]])
>>> from milia import make
>>> GroupMean(clusterer=make("clusterer", "kmeans", n_clusters=2)).fit(X, [1.0, 3.0, 10.0, 12.0]).predict(
...     [[0.1]]
... ).tolist()
[2.0]
groups(X, fit=False)#

Group label of each row: a column of X or the fitted clusterer’s assignment.

Parameters:
  • X (ndarray) – Features.

  • fit (bool) – Fit the clusterer on these rows first.

Return type:

ndarray

Returns:

One group label per row.

fit(X, y)#

Learn each group’s mean outcome.

Parameters:
  • X (ArrayLike) – Features (with the group column when column is set).

  • y (ArrayLike) – Outcomes.

Return type:

GroupMean

Returns:

The fitted baseline.

predict(X)#

Each row’s group mean, or the overall mean for a group not seen in fit.

Parameters:

X (ArrayLike) – Features.

Return type:

ndarray

Returns:

One prediction per row.

Examples

>>> import numpy as np
>>> model = GroupMean(column=0).fit(np.array([[0.0], [0.0], [1.0]]), np.array([1.0, 3.0, 10.0]))
>>> model.predict(np.array([[0.0], [1.0], [7.0]])).round(2).tolist()
[2.0, 10.0, 4.67]
set_score_request(*, sample_weight='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters#

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns#

selfobject

The updated object.

Return type:

GroupMean