import
from
sklearn.inspection
import
DecisionBoundaryDisplay
from
sklearn.svm
import
X
,
y
=
(
n_samples
=
1_000
,
n_features
=
2
,
n_informative
=
2
,
n_redundant
=
0
,
n_clusters_per_class
=
1
,
class_sep
=
1.1
,
weights
=
[
0.9
,
0.1
],
random_state
=
0
,
)
rng
=
(
0
)
plot_indices
=
rng
.
choice
(
(
X
.
shape
[
0
]),
size
=
100
,
replace
=
True
)
X_plot
,
y_plot
=
X
[
plot_indices
],
y
[
plot_indices
]
def
plot_decision_function
(
classifier
,
sample_weight
,
axis
,
title
):
"""Plot the synthetic data and the classifier decision function. Points with
larger sample_weight are mapped to larger circles in the scatter plot."""
axis
.
scatter
(
X_plot
[:,
0
],
X_plot
[:,
1
],
c
=
y_plot
,
s
=
100
*
sample_weight
[
plot_indices
],
alpha
=
0.9
,
cmap
=
plt
.
cm
.
bone
,
edgecolors
=
"black"
,
)
(
classifier
,
X_plot
,
response_method
=
"decision_function"
,
alpha
=
0.75
,
ax
=
axis
,
cmap
=
plt
.
cm
.
bone
,
)
axis
.
axis
(
"off"
)
axis
.
set_title
(
title
)
sample_weight_constant
=
(
len
(
X
))
sample_weight_modified
=
abs
(
rng
.
randn
(
len
(
X
)))
positive_class_indices
=
(
y
==
1
)
.
nonzero
()[
0
]
sample_weight_modified
[
positive_class_indices
]
*=
15
clf_no_weights
=
(
gamma
=
1
)
clf_no_weights
.
fit
(
X
,
y
)
clf_weights
=
(
gamma
=
1
)
clf_weights
.
fit
(
X
,
y
,
sample_weight
=
sample_weight_modified
)
fig
,
axes
=
(
1
,
2
,
figsize
=
(
14
,
6
))
plot_decision_function
(
clf_no_weights
,
sample_weight_constant
,
axes
[
0
],
"Constant weights"
)
plot_decision_function
(
clf_weights
,
sample_weight_modified
,
axes
[
1
],
"Modified weights"
)
()
Total running time of the script: (0 minutes 0.317 seconds)
Related examples