.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/sphinx_gallery_examples/pool/plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).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_generated_sphinx_gallery_examples_pool_plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).py: Clustering Uncertainty-weighted Embeddings (CLUE) ================================================= .. GENERATED FROM PYTHON SOURCE LINES 10-23 .. note:: The generated animation can be found at the bottom of the page. | **Google Colab Note**: If the notebook fails to run after installing the needed packages, try to restart the runtime (Ctrl + M) under Runtime -> Restart session. .. image:: https://colab.research.google.com/assets/colab-badge.svg :target: https://colab.research.google.com/github/scikit-activeml/scikit-activeml-docs/blob/gh-pages/latest/generated/sphinx_gallery_notebooks//pool/plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).ipynb | **Notebook Dependencies** | Uncomment the following cell to install all dependencies for this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python # !pip install scikit-activeml .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 33-97 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt, animation from sklearn.datasets import make_blobs from skactiveml.utils import MISSING_LABEL, labeled_indices, unlabeled_indices from skactiveml.visualization import plot_utilities, plot_decision_boundary, \ plot_contour_for_samples from skactiveml.classifier import ParzenWindowClassifier from skactiveml.pool import Clue random_state = np.random.RandomState(0) # Build a dataset. X, y_true = make_blobs(n_samples=200, n_features=2, centers=[[0, 1], [-3, .5], [-1, -1], [2, 1], [1, -.5]], cluster_std=.7, random_state=random_state) y_true = y_true % 2 y = np.full(shape=y_true.shape, fill_value=MISSING_LABEL) # Initialise the classifier. clf = ParzenWindowClassifier(classes=[0, 1], random_state=random_state) # Initialise the query strategy. qs = Clue() # Preparation for plotting. fig, axs = plt.subplots(2, 2, constrained_layout=True) feature_bound = [[min(X[:, 0]), min(X[:, 1])], [max(X[:, 0]), max(X[:, 1])]] artists = [[] for j in range(5)] # The active learning cycle: n_cycles = 5 for c in range(n_cycles): # Fit the classifier. clf.fit(X, y) # Get labeled instances. X_labeled = X[labeled_indices(y)] # Query the next instance/s. query_idx, utilities = qs.query(X=X, y=y, clf=clf, batch_size=4, return_utilities=True) # Plot the labeled data. for i, ax in enumerate(axs.flatten()): coll_old = list(ax.collections) plot_contour_for_samples(X, utilities[i], res=25, feature_bound=feature_bound, replace_nan=None, ax=ax) ax.scatter(X[:, 0], X[:, 1], c=y_true, cmap="coolwarm", marker=".", zorder=2) ax.scatter(X_labeled[:, 0], X_labeled[:, 1], c="grey", alpha=.8, marker=".", s=300) ax = plot_decision_boundary(clf, feature_bound, ax=ax) ax.set_title(f"Batch {c+1}, Utilities[{i}]") for x in ax.collections: if x not in coll_old: artists[c].append(x) # Label the queried instances. y[query_idx] = y_true[query_idx] ani = animation.ArtistAnimation(fig, artists, interval=1000, blit=True) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 98-99 .. image:: ../../examples/pool_classification_legend.png .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. rubric:: References: The implementation of this strategy is based on :footcite:t:`prabhu2021active`. .. footbibliography:: .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.712 seconds) .. _sphx_glr_download_generated_sphinx_gallery_examples_pool_plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot-Clue-Clustering_Uncertainty-weighted_Embeddings_(CLUE).zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_