Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
licenseplates
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
licenseplates
Commits
3158862e
Commit
3158862e
authored
Dec 09, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added script to find SVM parameters.
parent
57cdc26e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
2 deletions
+38
-2
src/Classifier.py
src/Classifier.py
+17
-2
src/find_svm_params.py
src/find_svm_params.py
+21
-0
No files found.
src/Classifier.py
View file @
3158862e
...
...
@@ -3,14 +3,17 @@ from svmutil import svm_train, svm_problem, svm_parameter, svm_predict, \
class
Classifier
:
def
__init__
(
self
,
c
=
None
,
filename
=
None
):
def
__init__
(
self
,
c
=
None
,
gamma
=
None
,
filename
=
None
):
if
filename
:
# If a filename is given, load a model from the given filename
self
.
model
=
svm_load_model
(
filename
)
elif
c
==
None
or
gamma
==
None
:
raise
Exception
(
'Please specify both C and gamma.'
)
else
:
self
.
param
=
svm_parameter
()
self
.
param
.
kernel_type
=
2
# Radial kernel type
self
.
param
.
C
=
c
self
.
param
.
C
=
c
# Soft margin
self
.
param
.
gamma
=
gamma
# Parameter for radial kernel
self
.
model
=
None
def
save
(
self
,
filename
):
...
...
@@ -33,6 +36,18 @@ class Classifier:
problem
=
svm_problem
(
classes
,
features
)
self
.
model
=
svm_train
(
problem
,
self
.
param
)
def
test
(
self
,
test_set
):
"""Test the classifier with the given test set and return the score."""
matches
=
0
for
char
in
test_set
:
prediction
=
self
.
classify
(
char
)
if
char
.
value
==
prediction
:
matches
+=
1
return
float
(
matches
)
/
len
(
test_set
)
def
classify
(
self
,
character
):
"""Classify a character object, return its value."""
predict
=
lambda
x
:
svm_predict
([
0
],
[
x
],
self
.
model
)[
0
][
0
]
...
...
src/find_svm_params.py
0 → 100644
View file @
3158862e
C
=
[
2
**
p
for
p
in
xrange
(
-
5
,
16
,
2
)]:
Y
=
[
2
**
p
for
p
in
xrange
(
-
15
,
4
,
2
)]
best_result
=
0
best_classifier
=
None
learning_set
=
load
(
file
(
'learning_set'
,
'r'
))
test_set
=
load
(
file
(
'test_set'
,
'r'
))
# Perform a grid-search on different combinations of soft margin and gamma
for
c
in
C
:
for
y
in
Y
:
classifier
=
Classifier
(
c
=
c
,
gamma
=
y
)
classifier
.
train
(
learning_set
)
result
=
classifier
.
test
(
test_set
)
if
result
>
best_result
:
best_classifier
=
classifier
print
'c = %f, gamma = %f, result = %d%%'
%
(
c
,
y
,
int
(
result
*
100
))
best_classifier
.
save
(
'best_classifier'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment