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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian Johnson
scikit-learn
Commits
4d2b3762
Commit
4d2b3762
authored
14 years ago
by
Alexandre Gramfort
Browse files
Options
Downloads
Patches
Plain Diff
pretifying plot_weighted_classes.py
parent
fccc2ec1
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/svm/plot_weighted_classes.py
+17
-11
17 additions, 11 deletions
examples/svm/plot_weighted_classes.py
with
17 additions
and
11 deletions
examples/svm/plot_weighted_classes.py
+
17
−
11
View file @
4d2b3762
...
@@ -3,7 +3,11 @@
...
@@ -3,7 +3,11 @@
SVM: Separating hyperplane with weighted classes
SVM: Separating hyperplane with weighted classes
================================================
================================================
Fit linear SVMs with and without class weighting.
Allows to handle problems with unbalanced classes.
"""
"""
print
__doc__
import
numpy
as
np
import
numpy
as
np
import
pylab
as
pl
import
pylab
as
pl
...
@@ -13,32 +17,34 @@ from scikits.learn import svm
...
@@ -13,32 +17,34 @@ from scikits.learn import svm
np
.
random
.
seed
(
0
)
np
.
random
.
seed
(
0
)
n_samples_1
=
1000
n_samples_1
=
1000
n_samples_2
=
100
n_samples_2
=
100
X
=
np
.
r_
[
1.5
*
np
.
random
.
randn
(
n_samples_1
,
2
),
0.5
*
np
.
random
.
randn
(
n_samples_2
,
2
)
+
[
2
,
2
]]
X
=
np
.
r_
[
1.5
*
np
.
random
.
randn
(
n_samples_1
,
2
),
Y
=
[
0
]
*
(
n_samples_1
)
+
[
1
]
*
(
n_samples_2
)
0.5
*
np
.
random
.
randn
(
n_samples_2
,
2
)
+
[
2
,
2
]]
y
=
[
0
]
*
(
n_samples_1
)
+
[
1
]
*
(
n_samples_2
)
# fit the model and get the separating hyperplane
# fit the model and get the separating hyperplane
clf
=
svm
.
SVC
(
kernel
=
'
linear
'
)
clf
=
svm
.
SVC
(
kernel
=
'
linear
'
)
clf
.
fit
(
X
,
Y
)
clf
.
fit
(
X
,
y
)
w
=
clf
.
coef_
[
0
]
w
=
clf
.
coef_
[
0
]
a
=
-
w
[
0
]
/
w
[
1
]
a
=
-
w
[
0
]
/
w
[
1
]
xx
=
np
.
linspace
(
-
5
,
5
)
xx
=
np
.
linspace
(
-
5
,
5
)
yy
=
a
*
xx
-
(
clf
.
intercept_
[
0
]
)
/
w
[
1
]
yy
=
a
*
xx
-
clf
.
intercept_
[
0
]
/
w
[
1
]
# get the separating hyperplane using weighted classes
# get the separating hyperplane using weighted classes
wclf
=
svm
.
SVC
(
kernel
=
'
linear
'
)
wclf
=
svm
.
SVC
(
kernel
=
'
linear
'
)
wclf
.
fit
(
X
,
Y
,
{
1
:
10
})
wclf
.
fit
(
X
,
y
,
class_weight
=
{
1
:
10
})
ww
=
wclf
.
coef_
[
0
]
ww
=
wclf
.
coef_
[
0
]
wa
=
-
ww
[
0
]
/
ww
[
1
]
wa
=
-
ww
[
0
]
/
ww
[
1
]
wyy
=
wa
*
xx
-
(
wclf
.
intercept_
[
0
]
)
/
ww
[
1
]
wyy
=
wa
*
xx
-
wclf
.
intercept_
[
0
]
/
ww
[
1
]
# plot separating hyperplanes and samples
# plot separating hyperplanes and samples
pl
.
set_cmap
(
pl
.
cm
.
Paired
)
pl
.
set_cmap
(
pl
.
cm
.
Paired
)
pl
.
plot
(
xx
,
yy
,
'
k-
'
)
h0
=
pl
.
plot
(
xx
,
yy
,
'
k-
'
)
pl
.
plot
(
xx
,
wyy
,
'
k--
'
)
h1
=
pl
.
plot
(
xx
,
wyy
,
'
k--
'
)
pl
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
Y
)
pl
.
scatter
(
X
[:,
0
],
X
[:,
1
],
c
=
y
)
pl
.
legend
((
h0
,
h1
),
(
'
no weights
'
,
'
with weights
'
))
pl
.
axis
(
'
tight
'
)
pl
.
axis
(
'
tight
'
)
pl
.
show
()
pl
.
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