plt
import
numpy
as
np
l1_color
=
"navy"
l2_color
=
"c"
elastic_net_color
=
"darkorange"
line
=
(
-
1.5
,
1.5
,
1001
)
xx
,
yy
=
(
line
,
line
)
l2
=
xx
**
2
+
yy
**
2
l1
=
np
.
abs
(
xx
)
+
np
.
abs
(
yy
)
rho
=
0.5
elastic_net
=
rho
*
l1
+
(
1
-
rho
)
*
l2
(
figsize
=
(
10
,
10
),
dpi
=
100
)
ax
=
()
elastic_net_contour
=
(
xx
,
yy
,
elastic_net
,
levels
=
[
1
],
colors
=
elastic_net_color
)
l2_contour
=
(
xx
,
yy
,
l2
,
levels
=
[
1
],
colors
=
l2_color
)
l1_contour
=
(
xx
,
yy
,
l1
,
levels
=
[
1
],
colors
=
l1_color
)
ax
.
set_aspect
(
"equal"
)
ax
.
spines
[
"left"
]
.
set_position
(
"center"
)
ax
.
spines
[
"right"
]
.
set_color
(
"none"
)
ax
.
spines
[
"bottom"
]
.
set_position
(
"center"
)
ax
.
spines
[
"top"
]
.
set_color
(
"none"
)
(
elastic_net_contour
,
inline
=
1
,
fontsize
=
18
,
fmt
=
{
1.0
:
"elastic-net"
},
manual
=
[(
-
1
,
-
1
)],
)
(
l2_contour
,
inline
=
1
,
fontsize
=
18
,
fmt
=
{
1.0
:
"L2"
},
manual
=
[(
-
1
,
-
1
)])
(
l1_contour
,
inline
=
1
,
fontsize
=
18
,
fmt
=
{
1.0
:
"L1"
},
manual
=
[(
-
1
,
-
1
)])
()
()
Total running time of the script: (0 minutes 0.245 seconds)
Related examples