PlottingMixin#
- class scikitplot.annoy.PlottingMixin[source]#
Mixin that adds convenient plotting methods to high-level Annoy wrappers.
The mixin assumes the host class is Annoy-like (implements
get_n_items(),get_item_vector(i), andget_nns_by_item(...)). If your wrapper delegates to an internal Annoy instance, override_plotting_backendto return that backend.See also
plot_annoy_indexFunction-level plotting helper.
plot_annoy_knn_edgesFunction-level kNN edge overlay helper.
Notes
The underlying plotting logic is implemented in
scikitplot.cexternals._annoy._plotting.All methods are deterministic given index contents and parameters.
- plot_index(labels=None, *, ids=None, projection='pca', dims=(0, 1), center=True, maxabs=False, l2_normalize=False, dtype=<class 'numpy.float32'>, ax=None, title=None, plot_kwargs=None)[source]#
Plot this index as a 2D scatter plot.
This is a thin wrapper around
plot_annoy_indexthat uses_plotting_backend.- Parameters:
- labels, ids, projection, dims, center, maxabs, l2_normalize, dtype, ax, title, plot_kwargs
See
plot_annoy_index.
- Returns:
- y2, ids_out, ax
See
plot_annoy_index.
- Parameters:
- Return type:
See also
plot_annoy_indexLow-level plotting helper this method delegates to.
plot_knn_edgesOverlay kNN edges on the returned 2D coordinates.
Notes
This method does not mutate the index.
Plotting backends (e.g. Matplotlib) are imported lazily and are only required when this method is called.
The returned
ids_outcorresponds to the item id for each row iny2.
Examples
>>> import numpy as np >>> import scikitplot.annoy as skann >>> idx = skann.Index(f=10, metric="angular") >>> # ... add items & build ... >>> labels = np.zeros(idx.get_n_items(), dtype=int) >>> y2, ids, ax = idx.plot_index(labels=labels, projection="pca")
- plot_knn_edges(y2, *, ids=None, k=10, search_k=-1, ax=None, line_kwargs=None, undirected=True)[source]#
Overlay kNN edges onto an existing 2D index plot.
This is a thin wrapper around
plot_annoy_knn_edgesthat uses_plotting_backend.- Parameters:
- y2, ids, k, search_k, ax, line_kwargs, undirected
See
plot_annoy_knn_edges.
- Returns:
- ax
The axes that were drawn on.
- Parameters:
- Return type:
See also
plot_annoy_knn_edgesLow-level edge overlay helper this method delegates to.
plot_indexComputes the 2D coordinates used as input to this method.
Notes
y2must represent 2D coordinates with shape(n_samples, 2).If
idsis provided, it must have lengthn_samples.This method does not mutate the index; it only performs neighbor queries to draw edges.
Examples
>>> y2, ids, ax = idx.plot_index(labels=np.zeros(idx.get_n_items(), dtype=int)) >>> idx.plot_knn_edges(y2, ids=ids, k=5, line_kwargs={"alpha": 0.15})