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)

[t-SNE] Computing 91 nearest neighbors...
[t-SNE] Indexed 1797 samples in 0.000s...
[t-SNE] Computed neighbors for 1797 samples in 0.115s...
[t-SNE] Computed conditional probabilities for sample 1000 / 1797
[t-SNE] Computed conditional probabilities for sample 1797 / 1797
[t-SNE] Mean sigma: 157.044611
[t-SNE] Computed conditional probabilities in 0.115s
[t-SNE] Iteration 50: error = 74.3177795, gradient norm = 0.1374945 (50 iterations in 0.692s)
[t-SNE] Iteration 100: error = 63.8778496, gradient norm = 0.0524094 (50 iterations in 0.449s)
[t-SNE] Iteration 150: error = 62.4267349, gradient norm = 0.0341171 (50 iterations in 0.413s)
[t-SNE] Iteration 200: error = 61.9249268, gradient norm = 0.0215864 (50 iterations in 0.421s)
[t-SNE] Iteration 250: error = 61.6830444, gradient norm = 0.0161740 (50 iterations in 0.417s)
[t-SNE] KL divergence after 250 iterations with early exaggeration: 61.683044
[t-SNE] Iteration 300: error = 1.0435960, gradient norm = 0.0008779 (50 iterations in 0.393s)
[t-SNE] Iteration 350: error = 0.8707247, gradient norm = 0.0003671 (50 iterations in 0.394s)
[t-SNE] Iteration 400: error = 0.8159606, gradient norm = 0.0002398 (50 iterations in 0.393s)
[t-SNE] Iteration 450: error = 0.7922584, gradient norm = 0.0001869 (50 iterations in 0.400s)
[t-SNE] Iteration 500: error = 0.7798760, gradient norm = 0.0001477 (50 iterations in 0.393s)
[t-SNE] Iteration 550: error = 0.7717075, gradient norm = 0.0001203 (50 iterations in 0.394s)
[t-SNE] Iteration 600: error = 0.7649608, gradient norm = 0.0001225 (50 iterations in 0.396s)
[t-SNE] Iteration 650: error = 0.7607430, gradient norm = 0.0001150 (50 iterations in 0.399s)
[t-SNE] Iteration 700: error = 0.7575969, gradient norm = 0.0001114 (50 iterations in 0.398s)
[t-SNE] Iteration 750: error = 0.7549774, gradient norm = 0.0001055 (50 iterations in 0.395s)
[t-SNE] Iteration 800: error = 0.7527757, gradient norm = 0.0000913 (50 iterations in 0.395s)
[t-SNE] Iteration 850: error = 0.7507852, gradient norm = 0.0000992 (50 iterations in 0.403s)
[t-SNE] Iteration 900: error = 0.7490521, gradient norm = 0.0000817 (50 iterations in 0.399s)
[t-SNE] Iteration 950: error = 0.7473730, gradient norm = 0.0000816 (50 iterations in 0.395s)
[t-SNE] Iteration 1000: error = 0.7459844, gradient norm = 0.0000861 (50 iterations in 0.392s)
[t-SNE] KL divergence after 1000 iterations: 0.745984

..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.

Cube_1 is empty.

Cube_2 is empty.

   > Found 1 clusters in hypercube 3.
   > Found 1 clusters in hypercube 4.
Cube_5 is empty.

Cube_6 is empty.

Cube_7 is empty.

   > Found 1 clusters in hypercube 8.
Cube_9 is empty.

Cube_10 is empty.

Cube_11 is empty.

Cube_12 is empty.

Cube_13 is empty.

Cube_14 is empty.

Cube_15 is empty.

   > Found 1 clusters in hypercube 16.
   > Found 1 clusters in hypercube 17.
   > Found 1 clusters in hypercube 18.
   > Found 1 clusters in hypercube 19.
Cube_20 is empty.

Cube_21 is empty.

Cube_22 is empty.

Cube_23 is empty.

   > Found 1 clusters in hypercube 24.
   > Found 1 clusters in hypercube 25.
   > Found 1 clusters in hypercube 26.
Cube_27 is empty.

   > Found 1 clusters in hypercube 28.
Cube_29 is empty.

Cube_30 is empty.

   > Found 1 clusters in hypercube 31.
   > Found 1 clusters in hypercube 32.
   > Found 1 clusters in hypercube 33.
   > Found 1 clusters in hypercube 34.
   > Found 1 clusters in hypercube 35.
Cube_36 is empty.

Cube_37 is empty.

   > Found 1 clusters in hypercube 38.
Cube_39 is empty.

   > Found 1 clusters in hypercube 40.
   > Found 1 clusters in hypercube 41.
Cube_42 is empty.

Cube_43 is empty.

   > Found 1 clusters in hypercube 44.
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.

Cube_51 is empty.

Cube_52 is empty.

   > Found 1 clusters in hypercube 53.
   > Found 1 clusters in hypercube 54.
Cube_55 is empty.

Cube_56 is empty.

Cube_57 is empty.

Cube_58 is empty.

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

Cube_63 is empty.

Cube_64 is empty.

Cube_65 is empty.

   > Found 1 clusters in hypercube 66.
   > Found 1 clusters in hypercube 67.
   > Found 1 clusters in hypercube 68.
   > Found 1 clusters in hypercube 69.
Cube_70 is empty.

Cube_71 is empty.

Cube_72 is empty.

Cube_73 is empty.

Cube_74 is empty.

Cube_75 is empty.

   > Found 1 clusters in hypercube 76.
   > Found 1 clusters in hypercube 77.
Cube_78 is empty.

   > Found 1 clusters in hypercube 79.
   > Found 1 clusters in hypercube 80.
Cube_81 is empty.

Cube_82 is empty.

Cube_83 is empty.

   > 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.

Cube_90 is empty.

Cube_91 is empty.

Cube_92 is empty.

   > Found 1 clusters in hypercube 93.
   > Found 1 clusters in hypercube 94.
   > Found 1 clusters in hypercube 95.
Cube_96 is empty.

Cube_97 is empty.

Cube_98 is empty.

Cube_99 is empty.

Cube_100 is empty.

   > Found 1 clusters in hypercube 101.
Cube_102 is empty.

Cube_103 is empty.

Cube_104 is empty.

Cube_105 is empty.

Cube_106 is empty.

Cube_107 is empty.

   > Found 1 clusters in hypercube 108.
   > Found 1 clusters in hypercube 109.
   > Found 1 clusters in hypercube 110.
Cube_111 is empty.

Cube_112 is empty.

Cube_113 is empty.

   > Found 1 clusters in hypercube 114.
   > Found 1 clusters in hypercube 115.
Cube_116 is empty.

Cube_117 is empty.

Cube_118 is empty.

Cube_119 is empty.

Cube_120 is empty.

Cube_121 is empty.

Cube_122 is empty.

Cube_123 is empty.

Cube_124 is empty.

Cube_125 is empty.

   > Found 1 clusters in hypercube 126.
Cube_127 is empty.

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.

   > Found 1 clusters in hypercube 137.
Cube_138 is empty.

Cube_139 is empty.

Cube_140 is empty.

Cube_141 is empty.

   > Found 1 clusters in hypercube 142.
   > Found 1 clusters in hypercube 143.
Cube_144 is empty.

   > Found 1 clusters in hypercube 145.
Cube_146 is empty.

Cube_147 is empty.

Cube_148 is empty.

Cube_149 is empty.

Cube_150 is empty.

Cube_151 is empty.

Cube_152 is empty.

Cube_153 is empty.

   > Found 1 clusters in hypercube 154.
   > Found 1 clusters in hypercube 155.
   > Found 1 clusters in hypercube 156.
Cube_157 is empty.

Cube_158 is empty.

Cube_159 is empty.

Cube_160 is empty.

Cube_161 is empty.

Cube_162 is empty.

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

Cube_166 is empty.

Cube_167 is empty.

Cube_168 is empty.

Cube_169 is empty.

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.
Cube_176 is empty.

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.

Cube_184 is empty.

Cube_185 is empty.

Cube_186 is empty.

   > Found 1 clusters in hypercube 187.
   > Found 1 clusters in hypercube 188.
Cube_189 is empty.

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.

   > Found 1 clusters in hypercube 197.
   > Found 1 clusters in hypercube 198.
   > Found 1 clusters in hypercube 199.
Cube_200 is empty.

Cube_201 is empty.

Cube_202 is empty.

Cube_203 is empty.

Cube_204 is empty.

Cube_205 is empty.

Cube_206 is empty.

Cube_207 is empty.

Cube_208 is empty.

Cube_209 is empty.

Cube_210 is empty.

Cube_211 is empty.

   > Found 1 clusters in hypercube 212.
   > Found 1 clusters in hypercube 213.
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.
   > Found 1 clusters in hypercube 222.
   > Found 1 clusters in hypercube 223.
   > Found 1 clusters in hypercube 224.
   > Found 1 clusters in hypercube 225.
Cube_226 is empty.

Cube_227 is empty.

Cube_228 is empty.

Cube_229 is empty.

Cube_230 is empty.

Cube_231 is empty.

Cube_232 is empty.

Cube_233 is empty.

   > Found 1 clusters in hypercube 234.
   > Found 1 clusters in hypercube 235.
Cube_236 is empty.

Cube_237 is empty.

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.

   > Found 1 clusters in hypercube 245.
   > 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.
Cube_251 is empty.

Cube_252 is empty.

Cube_253 is empty.

Cube_254 is empty.

Cube_255 is empty.

Cube_256 is empty.

Cube_257 is empty.

Cube_258 is empty.

Cube_259 is empty.

Cube_260 is empty.

   > Found 1 clusters in hypercube 261.
   > Found 1 clusters in hypercube 262.
   > Found 1 clusters in hypercube 263.
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.

Cube_272 is empty.

Cube_273 is empty.

Cube_274 is empty.

Cube_275 is empty.

Cube_276 is empty.

Cube_277 is empty.

   > Found 1 clusters in hypercube 278.
   > Found 1 clusters in hypercube 279.
   > Found 1 clusters in hypercube 280.
   > Found 1 clusters in hypercube 281.
Cube_282 is empty.

Cube_283 is empty.

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.

Cube_293 is empty.

Cube_294 is empty.

Cube_295 is empty.

Cube_296 is empty.

Cube_297 is empty.

Cube_298 is empty.

Cube_299 is empty.

Cube_300 is empty.

   > Found 1 clusters in hypercube 301.
   > Found 1 clusters in hypercube 302.
   > Found 1 clusters in hypercube 303.
Cube_304 is empty.

Cube_305 is empty.

Cube_306 is empty.

Cube_307 is empty.

Cube_308 is empty.

Cube_309 is empty.

Cube_310 is empty.

Cube_311 is empty.

Cube_312 is empty.

Cube_313 is empty.

Cube_314 is empty.

Cube_315 is empty.

Cube_316 is empty.

Cube_317 is empty.

Cube_318 is empty.

   > Found 1 clusters in hypercube 319.
   > Found 1 clusters in hypercube 320.
   > Found 1 clusters in hypercube 321.
   > Found 1 clusters in hypercube 322.
   > Found 1 clusters in hypercube 323.
Cube_324 is empty.

Cube_325 is empty.

Cube_326 is empty.

   > Found 1 clusters in hypercube 327.
   > Found 1 clusters in hypercube 328.
   > Found 1 clusters in hypercube 329.
   > Found 1 clusters in hypercube 330.
Cube_331 is empty.

Cube_332 is empty.

Cube_333 is empty.

Cube_334 is empty.

Cube_335 is empty.

   > Found 1 clusters in hypercube 336.
   > Found 1 clusters in hypercube 337.
Cube_338 is empty.

Cube_339 is empty.

   > Found 1 clusters in hypercube 340.
   > Found 1 clusters in hypercube 341.
   > Found 1 clusters in hypercube 342.
   > Found 1 clusters in hypercube 343.
Cube_344 is empty.

Cube_345 is empty.

   > Found 1 clusters in hypercube 346.
   > Found 1 clusters in hypercube 347.
   > Found 1 clusters in hypercube 348.
   > Found 1 clusters in hypercube 349.
   > Found 1 clusters in hypercube 350.
Cube_351 is empty.

Cube_352 is empty.

Cube_353 is empty.

Cube_354 is empty.

   > Found 1 clusters in hypercube 355.
   > Found 1 clusters in hypercube 356.
   > Found 1 clusters in hypercube 357.
Cube_358 is empty.

Cube_359 is empty.

Cube_360 is empty.

Cube_361 is empty.

Cube_362 is empty.

Cube_363 is empty.

Cube_364 is empty.

Cube_365 is empty.

Cube_366 is empty.

   > Found 1 clusters in hypercube 367.
   > Found 1 clusters in hypercube 368.
Cube_369 is empty.

Cube_370 is empty.

Cube_371 is empty.

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.

Cube_381 is empty.

   > 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.
Cube_388 is empty.

   > Found 1 clusters in hypercube 389.
   > Found 1 clusters in hypercube 390.
Cube_391 is empty.

Cube_392 is empty.

Cube_393 is empty.

   > Found 1 clusters in hypercube 394.
   > Found 1 clusters in hypercube 395.
   > Found 1 clusters in hypercube 396.
   > Found 1 clusters in hypercube 397.
   > Found 1 clusters in hypercube 398.
   > Found 1 clusters in hypercube 399.
Cube_400 is empty.

Cube_401 is empty.

   > Found 1 clusters in hypercube 402.
   > Found 1 clusters in hypercube 403.
   > Found 1 clusters in hypercube 404.
   > Found 1 clusters in hypercube 405.
   > Found 1 clusters in hypercube 406.
   > Found 1 clusters in hypercube 407.
   > Found 1 clusters in hypercube 408.
Cube_409 is empty.

Cube_410 is empty.

Cube_411 is empty.

Cube_412 is empty.

Cube_413 is empty.

Cube_414 is empty.

Cube_415 is empty.

Cube_416 is empty.

Cube_417 is empty.

Cube_418 is empty.

Cube_419 is empty.

Cube_420 is empty.

Cube_421 is empty.

Cube_422 is empty.

Cube_423 is empty.

Cube_424 is empty.

   > Found 1 clusters in hypercube 425.
Cube_426 is empty.

Cube_427 is empty.

Cube_428 is empty.

Cube_429 is empty.

   > Found 1 clusters in hypercube 430.
   > Found 1 clusters in hypercube 431.
   > Found 1 clusters in hypercube 432.
   > Found 1 clusters in hypercube 433.
Cube_434 is empty.

Cube_435 is empty.

Cube_436 is empty.

Cube_437 is empty.

   > Found 1 clusters in hypercube 438.
   > Found 1 clusters in hypercube 439.
   > Found 1 clusters in hypercube 440.
   > Found 1 clusters in hypercube 441.
   > Found 1 clusters in hypercube 442.
Cube_443 is empty.

Cube_444 is empty.

Cube_445 is empty.

   > Found 1 clusters in hypercube 446.
   > Found 1 clusters in hypercube 447.
   > Found 1 clusters in hypercube 448.
   > Found 1 clusters in hypercube 449.
Cube_450 is empty.

Cube_451 is empty.

Cube_452 is empty.

Cube_453 is empty.

   > Found 1 clusters in hypercube 454.
   > Found 1 clusters in hypercube 455.
Cube_456 is empty.

Cube_457 is empty.


Created 319 edges and 151 nodes in 0:00:00.177320.
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.762 seconds)

Gallery generated by Sphinx-Gallery