Skip to content

Commit d1a350f

Browse files
Ivanidzo4kaTomFinley
authored andcommitted
add append function to pipeline (#284)
Add `Append` function to pipeline for more fluent API than that allowed by `Add`
1 parent 28c0709 commit d1a350f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Microsoft.ML/LearningPipeline.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public LearningPipeline()
6666
/// </summary>
6767
/// <param name="seed">Specify seed for random generator</param>
6868
/// <param name="conc">Specify concurrency factor (default value - autoselection)</param>
69-
internal LearningPipeline(int? seed=null, int conc=0)
69+
internal LearningPipeline(int? seed = null, int conc = 0)
7070
{
7171
_seed = seed;
7272
_conc = conc;
@@ -109,6 +109,16 @@ internal LearningPipeline(int? seed=null, int conc=0)
109109
/// <param name="item">Any ML component (data loader, transform or trainer) defined as <see cref="ILearningPipelineItem"/>.</param>
110110
public void Add(ILearningPipelineItem item) => Items.Add(item);
111111

112+
/// <summary>
113+
/// Add a data loader, transform or trainer into the pipeline.
114+
/// </summary>
115+
/// <param name="item">Any ML component (data loader, transform or trainer) defined as <see cref="ILearningPipelineItem"/>.</param>
116+
/// <returns>Pipeline with added item</returns>
117+
public LearningPipeline Append(ILearningPipelineItem item)
118+
{
119+
Add(item);
120+
return this;
121+
}
112122
/// <summary>
113123
/// Remove all the loaders/transforms/trainers from the pipeline.
114124
/// </summary>
@@ -152,7 +162,7 @@ public PredictionModel<TInput, TOutput> Train<TInput, TOutput>()
152162
where TInput : class
153163
where TOutput : class, new()
154164
{
155-
using (var environment = new TlcEnvironment(seed:_seed, conc:_conc))
165+
using (var environment = new TlcEnvironment(seed: _seed, conc: _conc))
156166
{
157167
Experiment experiment = environment.CreateExperiment();
158168
ILearningPipelineStep step = null;

test/Microsoft.ML.Tests/LearningPipelineTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,22 @@ public void NullableBooleanLabelPipeline()
167167
pipeline.Add(new FastForestBinaryClassifier());
168168
var model = pipeline.Train<Data, Prediction>();
169169
}
170+
171+
[Fact]
172+
public void AppendPipeline()
173+
{
174+
var pipeline = new LearningPipeline();
175+
pipeline.Append(new CategoricalOneHotVectorizer("String1", "String2"))
176+
.Append(new ColumnConcatenator(outputColumn: "Features", "String1", "String2", "Number1", "Number2"))
177+
.Append(new StochasticDualCoordinateAscentRegressor());
178+
Assert.NotNull(pipeline);
179+
Assert.Equal(3, pipeline.Count);
180+
181+
pipeline.Remove(pipeline.ElementAt(2));
182+
Assert.Equal(2, pipeline.Count);
183+
184+
pipeline.Append(new StochasticDualCoordinateAscentRegressor());
185+
Assert.Equal(3, pipeline.Count);
186+
}
170187
}
171188
}

test/Microsoft.ML.Tests/Scenarios/IrisPlantClassificationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void TrainAndPredictIrisModelTest()
1818
{
1919
string dataPath = GetDataPath("iris.txt");
2020

21-
var pipeline = new LearningPipeline();
21+
var pipeline = new LearningPipeline(seed:1, conc:1);
2222

2323
pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(useHeader: false));
2424
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",

0 commit comments

Comments
 (0)