Plot multi-class SGD on the iris dataset
sklearn.inspection
import
DecisionBoundaryDisplay
from
sklearn.linear_model
import
iris
=
()
X
=
iris
.
data
[:,
:
2
]
y
=
iris
.
target
colors
=
"byr"
idx
=
(
X
.
shape
[
0
])
(
13
)
(
idx
)
X
=
X
[
idx
]
y
=
y
[
idx
]
mean
=
X
.
mean
(
axis
=
0
)
std
=
X
.
std
(
axis
=
0
)
X
=
(
X
-
mean
)
/
std
clf
=
(
alpha
=
0.001
,
max_iter
=
100
)
.
fit
(
X
,
y
)
ax
=
()
(
clf
,
X
,
ax
=
ax
,
response_method
=
"predict"
,
xlabel
=
iris
.
feature_names
[
0
],
ylabel
=
iris
.
feature_names
[
1
],
)
(
"tight"
)
for
i
,
color
in
zip
(
clf
.
classes_
,
colors
):
idx
=
(
y
==
i
)
.
nonzero
()
(
X
[
idx
,
0
],
X
[
idx
,
1
],
c
=
color
,
label
=
iris
.
target_names
[
i
],
edgecolor
=
"black"
,
s
=
20
,
)
(
"Decision surface of multi-class SGD"
)
(
"tight"
)
xmin
,
xmax
=
()
ymin
,
ymax
=
()
coef
=
clf
.
coef_
intercept
=
clf
.
intercept_
def
plot_hyperplane
(
c
,
color
):
def
line
(
x0
):
return
(
-
(
x0
*
coef
[
c
,
0
])
-
intercept
[
c
])
/
coef
[
c
,
1
]
([
xmin
,
xmax
],
[
line
(
xmin
),
line
(
xmax
)],
ls
=
"--"
,
color
=
color
)
for
i
,
color
in
zip
(
clf
.
classes_
,
colors
):
plot_hyperplane
(
i
,
color
)
()
()
Total running time of the script: (0 minutes 0.117 seconds)
Related examples