sklearn.feature_extraction.image
import
grid_to_graph
digits
=
()
images
=
digits
.
images
X
=
(
images
,
(
len
(
images
),
-
1
))
connectivity
=
grid_to_graph
(
*
images
[
0
]
.
shape
)
agglo
=
(
connectivity
=
connectivity
,
n_clusters
=
32
)
agglo
.
fit
(
X
)
X_reduced
=
agglo
.
transform
(
X
)
X_restored
=
agglo
.
inverse_transform
(
X_reduced
)
images_restored
=
(
X_restored
,
images
.
shape
)
(
1
,
figsize
=
(
4
,
3.5
))
()
(
left
=
0.01
,
right
=
0.99
,
bottom
=
0.01
,
top
=
0.91
)
for
i
in
range
(
4
):
(
3
,
4
,
i
+
1
)
(
images
[
i
],
cmap
=
plt
.
cm
.
gray
,
vmax
=
16
,
interpolation
=
"nearest"
)
(())
(())
if
i
==
1
:
(
"Original data"
)
(
3
,
4
,
4
+
i
+
1
)
(
images_restored
[
i
],
cmap
=
plt
.
cm
.
gray
,
vmax
=
16
,
interpolation
=
"nearest"
)
if
i
==
1
:
(
"Agglomerated data"
)
(())
(())
(
3
,
4
,
10
)
(
(
agglo
.
labels_
,
images
[
0
]
.
shape
),
interpolation
=
"nearest"
,
cmap
=
plt
.
cm
.
nipy_spectral
,
)
(())
(())
(
"Labels"
)
()
Total running time of the script: (0 minutes 0.099 seconds)
Related examples