-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathStringIndexerModelTests.cs
83 lines (74 loc) · 3.26 KB
/
StringIndexerModelTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Spark.ML.Feature;
using Microsoft.Spark.Sql;
using Microsoft.Spark.Sql.Types;
using Microsoft.Spark.UnitTest.TestUtils;
using Xunit;
namespace Microsoft.Spark.E2ETest.IpcTests.ML.Feature
{
[Collection("Spark E2E Tests")]
public class StringIndexerModelTests : FeatureBaseTests<StringIndexerModel>
{
private readonly SparkSession _spark;
public StringIndexerModelTests(SparkFixture fixture) : base(fixture)
{
_spark = fixture.Spark;
}
/// <summary>
/// Create a <see cref="DataFrame"/>, create a <see cref="StringIndexerModel"/> and test the
/// available methods.
/// </summary>
[Fact]
public void TestStringIndexerModel()
{
DataFrame input = _spark.CreateDataFrame(
new List<GenericRow>
{
new GenericRow(new object[] {0, "a"}),
new GenericRow(new object[] {1, "b"}),
new GenericRow(new object[] {2, "c"}),
new GenericRow(new object[] {3, "a"}),
new GenericRow(new object[] {4, "a"}),
new GenericRow(new object[] {5, "c"})
},
new StructType(new List<StructField>
{
new StructField("id", new IntegerType()),
new StructField("category", new StringType())
}));
string expectedUid = "theUid";
StringIndexer stringIndexer = new StringIndexer(expectedUid)
.SetInputCol("category")
.SetOutputCol("categoryIndex");
StringIndexerModel stringIndexerModel = stringIndexer.Fit(input);
DataFrame transformedDF = stringIndexerModel.Transform(input);
List<Row> observed = transformedDF.Select("category", new string[] { "categoryIndex" })
.Collect().ToList();
List<Row> expected = new List<Row>
{
new Row(new GenericRow(new object[] {"a", "0"})),
new Row(new GenericRow(new object[] {"b", "2"})),
new Row(new GenericRow(new object[] {"c", "1"})),
new Row(new GenericRow(new object[] {"a", "0"})),
new Row(new GenericRow(new object[] {"a", "0"})),
new Row(new GenericRow(new object[] {"c", "1"}))
};
Assert.Equal(expected, observed);
Assert.Equal("category", stringIndexer.GetInputCol());
Assert.Equal("categoryIndex", stringIndexer.GetOutputCol());
Assert.Equal(expectedUid, stringIndexer.Uid());
using (var tempDirectory = new TemporaryDirectory())
{
string savePath = Path.Join(tempDirectory.Path, "stringIndexerModel");
stringIndexerModel.Save(savePath);
StringIndexerModel loadedModel = StringIndexerModel.Load(savePath);
Assert.Equal(stringIndexerModel.Uid(), loadedModel.Uid());
}
}
}
}