-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TestFeatureExtraction.cs
121 lines (107 loc) · 4.32 KB
/
TestFeatureExtraction.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VW;
using VW.Serializer;
using VW.Serializer.Attributes;
namespace cs_unittest
{
[TestClass]
public class TestFeatureExtractionClass
{
public class Features
{
[Feature]
public float F1 { get; set; }
[Feature(FeatureGroup = 'l')]
public string Location { get; set; }
}
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestFeatureExtraction()
{
using (var vw = new VowpalWabbit<Features>("--noconstant"))
using (var serializer = vw.Serializer.Create(vw.Native))
using (var example = serializer.Serialize(new Features { F1 = 3.2f, Location = "New York" }))
{
var singleExample = example as VowpalWabbitSingleLineExampleCollection;
Assert.IsNotNull(singleExample);
foreach (var ns in singleExample.Example)
{
Console.WriteLine(ns.Index);
foreach (var feature in ns)
{
Console.WriteLine("{0}:{1}", feature.FeatureIndex, feature.X);
}
}
var namespaces = singleExample.Example.ToArray();
Assert.AreEqual((byte)' ', namespaces[0].Index);
CollectionAssert.AreEqual(
new[] {
new VowpalWabbitFeature(singleExample.Example, 3.2f, 610696),
},
namespaces[0].ToArray());
Assert.AreEqual((byte)'l', namespaces[1].Index);
CollectionAssert.AreEqual(
new[] {
new VowpalWabbitFeature(singleExample.Example, 1, 414696),
new VowpalWabbitFeature(singleExample.Example, 1, 380324),
},
namespaces[1].ToArray());
}
}
[TestMethod]
[TestCategory("Vowpal Wabbit")]
public void TestJsonFeatureExtraction()
{
string json = "{\"ns1\":{\"location\":\"New York\", \"f2\":3.4}}";
using (var vw = new VowpalWabbit("-b 3 --noconstant"))
using (var serializer = new VowpalWabbitJsonSerializer(vw))
using (var result = serializer.ParseAndCreate(json))
{
var singleExample = result as VowpalWabbitSingleLineExampleCollection;
Assert.IsNotNull(singleExample);
if (singleExample != null)
{
foreach (var ns in singleExample.Example)
{
Console.WriteLine(ns.Index);
foreach (var feature in ns)
{
Console.WriteLine("{0}:{1}", feature.FeatureIndex, feature.X);
}
}
var ns1 = singleExample.Example.ToArray();
Assert.AreEqual(1, ns1.Length);
Assert.AreEqual((byte)'n', ns1[0].Index);
CollectionAssert.AreEqual(
new[] {
new VowpalWabbitFeature(singleExample.Example, 1, 12),
new VowpalWabbitFeature(singleExample.Example, 3.4f, 28)
},
ns1[0].ToArray());
}
// for documentation purpose only
var multiExample = result as VowpalWabbitMultiLineExampleCollection;
Assert.IsNull(multiExample);
if (multiExample != null)
{
foreach (var example in multiExample.Examples)
{
foreach (var ns in example)
{
Console.WriteLine(ns.Index);
foreach (var feature in ns)
{
Console.WriteLine("{0}:{1}", feature.FeatureIndex, feature.X);
}
}
}
}
}
}
}
}