Kernel Density Estimation
as
np
from
sklearn.datasets
import
from
sklearn.decomposition
import
from
sklearn.model_selection
import
from
sklearn.neighbors
import
digits
=
()
pca
=
(
n_components
=
15
,
whiten
=
False
)
data
=
pca
.
fit_transform
(
digits
.
data
)
params
=
{
"bandwidth"
:
(
-
1
,
1
,
20
)}
grid
=
(
(),
params
)
grid
.
fit
(
data
)
print
(
"best bandwidth:
{0}
"
.
format
(
grid
.
best_estimator_
.
bandwidth
))
kde
=
grid
.
best_estimator_
new_data
=
kde
.
sample
(
44
,
random_state
=
0
)
new_data
=
pca
.
inverse_transform
(
new_data
)
new_data
=
new_data
.
reshape
((
4
,
11
,
-
1
))
real_data
=
digits
.
data
[:
44
]
.
reshape
((
4
,
11
,
-
1
))
fig
,
ax
=
(
9
,
11
,
subplot_kw
=
dict
(
xticks
=
[],
yticks
=
[]))
for
j
in
range
(
11
):
ax
[
4
,
j
]
.
set_visible
(
False
)
for
i
in
range
(
4
):
im
=
ax
[
i
,
j
]
.
imshow
(
real_data
[
i
,
j
]
.
reshape
((
8
,
8
)),
cmap
=
plt
.
cm
.
binary
,
interpolation
=
"nearest"
)
im
.
set_clim
(
0
,
16
)
im
=
ax
[
i
+
5
,
j
]
.
imshow
(
new_data
[
i
,
j
]
.
reshape
((
8
,
8
)),
cmap
=
plt
.
cm
.
binary
,
interpolation
=
"nearest"
)
im
.
set_clim
(
0
,
16
)
ax
[
0
,
5
]
.
set_title
(
"Selection from the input data"
)
ax
[
5
,
5
]
.
set_title
(
'"New" digits drawn from the kernel density model'
)
()
Total running time of the script: (0 minutes 3.659 seconds)
Related examples