Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scikit-learn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian Johnson
scikit-learn
Commits
dd097e74
Commit
dd097e74
authored
11 years ago
by
Andreas Mueller
Browse files
Options
Downloads
Patches
Plain Diff
DOC improve svm sample weight example
parent
daa051ed
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/svm/plot_weighted_samples.py
+38
-16
38 additions, 16 deletions
examples/svm/plot_weighted_samples.py
with
38 additions
and
16 deletions
examples/svm/plot_weighted_samples.py
+
38
−
16
View file @
dd097e74
...
...
@@ -5,34 +5,56 @@ SVM: Weighted samples
Plot decision function of a weighted dataset, where the size of points
is proportional to its weight.
The sample weighting rescales the C parameter, which means that the classifier
puts more emphasis on getting these points right. The effect might often be
subtle.
"""
print
(
__doc__
)
import
numpy
as
np
import
pylab
as
pl
import
matplotlib.pyplot
as
pl
t
from
sklearn
import
svm
def
plot_decision_function
(
classifier
,
sample_weight
,
axis
,
title
):
# plot the decision function
xx
,
yy
=
np
.
meshgrid
(
np
.
linspace
(
-
4
,
5
,
500
),
np
.
linspace
(
-
4
,
5
,
500
))
Z
=
classifier
.
decision_function
(
np
.
c_
[
xx
.
ravel
(),
yy
.
ravel
()])
Z
=
Z
.
reshape
(
xx
.
shape
)
# plot the line, the points, and the nearest vectors to the plane
axis
.
contourf
(
xx
,
yy
,
Z
,
alpha
=
0.75
,
cmap
=
plt
.
cm
.
bone
)
axis
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
Y
,
s
=
100
*
sample_weight
,
alpha
=
0.9
,
cmap
=
plt
.
cm
.
bone
)
axis
.
axis
(
'
off
'
)
axis
.
set_title
(
title
)
# we create 20 points
np
.
random
.
seed
(
0
)
X
=
np
.
r_
[
np
.
random
.
randn
(
10
,
2
)
+
[
1
,
1
],
np
.
random
.
randn
(
10
,
2
)]
Y
=
[
1
]
*
10
+
[
-
1
]
*
10
sample_weight
=
100
*
np
.
abs
(
np
.
random
.
randn
(
20
))
# and assign a bigger weight to the last 10 samples
sample_weight
[:
10
]
*=
10
sample_weight_last_ten
=
abs
(
np
.
random
.
randn
(
len
(
X
)))
sample_weight_constant
=
np
.
ones
(
len
(
X
))
# and assign a bigger weight to the last 5 samples
sample_weight_last_ten
[
15
:]
*=
5
# # fit the model
clf
=
svm
.
SVC
()
clf
.
fit
(
X
,
Y
,
sample_weight
=
sample_weight
)
# for reference, first fit without class weights
# plot the decision function
xx
,
yy
=
np
.
meshgrid
(
np
.
linspace
(
-
4
,
5
,
500
),
np
.
linspace
(
-
4
,
5
,
500
))
# fit the model
clf_weights
=
svm
.
SVC
()
clf_weights
.
fit
(
X
,
Y
,
sample_weight
=
sample_weight_last_ten
)
Z
=
clf
.
decision_function
(
np
.
c_
[
xx
.
ravel
(),
yy
.
ravel
()]
)
Z
=
Z
.
reshape
(
xx
.
shape
)
clf_no_weights
=
svm
.
SVC
(
)
clf_no_weights
.
fit
(
X
,
Y
)
# plot the line, the points, and the nearest vectors to the plane
pl
.
contourf
(
xx
,
yy
,
Z
,
alpha
=
0.75
,
cmap
=
pl
.
cm
.
bone
)
pl
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
Y
,
s
=
sample_weight
,
alpha
=
0.9
,
cmap
=
pl
.
cm
.
bone
)
fig
,
axes
=
plt
.
subplots
(
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_last_ten
,
axes
[
1
],
"
Modified weights
"
)
pl
.
axis
(
'
off
'
)
pl
.
show
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment