archetypes.FairAA#

class archetypes.FairAA(n_archetypes, *, fairness_const=200, max_iter=300, tol=0.0001, init='uniform', n_init=1, init_params=None, save_init=False, method='nnls', method_params=None, verbose=False, random_state=None)#

Fair Archetype Analysis.

This class implements the Fair Archetype Analysis algorithm, which is a variant of the archetype analysis that incorporates fairness constraints.

Parameters:
n_archetypes: int

The number of archetypes to compute.

fairness_const: float, default=200.0
max_iterint, default=300

Maximum number of iterations of the archetype analysis algorithm for a single run.

tolfloat, default=1e-4

Relative tolerance of two consecutive iterations to declare convergence.

initstr, default=’uniform’

Method used to initialize the archetypes, must be one of the following: ‘uniform’, ‘furthest_sum’, ‘furthest_first’ or ‘aa_plus_plus’. See Initialization Methods.

n_initint, default=1

Number of time the archetype analysis algorithm will be run with different initializations. The final results will be the best output of n_init consecutive runs.

init_paramsdict, default=None

Additional keyword arguments to pass to the initialization method.

save_initbool, default=False

If True, save the initial archetypes in the attribute archetypes_init_,

method: str, default=’pgd’

The optimization method to use for the archetypes and the coefficients, must be one of the following: ‘pgd’, ‘pseudo_pgd’. See Optimization Methods.

method_paramsdict, default=None

Additional arguments to pass to the optimization method. See Optimization Methods.

verbosebool, default=False

Verbosity mode.

random_stateint, RandomState instance or None, default=None

Determines random number generation of coefficients in initialization. Use an int to make the randomness reproducible.

Attributes:
archetypes_: np.ndarray

The computed archetypes. It has shape (n_archetypes, n_features).

n_archetypes_: int

The number of archetypes after fitting.

archetypes_init_np.ndarray

The initial archetypes. It is only available if save_init=True.

coefficients_, A_np.ndarray

The similarity degree of each sample to each archetype. It has shape (n_samples, n_archetypes).

arch_coefficients_, B_np.ndarray

The similarity degree of each archetype to each sample. It has shape (n_archetypes, n_samples).

labels_np.ndarray

The label of each sample. It is the index of the closest archetype. It has shape (n_samples,).

loss_list

The loss at each iteration.

rss_, reconstruction_error_float

The residual sum of squares of the fitted data.

Methods

fit(X[, y, Z])

Compute Archetype Analysis.

fit_transform(X[, y, Z])

Compute the archetypes and transform X to the archetypal space.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

set_fit_request(*[, Z])

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

set_output(*[, transform])

Set output container.

set_params(**params)

Set the parameters of this estimator.

set_transform_request(*[, Z])

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

transform(X, Z)

Transform X to the archetypal space.

fit(X, y=None, Z=None)#

Compute Archetype Analysis.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training instances to compute the archetypes. It must be noted that the data will be converted to C ordering, which will cause a memory copy if the given data is not C-contiguous.

yIgnored

Not used, present here for API consistency by convention.

Zarray-like of shape (n_samples, n_sensitive_features)

Sensitive features to compute the fairness constraint.

Returns:
selfobject

Fitted estimator.

fit_transform(X, y=None, Z=None, **params)#

Compute the archetypes and transform X to the archetypal space.

Equivalent to fit(X).transform(X).

Parameters:
Xarray-like of shape (n_samples, n_features)

New data to transform.

yIgnored

Not used, present here for API consistency by convention.

Zarray-like of shape (n_samples, n_sensitive_features)

Sensitive features to compute the fairness constraint.

Returns
——-
Andarray of shape (n_samples, n_archetypes)

X transformed in the new space.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating 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.

set_fit_request(*, Z: bool | None | str = '$UNCHANGED$') FairAA#

Configure whether metadata should be requested to be passed to the fit 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 fit 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 fit.

  • 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:
Zstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for Z parameter in fit.

Returns:
selfobject

The updated object.

set_output(*, transform=None)#

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters:
transform{“default”, “pandas”, “polars”}, default=None

Configure output of transform and fit_transform.

  • “default”: Default output format of a transformer

  • “pandas”: DataFrame output

  • “polars”: Polars output

  • None: Transform configuration is unchanged

Added in version 1.4: “polars” option was added.

Returns:
selfestimator instance

Estimator instance.

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.

set_transform_request(*, Z: bool | None | str = '$UNCHANGED$') FairAA#

Configure whether metadata should be requested to be passed to the transform 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 transform 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 transform.

  • 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:
Zstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for Z parameter in transform.

Returns:
selfobject

The updated object.

transform(X, Z)#

Transform X to the archetypal space.

In the new space, each dimension is the distance to the archetypes.

Parameters:
Xarray-like of shape (n_samples, n_features)

New data to transform.

Zarray-like of shape (n_samples, n_sensitive_features)

Sensitive features to compute the fairness constraint.

Returns:
Andarray of shape (n_samples, n_archetypes)

X transformed in the new space.