quantificationlib.estimators.weighted_knn module

Proportion-weighted K-Nearest Neighbor Classifier

class PWK(n_neighbors=10, algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None)[source]

Bases: BaseEstimator, ClassifierMixin

Proportion-weighted k-Nearest Neighbor Classifier

This class is an kind of wrapper of sklearn.neighbors.KNeighborsClassifier (version 1.0.2) to use class-dependent weights to deal with imbalanced problems. The parameters are the same, except weights that are computed by this class

Parameters:
  • n_neighbors (int, (default=10)) – Number of neighbors to use by default for kneighbors() queries.

  • algorithm ({'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto') –

    Algorithm used to compute the nearest neighbors:

    • ’ball_tree’ will use BallTree

    • ’kd_tree’ will use KDTree

    • ’brute’ will use a brute-force search

    • ’auto’ will attempt to decide the most appropriate algorithm based on the values passed to fit() method.

    Note: fitting on sparse input will override the setting of this parameter, using brute force.

  • leaf_size (int, default=30) – Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

  • p (int, default=2) – Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

  • metric (str or callable, default='minkowski') – The distance metric to use for the tree. The default metric is minkowski, and with p=2 is equivalent to the standard Euclidean metric. For a list of available metrics, see the documentation of DistanceMetric. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.

  • metric_params (dict, default=None) – Additional keyword arguments for the metric function.

  • n_jobs (int, default=None) – The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. Doesn’t affect fit() method.

knn_

KNN classifier

Type:

KNeighborsClassifier object

classes_

Class labels known to the classifier

Type:

array of shape (n_classes,)

weights_

The weight for each example

Type:

array, shape (n_samples, )

y_

True labels

Type:

array

fit(X, y)[source]

Fit the k-nearest neighbors classifier and compute the weights using the training dataset

Parameters:
  • X (array-like, shape (n_examples, n_features)) – Data

  • y (array-like, shape (n_examples, )) – True classes

predict(X)[source]

Returns the crisp predictions for the provided data

Parameters:

X (array-like, shape (n_examples, n_features)) – Test ata

Returns:

preds – Crisp predictions for the examples in X

Return type:

array-like, shape shape(n_examples, )

predict_proba(X)[source]

Returns the probabilistic predictions for the provided data

Parameters:

X (array-like, shape (n_examples, n_features)) – Test ata

Returns:

preds – Probabilistic predictions for the examples in X

Return type:

array-like, shape shape(n_examples, n_classes)

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 (PWK) –

Returns:

self – The updated object.

Return type:

object