.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/neighbors/plot_partial_label_ranking_neighbors.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_neighbors_plot_partial_label_ranking_neighbors.py: ================================================== Partial label ranking with the k-nearest neighbors ================================================== This example compares the weights of the neighbors of :class:`~sklr.neighbors.KNeighborsPartialLabelRanker` on synthetic bucket orders. It first scores them for several numbers of neighbors, and then removes labels from the training rankings to see how the consensus of the neighbors copes with incomplete partial rankings. .. GENERATED FROM PYTHON SOURCE LINES 14-22 Synthetic bucket orders ----------------------- Each sample has two features and a utility for each of five labels, which is a nonlinear function of the features plus some noise. The labels whose utilities round to the same value are tied, so the ranking of a sample is a bucket order that sorts the buckets by decreasing utility. It holds the dense position of each label, as described in :ref:`ranking_representation`. .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: Python import numpy as np from scipy.stats import rankdata rng = np.random.RandomState(0) n_samples, n_features, n_labels = 300, 2, 5 X = rng.uniform(-1, 1, size=(n_samples, n_features)) coef = rng.normal(size=(n_features, n_labels)) utilities = np.sin(3 * X) @ coef + 0.3 * rng.normal(size=(n_samples, n_labels)) y = rankdata(-np.round(utilities), method="dense", axis=1) print(y[:3]) .. rst-class:: sphx-glr-script-out .. code-block:: none [[1. 3. 3. 4. 2.] [1. 2. 3. 2. 2.] [1. 3. 4. 5. 2.]] .. GENERATED FROM PYTHON SOURCE LINES 35-43 Number of neighbors and weights ------------------------------- Each weight function is scored by 5-fold cross-validation with the :math:`\tau_x` coefficient, whose scorer is given by :func:`~sklr.metrics.get_scorer`. Both features have the same range, so they are not scaled. As a baseline, :class:`~sklr.dummy.DummyPartialLabelRanker` predicts the consensus of the training rankings for every sample, without looking at the features. .. GENERATED FROM PYTHON SOURCE LINES 43-71 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.model_selection import KFold, cross_val_score from sklr.dummy import DummyPartialLabelRanker from sklr.metrics import get_scorer from sklr.neighbors import KNeighborsPartialLabelRanker cv = KFold(n_splits=5, shuffle=True, random_state=0) scoring = get_scorer("tau_x") n_neighbors_grid = [1, 5, 10, 20, 40] weights_grid = ["uniform", "distance", "dudani"] fig, ax = plt.subplots(figsize=(5, 4)) for weights in weights_grid: scores = [] for n_neighbors in n_neighbors_grid: ranker = KNeighborsPartialLabelRanker(n_neighbors, weights=weights) scores.append(cross_val_score(ranker, X, y, scoring=scoring, cv=cv).mean()) ax.plot(n_neighbors_grid, scores, marker="o", label=f"weights={weights!r}") baseline = cross_val_score( DummyPartialLabelRanker(), X, y, scoring=scoring, cv=cv ).mean() ax.axhline(baseline, color="gray", linestyle="--", label="DummyPartialLabelRanker") ax.set(xlabel="Number of neighbors", ylabel=r"$\tau_x$ (5-fold CV)") ax.legend() fig.tight_layout() .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_partial_label_ranking_neighbors_001.png :alt: plot partial label ranking neighbors :srcset: /auto_examples/neighbors/images/sphx_glr_plot_partial_label_ranking_neighbors_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-89 All the rankers score well above the baseline, so the features carry information about the bucket orders. With a single neighbor, the ranker predicts its bucket order, whatever the weights. More neighbors average out the noise of the rankings, until they come from too far away. Weighting the neighbors by the inverse of their distance or by the rule of Dudani lets the ranker use more neighbors before the far ones degrade the prediction. In practice, the number of neighbors and the weights are chosen by cross-validation, as described in the :ref:`user guide `. Incomplete training rankings ---------------------------- Labels are now removed at random from the training rankings, with a probability of 0, 0.3 and 0.6 for each label, and the positions of the remaining ones are made dense again. The test rankings stay complete, because :math:`\tau_x` only compares complete rankings. The neighbors are weighted by the inverse of their distance, and several numbers of neighbors are compared. .. GENERATED FROM PYTHON SOURCE LINES 89-125 .. code-block:: Python def remove_labels(y, probability, random_state): """Remove each label of each ranking with the given probability.""" y = y.copy() y[random_state.rand(*y.shape) < probability] = np.nan return rankdata(y, method="dense", axis=1, nan_policy="omit") probabilities = [0, 0.3, 0.6] n_neighbors_grid = [5, 10, 20] scores = {n_neighbors: [] for n_neighbors in n_neighbors_grid} for probability in probabilities: for n_neighbors in n_neighbors_grid: fold_scores = [] for train, test in cv.split(X): y_train = remove_labels(y[train], probability, rng) ranker = KNeighborsPartialLabelRanker(n_neighbors, weights="distance") ranker.fit(X[train], y_train) fold_scores.append(ranker.score(X[test], y[test])) scores[n_neighbors].append(np.mean(fold_scores)) fig, ax = plt.subplots(figsize=(5, 4)) for n_neighbors in n_neighbors_grid: ax.plot( probabilities, scores[n_neighbors], marker="o", label=f"{n_neighbors} neighbors" ) ax.set( xlabel="Probability of removing each training label", ylabel=r"$\tau_x$ (5-fold CV)", xticks=probabilities, ) ax.legend() fig.tight_layout() plt.show() .. image-sg:: /auto_examples/neighbors/images/sphx_glr_plot_partial_label_ranking_neighbors_002.png :alt: plot partial label ranking neighbors :srcset: /auto_examples/neighbors/images/sphx_glr_plot_partial_label_ranking_neighbors_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-130 The consensus only uses, for each pair of labels, the neighbors that rank both, so removing labels leaves fewer rankings to decide each pair. The score drops as more labels are removed, and more neighbors make up for part of the loss, since each pair is then still ranked by enough of them. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.452 seconds) .. _sphx_glr_download_auto_examples_neighbors_plot_partial_label_ranking_neighbors.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_partial_label_ranking_neighbors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_partial_label_ranking_neighbors.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_partial_label_ranking_neighbors.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_