forked from shilad/PyVowpal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_examples.py
85 lines (76 loc) · 3.3 KB
/
test_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from vowpal import *
"""
Examples and test functions for py-vowpal
"""
# fake input data about running speed
DATA = [
[0.4, {'body' : {'height' : 0.8, 'weight' : 0.3}, 'age' : {'age' : 0.4}, 'sports' : { 'football' : None }}],
[0.7, {'body' : {'height' : 0.8, 'weight' : 0.3}, 'age' : {'age' : 0.3}, 'sports' : { 'soccer' : None }}],
[0.2, {'body' : {'height' : 0.8, 'weight' : 0.5}, 'age' : {'age' : 0.7}, 'sports' : { 'soccer' : None }}],
[0.7, {'body' : {'height' : 0.9, 'weight' : 0.3}, 'age' : {'age' : 0.2}, 'sports' : { 'track' : None }}],
[0.9, {'body' : {'height' : 0.7, 'weight' : 0.3}, 'age' : {'age' : 0.3}, 'sports' : { 'track' : None }}],
[0.6, {'body' : {'height' : 0.7, 'weight' : 0.7}, 'age' : {'age' : 0.2}, 'sports' : { 'track' : None }}],
[None, {'body' : {'height' : 0.7, 'weight' : 0.2}, 'age' : {'age' : 0.3}, 'sports' : { 'track' : None }}],
[None, {'body' : {'height' : 0.7, 'weight' : 0.2}, 'age' : {'age' : 0.3}, 'sports' : { 'soccer' : None }}],
]
PATH_VW = '/Users/shilad/Downloads/JohnLangford-vowpal_wabbit-9a1da62/vw'
def test_predict_from_examples():
"""
Predicting directly from VowpalExample objects.
Easiest, but requires that the input data fit in memory.
"""
examples = []
for i in xrange(len(DATA)):
(value, all_sections) = DATA[i]
ex = VowpalExample(i, value)
for (namespace, section) in all_sections.items():
ex.add_section(namespace, section)
examples.append(ex)
train = examples[:-2]
test = examples[-2:]
vw = Vowpal(PATH_VW, './vw.%s', {'--passes' : '10' })
preds = vw.predict_from_examples(train, test)
for (id, value) in preds:
print 'prediction for %s is %s' % (id, value)
def test_predict_from_example_stream():
"""
Predicting from an ExampleStream. An ExampleStream basically
writes an input file for you from VowpalExample objects.
All training examples (value != None) must appear before test examples.
"""
stream = ExampleStream('vw.stream.txt')
examples = []
for i in xrange(len(DATA)):
(value, all_sections) = DATA[i]
ex = VowpalExample(i, value)
for (namespace, section) in all_sections.items():
ex.add_section(namespace, section)
stream.add_example(ex)
train = examples[:-2]
test = examples[-2:]
vw = Vowpal(PATH_VW, './vw.%s', {'--passes' : '10' })
preds = vw.predict_from_example_stream(stream)
for (id, value) in preds:
print 'prediction for %s is %s' % (id, value)
def test_predict_from_file():
"""
Predicting directly from a file.
All training examples (value != None) must appear before test examples.
"""
f = open('vw.file.txt', 'w')
examples = []
for i in xrange(len(DATA)):
(value, all_sections) = DATA[i]
ex = VowpalExample(i, value)
for (namespace, section) in all_sections.items():
ex.add_section(namespace, section)
f.write(str(ex) + '\n')
f.close()
vw = Vowpal(PATH_VW, './vw.%s', {'--passes' : '10' })
preds = vw.predict_from_file('vw.file.txt')
for (id, value) in preds:
print 'prediction for %s is %s' % (id, value)
if __name__ == '__main__':
test_predict_from_examples()
test_predict_from_example_stream()
test_predict_from_file()