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
[1]:
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
from pathlib import Path
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
if Path("output/").is_dir():
prepend = "output/"
else:
prepend = "./"
mapper.visualize(
graph,
title="Handwritten digits Mapper",
path_html=prepend + "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=prepend + "digits_ylabel_tooltips.html",
custom_tooltips=labels,
)
# Matplotlib examples
km.draw_matplotlib(graph, layout="spring")
plt.show()
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.072s...
[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.055s
[t-SNE] Iteration 50: error = 67.4981155, gradient norm = 0.0369087 (50 iterations in 0.251s)
[t-SNE] Iteration 100: error = 62.6626358, gradient norm = 0.0087897 (50 iterations in 0.179s)
[t-SNE] Iteration 150: error = 61.8415680, gradient norm = 0.0029798 (50 iterations in 0.181s)
[t-SNE] Iteration 200: error = 61.5398254, gradient norm = 0.0018235 (50 iterations in 0.181s)
[t-SNE] Iteration 250: error = 61.3906517, gradient norm = 0.0012604 (50 iterations in 0.183s)
[t-SNE] KL divergence after 250 iterations with early exaggeration: 61.390652
[t-SNE] Iteration 300: error = 1.3391963, gradient norm = 0.0208161 (50 iterations in 0.174s)
[t-SNE] Iteration 350: error = 1.0354826, gradient norm = 0.0159741 (50 iterations in 0.169s)
[t-SNE] Iteration 400: error = 0.9206197, gradient norm = 0.0133829 (50 iterations in 0.166s)
[t-SNE] Iteration 450: error = 0.8608383, gradient norm = 0.0117988 (50 iterations in 0.168s)
[t-SNE] Iteration 500: error = 0.8250802, gradient norm = 0.0106276 (50 iterations in 0.167s)
[t-SNE] Iteration 550: error = 0.8024386, gradient norm = 0.0090440 (50 iterations in 0.169s)
[t-SNE] Iteration 600: error = 0.7875072, gradient norm = 0.0073222 (50 iterations in 0.166s)
[t-SNE] Iteration 650: error = 0.7781426, gradient norm = 0.0063616 (50 iterations in 0.166s)
[t-SNE] Iteration 700: error = 0.7715272, gradient norm = 0.0051712 (50 iterations in 0.168s)
[t-SNE] Iteration 750: error = 0.7668114, gradient norm = 0.0043388 (50 iterations in 0.233s)
[t-SNE] Iteration 800: error = 0.7634522, gradient norm = 0.0037223 (50 iterations in 0.170s)
[t-SNE] Iteration 850: error = 0.7606221, gradient norm = 0.0032000 (50 iterations in 0.168s)
[t-SNE] Iteration 900: error = 0.7584167, gradient norm = 0.0025068 (50 iterations in 0.168s)
[t-SNE] Iteration 950: error = 0.7564058, gradient norm = 0.0024263 (50 iterations in 0.168s)
[t-SNE] Iteration 1000: error = 0.7543452, gradient norm = 0.0025259 (50 iterations in 0.167s)
[t-SNE] KL divergence after 1000 iterations: 0.754345
..Scaling with: MinMaxScaler()
Mapping on data shaped (1797, 2) using lens shaped (1797, 2)
Minimal points in hypercube before clustering: 15
Creating 1225 hypercubes.
> Found 1 clusters in hypercube 0.
> Found 1 clusters in hypercube 1.
Cube_2 is empty.
Cube_3 is empty.
Cube_4 is empty.
Cube_5 is empty.
Cube_6 is empty.
Cube_7 is empty.
Cube_8 is empty.
Cube_9 is empty.
> Found 1 clusters in hypercube 10.
> Found 1 clusters in hypercube 11.
> Found 1 clusters in hypercube 12.
Cube_13 is empty.
Cube_14 is empty.
Cube_15 is empty.
Cube_16 is empty.
> Found 1 clusters in hypercube 17.
> Found 1 clusters in hypercube 18.
> Found 1 clusters in hypercube 19.
> Found 1 clusters in hypercube 20.
Cube_21 is empty.
Cube_22 is empty.
Cube_23 is empty.
Cube_24 is empty.
Cube_25 is empty.
Cube_26 is empty.
> Found 1 clusters in hypercube 27.
> Found 1 clusters in hypercube 28.
> Found 1 clusters in hypercube 29.
> Found 1 clusters in hypercube 30.
> Found 1 clusters in hypercube 31.
Cube_32 is empty.
Cube_33 is empty.
> Found 1 clusters in hypercube 34.
Cube_35 is empty.
Cube_36 is empty.
Cube_37 is empty.
Cube_38 is empty.
Cube_39 is empty.
> Found 1 clusters in hypercube 40.
> Found 1 clusters in hypercube 41.
> 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.
> Found 1 clusters in hypercube 48.
> Found 1 clusters in hypercube 49.
> Found 1 clusters in hypercube 50.
Cube_51 is empty.
Cube_52 is empty.
Cube_53 is empty.
Cube_54 is empty.
Cube_55 is empty.
Cube_56 is empty.
Cube_57 is empty.
Cube_58 is empty.
Cube_59 is empty.
Cube_60 is empty.
Cube_61 is empty.
> Found 1 clusters in hypercube 62.
> Found 1 clusters in hypercube 63.
> Found 1 clusters in hypercube 64.
> Found 1 clusters in hypercube 65.
Cube_66 is empty.
> Found 1 clusters in hypercube 67.
> Found 1 clusters in hypercube 68.
Cube_69 is empty.
Cube_70 is empty.
Cube_71 is empty.
Cube_72 is empty.
Cube_73 is empty.
Cube_74 is empty.
Cube_75 is empty.
Cube_76 is empty.
> 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.
> Found 1 clusters in hypercube 82.
> Found 1 clusters in hypercube 83.
Cube_84 is empty.
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.
> Found 1 clusters in hypercube 92.
> Found 1 clusters in hypercube 93.
> Found 1 clusters in hypercube 94.
Cube_95 is empty.
Cube_96 is empty.
> 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.
Cube_105 is empty.
Cube_106 is empty.
Cube_107 is empty.
Cube_108 is empty.
Cube_109 is empty.
Cube_110 is empty.
Cube_111 is empty.
> Found 1 clusters in hypercube 112.
> Found 1 clusters in hypercube 113.
> 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.
Cube_126 is empty.
Cube_127 is empty.
Cube_128 is empty.
Cube_129 is empty.
Cube_130 is empty.
> Found 1 clusters in hypercube 131.
> Found 1 clusters in hypercube 132.
Cube_133 is empty.
Cube_134 is empty.
Cube_135 is empty.
Cube_136 is empty.
Cube_137 is empty.
Cube_138 is empty.
> Found 1 clusters in hypercube 139.
> Found 1 clusters in hypercube 140.
> Found 1 clusters in hypercube 141.
> Found 1 clusters in hypercube 142.
Cube_143 is empty.
> Found 1 clusters in hypercube 144.
Cube_145 is empty.
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.
> Found 1 clusters in hypercube 153.
> Found 1 clusters in hypercube 154.
Cube_155 is empty.
Cube_156 is empty.
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.
> Found 1 clusters in hypercube 165.
Cube_166 is empty.
Cube_167 is empty.
Cube_168 is empty.
Cube_169 is empty.
Cube_170 is empty.
Cube_171 is empty.
> Found 1 clusters in hypercube 172.
> Found 1 clusters in hypercube 173.
Cube_174 is empty.
Cube_175 is empty.
Cube_176 is empty.
Cube_177 is empty.
Cube_178 is empty.
> Found 1 clusters in hypercube 179.
> Found 1 clusters in hypercube 180.
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.
> Found 1 clusters in hypercube 189.
Cube_190 is empty.
Cube_191 is empty.
> Found 1 clusters in hypercube 192.
> Found 1 clusters in hypercube 193.
> Found 1 clusters in hypercube 194.
Cube_195 is empty.
Cube_196 is empty.
Cube_197 is empty.
Cube_198 is empty.
Cube_199 is empty.
Cube_200 is empty.
> 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.
> Found 1 clusters in hypercube 207.
Cube_208 is empty.
Cube_209 is empty.
> Found 1 clusters in hypercube 210.
> Found 1 clusters in hypercube 211.
> Found 1 clusters in hypercube 212.
Cube_213 is empty.
Cube_214 is empty.
> Found 1 clusters in hypercube 215.
Cube_216 is empty.
Cube_217 is empty.
Cube_218 is empty.
Cube_219 is empty.
Cube_220 is empty.
Cube_221 is empty.
Cube_222 is empty.
> 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.
> Found 1 clusters in hypercube 231.
> Found 1 clusters in hypercube 232.
> Found 1 clusters in hypercube 233.
> Found 1 clusters in hypercube 234.
> Found 1 clusters in hypercube 235.
> Found 1 clusters in hypercube 236.
> Found 1 clusters in hypercube 237.
Cube_238 is empty.
Cube_239 is empty.
Cube_240 is empty.
> Found 1 clusters in hypercube 241.
Cube_242 is empty.
Cube_243 is empty.
> Found 1 clusters in hypercube 244.
Cube_245 is empty.
Cube_246 is empty.
Cube_247 is empty.
> Found 1 clusters in hypercube 248.
> Found 1 clusters in hypercube 249.
> Found 1 clusters in hypercube 250.
Cube_251 is empty.
Cube_252 is empty.
Cube_253 is empty.
> Found 1 clusters in hypercube 254.
> Found 1 clusters in hypercube 255.
> Found 1 clusters in hypercube 256.
Cube_257 is empty.
> Found 1 clusters in hypercube 258.
> Found 1 clusters in hypercube 259.
> Found 1 clusters in hypercube 260.
> Found 1 clusters in hypercube 261.
Cube_262 is empty.
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.
> Found 1 clusters in hypercube 271.
> Found 1 clusters in hypercube 272.
Cube_273 is empty.
Cube_274 is empty.
> Found 1 clusters in hypercube 275.
> Found 1 clusters in hypercube 276.
Cube_277 is empty.
Cube_278 is empty.
Cube_279 is empty.
Cube_280 is empty.
> Found 1 clusters in hypercube 281.
> Found 1 clusters in hypercube 282.
Cube_283 is empty.
> Found 1 clusters in hypercube 284.
> Found 1 clusters in hypercube 285.
> Found 1 clusters in hypercube 286.
Cube_287 is empty.
Cube_288 is empty.
Cube_289 is empty.
Cube_290 is empty.
Cube_291 is empty.
> Found 1 clusters in hypercube 292.
> Found 1 clusters in hypercube 293.
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.
Cube_301 is empty.
Cube_302 is empty.
Cube_303 is empty.
Cube_304 is empty.
Cube_305 is empty.
Cube_306 is empty.
Cube_307 is empty.
Cube_308 is empty.
Cube_309 is empty.
> Found 1 clusters in hypercube 310.
> Found 1 clusters in hypercube 311.
> Found 1 clusters in hypercube 312.
Cube_313 is empty.
> Found 1 clusters in hypercube 314.
> Found 1 clusters in hypercube 315.
Cube_316 is empty.
Cube_317 is empty.
Cube_318 is empty.
Cube_319 is empty.
Cube_320 is empty.
Cube_321 is empty.
> Found 1 clusters in hypercube 322.
Cube_323 is empty.
> Found 1 clusters in hypercube 324.
> Found 1 clusters in hypercube 325.
Cube_326 is empty.
Cube_327 is empty.
Cube_328 is empty.
Cube_329 is empty.
Cube_330 is empty.
Cube_331 is empty.
Cube_332 is empty.
> Found 1 clusters in hypercube 333.
Cube_334 is empty.
> Found 1 clusters in hypercube 335.
Cube_336 is empty.
Cube_337 is empty.
Cube_338 is empty.
Cube_339 is empty.
Cube_340 is empty.
Cube_341 is empty.
Cube_342 is empty.
> Found 1 clusters in hypercube 343.
Cube_344 is empty.
Cube_345 is empty.
Cube_346 is empty.
> 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.
> Found 1 clusters in hypercube 354.
> Found 1 clusters in hypercube 355.
> Found 1 clusters in hypercube 356.
Cube_357 is empty.
Cube_358 is empty.
> Found 1 clusters in hypercube 359.
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.
> Found 1 clusters in hypercube 365.
Cube_366 is empty.
Cube_367 is empty.
> Found 1 clusters in hypercube 368.
Cube_369 is empty.
Cube_370 is empty.
Cube_371 is empty.
Cube_372 is empty.
> Found 1 clusters in hypercube 373.
> Found 1 clusters in hypercube 374.
> Found 1 clusters in hypercube 375.
Cube_376 is empty.
Cube_377 is empty.
Cube_378 is empty.
Cube_379 is empty.
Cube_380 is empty.
Cube_381 is empty.
Cube_382 is empty.
Cube_383 is empty.
Cube_384 is empty.
> Found 1 clusters in hypercube 385.
> Found 1 clusters in hypercube 386.
> Found 1 clusters in hypercube 387.
Cube_388 is empty.
Cube_389 is empty.
> Found 1 clusters in hypercube 390.
> Found 1 clusters in hypercube 391.
> Found 1 clusters in hypercube 392.
> Found 1 clusters in hypercube 393.
Cube_394 is empty.
Cube_395 is empty.
Cube_396 is empty.
Cube_397 is empty.
Cube_398 is empty.
> Found 1 clusters in hypercube 399.
> Found 1 clusters in hypercube 400.
> Found 1 clusters in hypercube 401.
> Found 1 clusters in hypercube 402.
Cube_403 is empty.
Cube_404 is empty.
Cube_405 is empty.
> Found 1 clusters in hypercube 406.
Cube_407 is empty.
> Found 1 clusters in hypercube 408.
> Found 1 clusters in hypercube 409.
Cube_410 is empty.
Cube_411 is empty.
Cube_412 is empty.
> Found 1 clusters in hypercube 413.
> Found 1 clusters in hypercube 414.
Cube_415 is empty.
Created 326 edges and 149 nodes in 0:00:00.140383.
Output graph examples to html
Wrote visualization to: ./digits_custom_tooltips.html
Wrote visualization to: ./digits_ylabel_tooltips.html
no display found. Using non-interactive Agg backend
scikit-tda/kepler-mapper