Digits DatasetΒΆ

This digits example shows two ways of customizing the tooltips options in the HTML visualization. It generates the visualization with tooltips set as the y-label, or number of the image. The second generated result uses the actual image in the tooltips.

Visualization with y-label tooltip

Visualization with custom tooltips

plot digits

Out:

KeplerMapper(verbose=2)
..Composing projection pipeline of length 1:
        Projections: TSNE()
        Distance matrices: False
        Scalers: MinMaxScaler()
..Projecting on data shaped (1797, 64)

..Projecting data using:
        TSNE(verbose=2)

/home/docs/checkouts/readthedocs.org/user_builds/keplermapper/envs/latest/lib/python3.7/site-packages/sklearn/manifold/_t_sne.py:783: FutureWarning: The default initialization in TSNE will change from 'random' to 'pca' in 1.2.
  FutureWarning,
/home/docs/checkouts/readthedocs.org/user_builds/keplermapper/envs/latest/lib/python3.7/site-packages/sklearn/manifold/_t_sne.py:793: FutureWarning: The default learning rate in TSNE will change from 200.0 to 'auto' in 1.2.
  FutureWarning,
[t-SNE] Computing 91 nearest neighbors...
[t-SNE] Indexed 1797 samples in 0.000s...
[t-SNE] Computed neighbors for 1797 samples in 0.112s...
[t-SNE] Computed conditional probabilities for sample 1000 / 1797
[t-SNE] Computed conditional probabilities for sample 1797 / 1797
[t-SNE] Mean sigma: 186.389054
[t-SNE] Computed conditional probabilities in 0.105s
[t-SNE] Iteration 50: error = 74.2439117, gradient norm = 0.1286457 (50 iterations in 0.726s)
[t-SNE] Iteration 100: error = 63.5076141, gradient norm = 0.0374222 (50 iterations in 0.443s)
[t-SNE] Iteration 150: error = 62.1858444, gradient norm = 0.0310904 (50 iterations in 0.425s)
[t-SNE] Iteration 200: error = 61.7427330, gradient norm = 0.0209472 (50 iterations in 0.434s)
[t-SNE] Iteration 250: error = 61.5269318, gradient norm = 0.0168010 (50 iterations in 0.433s)
[t-SNE] KL divergence after 250 iterations with early exaggeration: 61.526932
[t-SNE] Iteration 300: error = 1.0279883, gradient norm = 0.0007920 (50 iterations in 0.396s)
[t-SNE] Iteration 350: error = 0.8614621, gradient norm = 0.0003295 (50 iterations in 0.389s)
[t-SNE] Iteration 400: error = 0.8087953, gradient norm = 0.0002140 (50 iterations in 0.399s)
[t-SNE] Iteration 450: error = 0.7865094, gradient norm = 0.0001752 (50 iterations in 0.396s)
[t-SNE] Iteration 500: error = 0.7755185, gradient norm = 0.0001357 (50 iterations in 0.395s)
[t-SNE] Iteration 550: error = 0.7679199, gradient norm = 0.0001196 (50 iterations in 0.397s)
[t-SNE] Iteration 600: error = 0.7622845, gradient norm = 0.0001029 (50 iterations in 0.399s)
[t-SNE] Iteration 650: error = 0.7580604, gradient norm = 0.0000949 (50 iterations in 0.398s)
[t-SNE] Iteration 700: error = 0.7546495, gradient norm = 0.0000914 (50 iterations in 0.397s)
[t-SNE] Iteration 750: error = 0.7519471, gradient norm = 0.0000930 (50 iterations in 0.401s)
[t-SNE] Iteration 800: error = 0.7500713, gradient norm = 0.0000883 (50 iterations in 0.400s)
[t-SNE] Iteration 850: error = 0.7484020, gradient norm = 0.0000945 (50 iterations in 0.401s)
[t-SNE] Iteration 900: error = 0.7471702, gradient norm = 0.0000748 (50 iterations in 0.402s)
[t-SNE] Iteration 950: error = 0.7453438, gradient norm = 0.0000783 (50 iterations in 0.404s)
[t-SNE] Iteration 1000: error = 0.7437218, gradient norm = 0.0000711 (50 iterations in 0.402s)
[t-SNE] KL divergence after 1000 iterations: 0.743722

..Scaling with: MinMaxScaler()

Mapping on data shaped (1797, 2) using lens shaped (1797, 2)

Minimal points in hypercube before clustering: 15
Creating 1225 hypercubes.
Cube_0 is empty.

   > Found 1 clusters in hypercube 1.
   > Found 1 clusters in hypercube 2.
Cube_3 is empty.

Cube_4 is empty.

Cube_5 is empty.

   > Found 1 clusters in hypercube 6.
   > Found 1 clusters in hypercube 7.
   > Found 1 clusters in hypercube 8.
   > Found 1 clusters in hypercube 9.
Cube_10 is empty.

Cube_11 is empty.

   > Found 1 clusters in hypercube 12.
   > Found 1 clusters in hypercube 13.
   > Found 1 clusters in hypercube 14.
   > Found 1 clusters in hypercube 15.
   > Found 1 clusters in hypercube 16.
Cube_17 is empty.

Cube_18 is empty.

   > Found 1 clusters in hypercube 19.
   > Found 1 clusters in hypercube 20.
   > Found 1 clusters in hypercube 21.
   > Found 1 clusters in hypercube 22.
   > Found 1 clusters in hypercube 23.
Cube_24 is empty.

Cube_25 is empty.

Cube_26 is empty.

Cube_27 is empty.

Cube_28 is empty.

Cube_29 is empty.

Cube_30 is empty.

Cube_31 is empty.

Cube_32 is empty.

Cube_33 is empty.

Cube_34 is empty.

   > Found 1 clusters in hypercube 35.
Cube_36 is empty.

Cube_37 is empty.

Cube_38 is empty.

Cube_39 is empty.

Cube_40 is empty.

Cube_41 is empty.

   > Found 1 clusters in hypercube 42.
Cube_43 is empty.

Cube_44 is empty.

Cube_45 is empty.

Cube_46 is empty.

   > Found 1 clusters in hypercube 47.
Cube_48 is empty.

Cube_49 is empty.

Cube_50 is empty.

   > Found 1 clusters in hypercube 51.
   > Found 1 clusters in hypercube 52.
   > Found 1 clusters in hypercube 53.
   > Found 1 clusters in hypercube 54.
   > Found 1 clusters in hypercube 55.
Cube_56 is empty.

Cube_57 is empty.

   > Found 1 clusters in hypercube 58.
   > Found 1 clusters in hypercube 59.
   > Found 1 clusters in hypercube 60.
   > Found 1 clusters in hypercube 61.
Cube_62 is empty.

   > Found 1 clusters in hypercube 63.
   > Found 1 clusters in hypercube 64.
   > Found 1 clusters in hypercube 65.
   > Found 1 clusters in hypercube 66.
   > Found 1 clusters in hypercube 67.
Cube_68 is empty.

Cube_69 is empty.

   > Found 1 clusters in hypercube 70.
   > Found 1 clusters in hypercube 71.
   > Found 1 clusters in hypercube 72.
   > Found 1 clusters in hypercube 73.
   > Found 1 clusters in hypercube 74.
Cube_75 is empty.

   > Found 1 clusters in hypercube 76.
   > Found 1 clusters in hypercube 77.
   > Found 1 clusters in hypercube 78.
   > Found 1 clusters in hypercube 79.
Cube_80 is empty.

Cube_81 is empty.

Cube_82 is empty.

   > Found 1 clusters in hypercube 83.
   > Found 1 clusters in hypercube 84.
Cube_85 is empty.

Cube_86 is empty.

Cube_87 is empty.

Cube_88 is empty.

Cube_89 is empty.

   > Found 1 clusters in hypercube 90.
   > Found 1 clusters in hypercube 91.
   > Found 1 clusters in hypercube 92.
   > Found 1 clusters in hypercube 93.
Cube_94 is empty.

Cube_95 is empty.

   > Found 1 clusters in hypercube 96.
   > Found 1 clusters in hypercube 97.
   > Found 1 clusters in hypercube 98.
   > Found 1 clusters in hypercube 99.
Cube_100 is empty.

Cube_101 is empty.

Cube_102 is empty.

Cube_103 is empty.

Cube_104 is empty.

   > Found 1 clusters in hypercube 105.
   > Found 1 clusters in hypercube 106.
Cube_107 is empty.

Cube_108 is empty.

   > Found 1 clusters in hypercube 109.
   > Found 1 clusters in hypercube 110.
   > Found 1 clusters in hypercube 111.
Cube_112 is empty.

Cube_113 is empty.

Cube_114 is empty.

Cube_115 is empty.

Cube_116 is empty.

Cube_117 is empty.

Cube_118 is empty.

   > Found 1 clusters in hypercube 119.
   > Found 1 clusters in hypercube 120.
Cube_121 is empty.

Cube_122 is empty.

Cube_123 is empty.

Cube_124 is empty.

   > Found 1 clusters in hypercube 125.
   > Found 1 clusters in hypercube 126.
   > Found 1 clusters in hypercube 127.
Cube_128 is empty.

Cube_129 is empty.

Cube_130 is empty.

Cube_131 is empty.

Cube_132 is empty.

Cube_133 is empty.

Cube_134 is empty.

Cube_135 is empty.

Cube_136 is empty.

Cube_137 is empty.

Cube_138 is empty.

Cube_139 is empty.

Cube_140 is empty.

Cube_141 is empty.

Cube_142 is empty.

Cube_143 is empty.

Cube_144 is empty.

Cube_145 is empty.

Cube_146 is empty.

Cube_147 is empty.

   > Found 1 clusters in hypercube 148.
   > Found 1 clusters in hypercube 149.
   > Found 1 clusters in hypercube 150.
Cube_151 is empty.

Cube_152 is empty.

Cube_153 is empty.

Cube_154 is empty.

   > Found 1 clusters in hypercube 155.
   > Found 1 clusters in hypercube 156.
   > Found 1 clusters in hypercube 157.
   > Found 1 clusters in hypercube 158.
Cube_159 is empty.

Cube_160 is empty.

Cube_161 is empty.

Cube_162 is empty.

Cube_163 is empty.

   > Found 1 clusters in hypercube 164.
Cube_165 is empty.

Cube_166 is empty.

   > Found 1 clusters in hypercube 167.
   > Found 1 clusters in hypercube 168.
   > Found 1 clusters in hypercube 169.
Cube_170 is empty.

Cube_171 is empty.

Cube_172 is empty.

Cube_173 is empty.

Cube_174 is empty.

   > Found 1 clusters in hypercube 175.
   > Found 1 clusters in hypercube 176.
Cube_177 is empty.

Cube_178 is empty.

Cube_179 is empty.

Cube_180 is empty.

Cube_181 is empty.

Cube_182 is empty.

Cube_183 is empty.

   > Found 1 clusters in hypercube 184.
   > Found 1 clusters in hypercube 185.
Cube_186 is empty.

Cube_187 is empty.

   > Found 1 clusters in hypercube 188.
   > Found 1 clusters in hypercube 189.
Cube_190 is empty.

   > Found 1 clusters in hypercube 191.
   > Found 1 clusters in hypercube 192.
Cube_193 is empty.

Cube_194 is empty.

Cube_195 is empty.

Cube_196 is empty.

Cube_197 is empty.

Cube_198 is empty.

Cube_199 is empty.

   > Found 1 clusters in hypercube 200.
   > Found 1 clusters in hypercube 201.
Cube_202 is empty.

Cube_203 is empty.

Cube_204 is empty.

Cube_205 is empty.

Cube_206 is empty.

Cube_207 is empty.

   > Found 1 clusters in hypercube 208.
   > Found 1 clusters in hypercube 209.
   > Found 1 clusters in hypercube 210.
Cube_211 is empty.

Cube_212 is empty.

Cube_213 is empty.

Cube_214 is empty.

Cube_215 is empty.

Cube_216 is empty.

   > Found 1 clusters in hypercube 217.
   > Found 1 clusters in hypercube 218.
Cube_219 is empty.

Cube_220 is empty.

   > Found 1 clusters in hypercube 221.
Cube_222 is empty.

Cube_223 is empty.

Cube_224 is empty.

Cube_225 is empty.

Cube_226 is empty.

Cube_227 is empty.

Cube_228 is empty.

Cube_229 is empty.

Cube_230 is empty.

Cube_231 is empty.

   > Found 1 clusters in hypercube 232.
   > Found 1 clusters in hypercube 233.
   > Found 1 clusters in hypercube 234.
Cube_235 is empty.

   > Found 1 clusters in hypercube 236.
   > Found 1 clusters in hypercube 237.
Cube_238 is empty.

Cube_239 is empty.

Cube_240 is empty.

Cube_241 is empty.

   > Found 1 clusters in hypercube 242.
   > Found 1 clusters in hypercube 243.
Cube_244 is empty.

Cube_245 is empty.

   > Found 1 clusters in hypercube 246.
   > Found 1 clusters in hypercube 247.
Cube_248 is empty.

   > Found 1 clusters in hypercube 249.
   > Found 1 clusters in hypercube 250.
   > Found 1 clusters in hypercube 251.
Cube_252 is empty.

Cube_253 is empty.

Cube_254 is empty.

Cube_255 is empty.

   > Found 1 clusters in hypercube 256.
Cube_257 is empty.

Cube_258 is empty.

Cube_259 is empty.

   > Found 1 clusters in hypercube 260.
   > Found 1 clusters in hypercube 261.
   > Found 1 clusters in hypercube 262.
Cube_263 is empty.

Cube_264 is empty.

Cube_265 is empty.

   > Found 1 clusters in hypercube 266.
   > Found 1 clusters in hypercube 267.
Cube_268 is empty.

Cube_269 is empty.

Cube_270 is empty.

Cube_271 is empty.

   > Found 1 clusters in hypercube 272.
   > Found 1 clusters in hypercube 273.
   > Found 1 clusters in hypercube 274.
   > Found 1 clusters in hypercube 275.
Cube_276 is empty.

Cube_277 is empty.

Cube_278 is empty.

Cube_279 is empty.

Cube_280 is empty.

Cube_281 is empty.

Cube_282 is empty.

   > Found 1 clusters in hypercube 283.
Cube_284 is empty.

Cube_285 is empty.

Cube_286 is empty.

Cube_287 is empty.

Cube_288 is empty.

Cube_289 is empty.

Cube_290 is empty.

Cube_291 is empty.

Cube_292 is empty.

   > Found 1 clusters in hypercube 293.
   > Found 1 clusters in hypercube 294.
   > Found 1 clusters in hypercube 295.
   > Found 1 clusters in hypercube 296.
Cube_297 is empty.

Cube_298 is empty.

Cube_299 is empty.

Cube_300 is empty.

Cube_301 is empty.

Cube_302 is empty.

Cube_303 is empty.

   > Found 1 clusters in hypercube 304.
Cube_305 is empty.

Cube_306 is empty.

   > Found 1 clusters in hypercube 307.
Cube_308 is empty.

   > Found 1 clusters in hypercube 309.
Cube_310 is empty.

Cube_311 is empty.

   > Found 1 clusters in hypercube 312.
Cube_313 is empty.

Cube_314 is empty.

Cube_315 is empty.

Cube_316 is empty.

Cube_317 is empty.

Cube_318 is empty.

Cube_319 is empty.

Cube_320 is empty.

Cube_321 is empty.

Cube_322 is empty.

Cube_323 is empty.

Cube_324 is empty.

   > Found 1 clusters in hypercube 325.
   > Found 1 clusters in hypercube 326.
Cube_327 is empty.

Cube_328 is empty.

Cube_329 is empty.

Cube_330 is empty.

Cube_331 is empty.

   > Found 1 clusters in hypercube 332.
Cube_333 is empty.

Cube_334 is empty.

Cube_335 is empty.

   > Found 1 clusters in hypercube 336.
   > Found 1 clusters in hypercube 337.
   > Found 1 clusters in hypercube 338.
   > Found 1 clusters in hypercube 339.
Cube_340 is empty.

Cube_341 is empty.

Cube_342 is empty.

Cube_343 is empty.

Cube_344 is empty.

Cube_345 is empty.

Cube_346 is empty.

Cube_347 is empty.

Cube_348 is empty.

   > Found 1 clusters in hypercube 349.
   > Found 1 clusters in hypercube 350.
   > Found 1 clusters in hypercube 351.
   > Found 1 clusters in hypercube 352.
   > Found 1 clusters in hypercube 353.
Cube_354 is empty.

Cube_355 is empty.

Cube_356 is empty.

Cube_357 is empty.

Cube_358 is empty.

Cube_359 is empty.

Cube_360 is empty.

Cube_361 is empty.

   > Found 1 clusters in hypercube 362.
   > Found 1 clusters in hypercube 363.
   > Found 1 clusters in hypercube 364.
Cube_365 is empty.

Cube_366 is empty.

Cube_367 is empty.

Cube_368 is empty.

Cube_369 is empty.

Cube_370 is empty.

   > Found 1 clusters in hypercube 371.
Cube_372 is empty.

Cube_373 is empty.

Cube_374 is empty.

Cube_375 is empty.

Cube_376 is empty.

Cube_377 is empty.

Cube_378 is empty.

Cube_379 is empty.

Cube_380 is empty.

   > Found 1 clusters in hypercube 381.
   > Found 1 clusters in hypercube 382.
   > Found 1 clusters in hypercube 383.
Cube_384 is empty.

Cube_385 is empty.

Cube_386 is empty.

   > Found 1 clusters in hypercube 387.
   > Found 1 clusters in hypercube 388.
   > Found 1 clusters in hypercube 389.
   > Found 1 clusters in hypercube 390.
   > Found 1 clusters in hypercube 391.
   > Found 1 clusters in hypercube 392.
Cube_393 is empty.

Cube_394 is empty.

   > Found 1 clusters in hypercube 395.
   > Found 1 clusters in hypercube 396.
   > Found 1 clusters in hypercube 397.
   > Found 1 clusters in hypercube 398.
Cube_399 is empty.

   > Found 1 clusters in hypercube 400.
Cube_401 is empty.

Cube_402 is empty.

   > Found 1 clusters in hypercube 403.
   > Found 1 clusters in hypercube 404.
Cube_405 is empty.

Cube_406 is empty.

Cube_407 is empty.


Created 341 edges and 152 nodes in 0:00:00.180648.
Output graph examples to html
Wrote visualization to: output/digits_custom_tooltips.html
Wrote visualization to: output/digits_ylabel_tooltips.html
no display found. Using non-interactive Agg backend

import io
import sys
import base64

import matplotlib.pyplot as plt
import numpy as np
import sklearn
from sklearn import datasets
from sklearn.preprocessing import MinMaxScaler
import kmapper as km

try:
    from PIL import Image
except ImportError as e:
    print("This example requires Pillow. Run `pip install pillow` and then try again.")
    sys.exit()


# Load digits data
data, labels = datasets.load_digits().data, datasets.load_digits().target

# Raw data is (0, 16), so scale to 8 bits (pillow can't handle 4-bit greyscale PNG depth)
scaler = MinMaxScaler(feature_range=(0, 255))
data = scaler.fit_transform(data).astype(np.uint8)

# Create images for a custom tooltip array
tooltip_s = []
for image_data in data:
    with io.BytesIO() as output:
        img = Image.fromarray(image_data.reshape((8, 8)), "L")
        img.save(output, "PNG")
        contents = output.getvalue()
        img_encoded = base64.b64encode(contents)
        img_tag = """<img src="data:image/png;base64,{}">""".format(
            img_encoded.decode("utf-8")
        )
        tooltip_s.append(img_tag)

tooltip_s = np.array(
    tooltip_s
)  # need to make sure to feed it as a NumPy array, not a list

# Initialize to use t-SNE with 2 components (reduces data to 2 dimensions). Also note high overlap_percentage.
mapper = km.KeplerMapper(verbose=2)

# Fit and transform data
projected_data = mapper.fit_transform(data, projection=sklearn.manifold.TSNE())

# Create the graph (we cluster on the projected data and suffer projection loss)
graph = mapper.map(
    projected_data,
    clusterer=sklearn.cluster.DBSCAN(eps=0.3, min_samples=15),
    cover=km.Cover(35, 0.4),
)

# Create the visualizations (increased the graph_gravity for a tighter graph-look.)
print("Output graph examples to html")
# Tooltips with image data for every cluster member
mapper.visualize(
    graph,
    title="Handwritten digits Mapper",
    path_html="output/digits_custom_tooltips.html",
    color_values=labels,
    color_function_name="labels",
    custom_tooltips=tooltip_s,
)
# Tooltips with the target y-labels for every cluster member
mapper.visualize(
    graph,
    title="Handwritten digits Mapper",
    path_html="output/digits_ylabel_tooltips.html",
    custom_tooltips=labels,
)

# Matplotlib examples
km.draw_matplotlib(graph, layout="spring")
plt.show()

Total running time of the script: ( 0 minutes 9.937 seconds)

Gallery generated by Sphinx-Gallery