Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
4a8e882f
Commit
4a8e882f
authored
Jun 08, 2013
by
Davis King
Browse files
Added more comments and discussion of how to use sparse vectors
parent
4ef91280
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
1 deletion
+36
-1
python_examples/svm_rank.py
python_examples/svm_rank.py
+36
-1
No files found.
python_examples/svm_rank.py
View file @
4a8e882f
...
...
@@ -31,6 +31,8 @@ import dlib
# vectors and store them into a ranking_pair object like so:
data
=
dlib
.
ranking_pair
()
# Here we add two examples. In real applications, you would want lots of
# examples of relevant and non-relevant vectors.
data
.
relevant
.
append
(
dlib
.
vector
([
1
,
0
]))
data
.
nonrelevant
.
append
(
dlib
.
vector
([
0
,
1
]))
...
...
@@ -52,7 +54,7 @@ rank = trainer.train(data)
# score for non-relevant vectors.
print
"ranking score for a relevant vector: "
,
rank
(
data
.
relevant
[
0
])
print
"ranking score for a non-relevant vector: "
,
rank
(
data
.
nonrelevant
[
0
])
# The
se
output the following:
# The output
is
the following:
# ranking score for a relevant vector: 0.5
# ranking score for a non-relevant vector: -0.5
...
...
@@ -108,3 +110,36 @@ rank = trainer.train(queries)
# mean average precision.
print
"cross validation results: "
,
dlib
.
cross_validate_ranking_trainer
(
trainer
,
queries
,
4
)
# Finally, note that the ranking tools also support the use of sparse vectors in
# addition to dense vectors (which we used above). So if we wanted to do
# exactly what we did in the first part of the example program above but using
# sparse vectors we would do it like so:
data
=
dlib
.
sparse_ranking_pair
()
samp
=
dlib
.
sparse_vector
()
# Make samp represent the same vector as dlib.vector([1, 0]). In dlib, a sparse
# vector is just an array of pair objects. Each pair stores an index and a
# value. Moreover, the svm-ranking tools require sparse vectors to be sorted
# and to have unique indices. This means that the indices are listed in
# increasing order and no index value shows up more than once. If necessary,
# you can use the dlib.make_sparse_vector() routine to make a sparse vector
# object properly sorted and contain unique indices.
samp
.
append
(
dlib
.
pair
(
0
,
1
))
data
.
relevant
.
append
(
samp
)
# Mow make samp represent the same vector as dlib.vector([0, 1])
samp
.
clear
()
samp
.
append
(
dlib
.
pair
(
1
,
1
))
data
.
nonrelevant
.
append
(
samp
)
trainer
=
dlib
.
svm_rank_trainer_sparse
()
rank
=
trainer
.
train
(
data
)
print
"ranking score for a relevant vector: "
,
rank
(
data
.
relevant
[
0
])
print
"ranking score for a non-relevant vector: "
,
rank
(
data
.
nonrelevant
[
0
])
# Just as before, the output is the following:
# ranking score for a relevant vector: 0.5
# ranking score for a non-relevant vector: -0.5
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