Face completion with a multi-output estimators
import
from
sklearn.ensemble
import
from
sklearn.linear_model
import
,
from
sklearn.neighbors
import
from
sklearn.utils.validation
import
data
,
targets
=
(
return_X_y
=
True
)
train
=
data
[
targets
<
30
]
test
=
data
[
targets
>=
30
]
n_faces
=
5
rng
=
(
4
)
face_ids
=
rng
.
randint
(
test
.
shape
[
0
],
size
=
(
n_faces
,))
test
=
test
[
face_ids
,
:]
n_pixels
=
data
.
shape
[
1
]
X_train
=
train
[:,
:
(
n_pixels
+
1
)
//
2
]
y_train
=
train
[:,
n_pixels
//
2
:]
X_test
=
test
[:,
:
(
n_pixels
+
1
)
//
2
]
y_test
=
test
[:,
n_pixels
//
2
:]
ESTIMATORS
=
{
"Extra trees"
:
(
n_estimators
=
10
,
max_features
=
32
,
random_state
=
0
),
"K-nn"
:
(),
"Linear regression"
:
(),
"Ridge"
:
(),
}
y_test_predict
=
dict
()
for
name
,
estimator
in
ESTIMATORS
.
items
():
estimator
.
fit
(
X_train
,
y_train
)
y_test_predict
[
name
]
=
estimator
.
predict
(
X_test
)
image_shape
=
(
64
,
64
)
n_cols
=
1
+
len
(
ESTIMATORS
)
(
figsize
=
(
2.0
*
n_cols
,
2.26
*
n_faces
))
(
"Face completion with multi-output estimators"
,
size
=
16
)
for
i
in
range
(
n_faces
):
true_face
=
((
X_test
[
i
],
y_test
[
i
]))
if
i
:
sub
=
(
n_faces
,
n_cols
,
i
*
n_cols
+
1
)
else
:
sub
=
(
n_faces
,
n_cols
,
i
*
n_cols
+
1
,
title
=
"true faces"
)
sub
.
axis
(
"off"
)
sub
.
imshow
(
true_face
.
reshape
(
image_shape
),
cmap
=
plt
.
cm
.
gray
,
interpolation
=
"nearest"
)
for
j
,
est
in
enumerate
(
sorted
(
ESTIMATORS
)):
completed_face
=
((
X_test
[
i
],
y_test_predict
[
est
][
i
]))
if
i
:
sub
=
(
n_faces
,
n_cols
,
i
*
n_cols
+
2
+
j
)
else
:
sub
=
(
n_faces
,
n_cols
,
i
*
n_cols
+
2
+
j
,
title
=
est
)
sub
.
axis
(
"off"
)
sub
.
imshow
(
completed_face
.
reshape
(
image_shape
),
cmap
=
plt
.
cm
.
gray
,
interpolation
=
"nearest"
,
)
()
Total running time of the script: (0 minutes 1.645 seconds)
Related examples