树状图

将树状图可视化为 SVG 图像。

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.hierarchy import Paris
from sknetwork.visualization import visualize_graph, visualize_bigraph
from sknetwork.visualization import visualize_dendrogram

图形

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
labels = graph.labels
[5]:
# graph
image = visualize_graph(adjacency, position, labels=labels)
SVG(image)
[5]:
../../_images/tutorials_visualization_dendrograms_7_0.svg
[6]:
# hierarchical clustering
paris = Paris()
dendrogram = paris.fit_transform(adjacency)
[7]:
# visualization
image = visualize_dendrogram(dendrogram)
SVG(image)
[7]:
../../_images/tutorials_visualization_dendrograms_9_0.svg
[8]:
# add names, set colors
n = adjacency.shape[0]
image = visualize_dendrogram(dendrogram, names=np.arange(n), n_clusters=5, color='gray')
SVG(image)
[8]:
../../_images/tutorials_visualization_dendrograms_10_0.svg
[9]:
# export
visualize_dendrogram(dendrogram, filename='dendrogram_karate_club')
[9]:
'<svg width="420" height="320"  xmlns="http://www.w3.org/2000/svg"><path stroke-width="2" stroke="red" d="M 351.1764705882353 310 351.1764705882353 306.05522660108716" /><path stroke-width="2" stroke="red" d="M 362.9411764705883 310 362.9411764705883 306.05522660108716" /><path stroke-width="2" stroke="red" d="M 351.1764705882353 306.05522660108716 362.9411764705883 306.05522660108716" /><path stroke-width="2" stroke="blue" d="M 33.529411764705884 310 33.529411764705884 306.05522660108716" /><path stroke-width="2" stroke="blue" d="M 45.294117647058826 310 45.294117647058826 306.05522660108716" /><path stroke-width="2" stroke="blue" d="M 33.529411764705884 306.05522660108716 45.294117647058826 306.05522660108716" /><path stroke-width="2" stroke="red" d="M 386.47058823529414 310 386.47058823529414 305.5621300890031" /><path stroke-width="2" stroke="red" d="M 398.2352941176471 310 398.2352941176471 305.5621300890031" /><path stroke-width="2" stroke="red" d="M 386.47058823529414 305.5621300890031 398.2352941176471 305.5621300890031" /><path stroke-width="2" stroke="blue" d="M 80.58823529411765 310 80.58823529411765 305.5621300890031" /><path stroke-width="2" stroke="blue" d="M 92.3529411764706 310 92.3529411764706 305.5621300890031" /><path stroke-width="2" stroke="blue" d="M 80.58823529411765 305.5621300890031 92.3529411764706 305.5621300890031" /><path stroke-width="2" stroke="red" d="M 257.05882352941177 310 257.05882352941177 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 268.82352941176475 310 268.82352941176475 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 257.05882352941177 304.0828399016307 268.82352941176475 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 357.0588235294118 306.05522660108716 357.0588235294118 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 374.7058823529412 310 374.7058823529412 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 357.0588235294118 304.0828399016307 374.7058823529412 304.0828399016307" /><path stroke-width="2" stroke="red" d="M 292.3529411764706 310 292.3529411764706 302.11045320217426" /><path stroke-width="2" stroke="red" d="M 304.11764705882354 310 304.11764705882354 302.11045320217426" /><path stroke-width="2" stroke="red" d="M 292.3529411764706 302.11045320217426 304.11764705882354 302.11045320217426" /><path stroke-width="2" stroke="red" d="M 221.76470588235296 310 221.76470588235296 301.12426017800624" /><path stroke-width="2" stroke="red" d="M 233.5294117647059 310 233.5294117647059 301.12426017800624" /><path stroke-width="2" stroke="red" d="M 221.76470588235296 301.12426017800624 233.5294117647059 301.12426017800624" /><path stroke-width="2" stroke="blue" d="M 57.05882352941177 310 57.05882352941177 301.12426017800624" /><path stroke-width="2" stroke="blue" d="M 68.82352941176471 310 68.82352941176471 301.12426017800624" /><path stroke-width="2" stroke="blue" d="M 57.05882352941177 301.12426017800624 68.82352941176471 301.12426017800624" /><path stroke-width="2" stroke="red" d="M 315.88235294117646 310 315.88235294117646 300.13806674387354" /><path stroke-width="2" stroke="red" d="M 327.64705882352945 310 327.64705882352945 300.13806674387354" /><path stroke-width="2" stroke="red" d="M 315.88235294117646 300.13806674387354 327.64705882352945 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 10.0 310 10.0 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 21.764705882352942 310 21.764705882352942 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 10.0 300.13806674387354 21.764705882352942 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 186.47058823529414 310 186.47058823529414 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 198.23529411764707 310 198.23529411764707 300.13806674387354" /><path stroke-width="2" stroke="blue" d="M 186.47058823529414 300.13806674387354 198.23529411764707 300.13806674387354" /><path stroke-width="2" stroke="red" d="M 210.0 310 210.0 299.15187374985" /><path stroke-width="2" stroke="red" d="M 227.64705882352945 301.12426017800624 227.64705882352945 299.15187374985" /><path stroke-width="2" stroke="red" d="M 210.0 299.15187374985 227.64705882352945 299.15187374985" /><path stroke-width="2" stroke="blue" d="M 104.11764705882354 310 104.11764705882354 298.1656798032614" /><path stroke-width="2" stroke="blue" d="M 115.88235294117648 310 115.88235294117648 298.1656798032614" /><path stroke-width="2" stroke="blue" d="M 104.11764705882354 298.1656798032614 115.88235294117648 298.1656798032614" /><path stroke-width="2" stroke="blue" d="M 62.94117647058824 301.12426017800624 62.94117647058824 296.6863895344989" /><path stroke-width="2" stroke="blue" d="M 86.47058823529412 305.5621300890031 86.47058823529412 296.6863895344989" /><path stroke-width="2" stroke="blue" d="M 62.94117647058824 296.6863895344989 86.47058823529412 296.6863895344989" /><path stroke-width="2" stroke="blue" d="M 110.0 298.1656798032614 110.0 296.19329361023193" /><path stroke-width="2" stroke="blue" d="M 127.64705882352942 310 127.64705882352942 296.19329361023193" /><path stroke-width="2" stroke="blue" d="M 110.0 296.19329361023193 127.64705882352942 296.19329361023193" /><path stroke-width="2" stroke="red" d="M 365.8823529411765 304.0828399016307 365.8823529411765 295.20709921147636" /><path stroke-width="2" stroke="red" d="M 392.3529411764706 305.5621300890031 392.3529411764706 295.20709921147636" /><path stroke-width="2" stroke="red" d="M 365.8823529411765 295.20709921147636 392.3529411764706 295.20709921147636" /><path stroke-width="2" stroke="red" d="M 262.9411764705883 304.0828399016307 262.9411764705883 294.2209064043485" /><path stroke-width="2" stroke="red" d="M 280.5882352941177 310 280.5882352941177 294.2209064043485" /><path stroke-width="2" stroke="red" d="M 262.9411764705883 294.2209064043485 280.5882352941177 294.2209064043485" /><path stroke-width="2" stroke="blue" d="M 118.82352941176471 296.19329361023193 118.82352941176471 294.2209064043485" /><path stroke-width="2" stroke="blue" d="M 139.41176470588238 310 139.41176470588238 294.2209064043485" /><path stroke-width="2" stroke="blue" d="M 118.82352941176471 294.2209064043485 139.41176470588238 294.2209064043485" /><path stroke-width="2" stroke="blue" d="M 162.94117647058823 310 162.94117647058823 293.2347133620938" /><path stroke-width="2" stroke="blue" d="M 174.7058823529412 310 174.7058823529412 293.2347133620938" /><path stroke-width="2" stroke="blue" d="M 162.94117647058823 293.2347133620938 174.7058823529412 293.2347133620938" /><path stroke-width="2" stroke="blue" d="M 129.11764705882354 294.2209064043485 129.11764705882354 292.2485203560125" /><path stroke-width="2" stroke="blue" d="M 151.1764705882353 310 151.1764705882353 292.2485203560125" /><path stroke-width="2" stroke="blue" d="M 129.11764705882354 292.2485203560125 151.1764705882353 292.2485203560125" /><path stroke-width="2" stroke="red" d="M 218.82352941176472 299.15187374985 218.82352941176472 290.76922968029976" /><path stroke-width="2" stroke="red" d="M 245.29411764705884 310 245.29411764705884 290.76922968029976" /><path stroke-width="2" stroke="red" d="M 218.82352941176472 290.76922968029976 245.29411764705884 290.76922968029976" /><path stroke-width="2" stroke="blue" d="M 15.882352941176471 300.13806674387354 15.882352941176471 283.37277906899783" /><path stroke-width="2" stroke="blue" d="M 39.411764705882355 306.05522660108716 39.411764705882355 283.37277906899783" /><path stroke-width="2" stroke="blue" d="M 15.882352941176471 283.37277906899783 39.411764705882355 283.37277906899783" /><path stroke-width="2" stroke="red" d="M 321.7647058823529 300.13806674387354 321.7647058823529 280.4141984229527" /><path stroke-width="2" stroke="red" d="M 339.4117647058824 310 339.4117647058824 280.4141984229527" /><path stroke-width="2" stroke="red" d="M 321.7647058823529 280.4141984229527 339.4117647058824 280.4141984229527" /><path stroke-width="2" stroke="blue" d="M 140.14705882352942 292.2485203560125 140.14705882352942 278.770543893454" /><path stroke-width="2" stroke="blue" d="M 168.8235294117647 293.2347133620938 168.8235294117647 278.770543893454" /><path stroke-width="2" stroke="blue" d="M 140.14705882352942 278.770543893454 168.8235294117647 278.770543893454" /><path stroke-width="2" stroke="red" d="M 271.764705882353 294.2209064043485 271.764705882353 276.46942672418766" /><path stroke-width="2" stroke="red" d="M 298.2352941176471 302.11045320217426 298.2352941176471 276.46942672418766" /><path stroke-width="2" stroke="red" d="M 271.764705882353 276.46942672418766 298.2352941176471 276.46942672418766" /><path stroke-width="2" stroke="red" d="M 232.05882352941177 290.76922968029976 232.05882352941177 271.86718783518063" /><path stroke-width="2" stroke="red" d="M 285.0 276.46942672418766 285.0 271.86718783518063" /><path stroke-width="2" stroke="red" d="M 232.05882352941177 271.86718783518063 285.0 271.86718783518063" /><path stroke-width="2" stroke="blue" d="M 154.48529411764707 278.770543893454 154.48529411764707 266.73076710305537" /><path stroke-width="2" stroke="blue" d="M 192.3529411764706 300.13806674387354 192.3529411764706 266.73076710305537" /><path stroke-width="2" stroke="blue" d="M 154.48529411764707 266.73076710305537 192.3529411764706 266.73076710305537" /><path stroke-width="2" stroke="red" d="M 258.52941176470586 271.86718783518063 258.52941176470586 256.1115744635648" /><path stroke-width="2" stroke="red" d="M 330.5882352941177 280.4141984229527 330.5882352941177 256.1115744635648" /><path stroke-width="2" stroke="red" d="M 258.52941176470586 256.1115744635648 330.5882352941177 256.1115744635648" /><path stroke-width="2" stroke="blue" d="M 27.647058823529413 283.37277906899783 27.647058823529413 254.52662363268593" /><path stroke-width="2" stroke="blue" d="M 74.70588235294117 296.6863895344989 74.70588235294117 254.52662363268593" /><path stroke-width="2" stroke="blue" d="M 27.647058823529413 254.52662363268593 74.70588235294117 254.52662363268593" /><path stroke-width="2" stroke="blue" d="M 51.17647058823529 254.52662363268593 51.17647058823529 231.104529127874" /><path stroke-width="2" stroke="blue" d="M 173.41911764705884 266.73076710305537 173.41911764705884 231.104529127874" /><path stroke-width="2" stroke="blue" d="M 51.17647058823529 231.104529127874 173.41911764705884 231.104529127874" /><path stroke-width="2" stroke="red" d="M 294.55882352941177 256.1115744635648 294.55882352941177 187.71202388611835" /><path stroke-width="2" stroke="red" d="M 379.11764705882354 295.20709921147636 379.11764705882354 187.71202388611835" /><path stroke-width="2" stroke="red" d="M 294.55882352941177 187.71202388611835 379.11764705882354 187.71202388611835" /><path stroke-width="2" stroke="black" d="M 112.29779411764707 231.104529127874 112.29779411764707 10.0" /><path stroke-width="2" stroke="black" d="M 336.8382352941177 187.71202388611835 336.8382352941177 10.0" /><path stroke-width="2" stroke="black" d="M 112.29779411764707 10.0 336.8382352941177 10.0" /></svg>'

有向图

[10]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[11]:
# graph
image = visualize_graph(adjacency, position, names)
SVG(image)
[11]:
../../_images/tutorials_visualization_dendrograms_14_0.svg
[12]:
# hierarchical clustering
paris = Paris()
dendrogram = paris.fit_transform(adjacency)
[13]:
# visualization
image = visualize_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
SVG(image)
[13]:
../../_images/tutorials_visualization_dendrograms_16_0.svg

二部图

[14]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[15]:
# graph
image = visualize_bigraph(biadjacency, names_row, names_col)
SVG(image)
[15]:
../../_images/tutorials_visualization_dendrograms_19_0.svg
[16]:
# hierarchical clustering
paris = Paris()
paris.fit(biadjacency)
dendrogram_row = paris.dendrogram_row_
dendrogram_col = paris.dendrogram_col_
dendrogram_full = paris.dendrogram_full_
[17]:
# visualization
image = visualize_dendrogram(dendrogram_row, names_row, n_clusters=3, rotate=True)
SVG(image)
[17]:
../../_images/tutorials_visualization_dendrograms_21_0.svg
[18]:
image = visualize_dendrogram(dendrogram_col, names_col, n_clusters=3, rotate=True)
SVG(image)

[18]:
../../_images/tutorials_visualization_dendrograms_22_0.svg