NearestCentroid#

class skfda.ml.classification.NearestCentroid(metric=LpDistance(p=2, vector_norm=None), centroid=<function mean>)[source]#

Nearest centroid classifier for functional data.

Each class is represented by its centroid, with test samples classified to the class with the nearest centroid.

Parameters:
  • metric (Metric[Input]) – The metric to use when calculating distance between test samples and centroids. See the documentation of the metrics module for a list of available metrics. L2 distance is used by default.

  • centroid (Callable[[Input], Input]) – The centroids for the samples corresponding to each class is the point from which the sum of the distances (according to the metric) of all samples that belong to that particular class are minimized. By default it is used the usual mean, which minimizes the sum of L2 distances. This parameter allows change the centroid constructor. The function must accept a FData with the samples of one class and return a FData object with only one sample representing the centroid.

Examples

Firstly, we will create a toy dataset with 2 classes

>>> from skfda.datasets import make_sinusoidal_process
>>> fd1 = make_sinusoidal_process(phase_std=.25, random_state=0)
>>> fd2 = make_sinusoidal_process(phase_mean=1.8, error_std=0.,
...                               phase_std=.25, random_state=0)
>>> fd = fd1.concatenate(fd2)
>>> y = 15*[0] + 15*[1]

We will fit a Nearest centroids classifier

>>> from skfda.ml.classification import NearestCentroid
>>> neigh = NearestCentroid()
>>> neigh.fit(fd, y)
NearestCentroid(...)

We can predict the class of new samples

>>> neigh.predict(fd[::2]) # Predict labels for even samples
array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1])

See also

DTMClassifier

Methods

fit(X, y)

Fit the model using X as training data and y as target values.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X)

Predict the class labels for the provided data.

score(X, y[, sample_weight])

Return the mean accuracy on the given test data and labels.

set_params(**params)

Set the parameters of this estimator.

set_score_request(*[, sample_weight])

Request metadata passed to the score method.

fit(X, y)[source]#

Fit the model using X as training data and y as target values.

Parameters:
  • X (Input) – FDataGrid with the training data or array matrix with shape (n_samples, n_samples) if metric=’precomputed’.

  • y (Target) – Target values of shape = (n_samples) or (n_samples, n_outputs).

Returns:

self

Return type:

NearestCentroid[Input, Target]

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

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 – Parameter names mapped to their values.

Return type:

dict

predict(X)[source]#

Predict the class labels for the provided data.

Parameters:

X (Input) – FDataGrid with the test samples.

Returns:

Array of shape (n_samples) or

(n_samples, n_outputs) with class labels for each data sample.

Return type:

Target

score(X, y, sample_weight=None)[source]#

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.

  • sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.

Returns:

score – Mean accuracy of self.predict(X) w.r.t. y.

Return type:

float

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.

Return type:

estimator instance

set_score_request(*, sample_weight='$UNCHANGED$')#

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

New in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters:
  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

  • self (NearestCentroid) –

Returns:

self – The updated object.

Return type:

object

Examples using skfda.ml.classification.NearestCentroid#

Classification methods

Classification methods

Scikit-fda and scikit-learn

Scikit-fda and scikit-learn