neural_trees.WeightedKNN#

class neural_trees.WeightedKNN(k: int = 5, weight_power: float = 2.0, metric: str = 'euclidean', condense: bool = False, n_condensed_sets: int = 1, random_state: int | None = None)[source]#

Bases: ClassifierMixin, BaseEstimator

Distance-Weighted K-Nearest Neighbors Classifier.

Parameters:
kint, default=5

Number of neighbors.

weight_powerfloat, default=2.0

Power for inverse-distance weighting. Set to 0 for uniform weights.

metricstr, default=”euclidean”

Distance metric: “euclidean” or “manhattan”.

condensebool, default=False

If True, apply condensing: keep only a subset of training samples that correctly classifies all the others (Hart’s CNN).

n_condensed_setsint, default=1

How many condensed subsets to build and vote over when condense=True.

Condensing is order dependent: which samples end up as prototypes depends on the order they were visited in, and a single pass throws away information that a different order would have kept. Alpaydin (1997) builds several subsets from different orderings and combines their votes, which is where some of the accuracy a single subset gives away comes back. 5-fold accuracy averaged over 5 seeds, by number of subsets, against keeping every sample:

                1       3       5       9     all
Iris          0.917   0.939   0.937   0.937   0.956
Wine          0.947   0.955   0.964   0.971   0.966
Breast Canc.  0.951   0.963   0.966   0.968   0.966

Voting beats a single subset everywhere. It reaches the uncondensed classifier on Wine and Breast Cancer while storing roughly a sixth of the data, and closes about half the gap on Iris without closing it.

Ignored when condense=False.

random_stateint or None, default=None

Seed for the orderings used to build the condensed subsets.

References

Alpaydın, E. (1997). Voting over Multiple Condensed Nearest Neighbors. Artificial Intelligence Review, 11, 115-132.

predict_proba(X) ndarray[source]#

Class probabilities, averaged over the condensed subsets.

Each subset votes with its own distance-weighted neighbours, and the votes are averaged. With n_condensed_sets=1 this is a plain weighted KNN over a single store.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') WeightedKNN#

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.