_trainDataPath 具有用于定型模型的数据集路径。
_testDataPath 具有用于评估模型的数据集路径。
_modelPath 具有在其中保存定型模型的路径。
_textLoader 是用于加载和转换数据集的 TextLoader。

然后定义程序的入口(main函数)以及相应的处理方法:
定义SaveModelAsFile方法将模型保存为 .zip 文件代码如下所示:
private static void SaveModelAsFile(MLContext mlContext, ITransformer model)
{
using (var fs = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))
mlContext.Model.Save(model, fs);
Console.WriteLine("模型保存路径为{0}", _modelPath);
Console.ReadLine();
}
定义Train方法选择学习方法并且创建相应的学习管道,输出定型后的模型model代码如下所示:
public static ITransformer Train(MLContext mlContext, string dataPath)
{
IDataView dataView = _textLoader.Read(dataPath);
//数据特征化(按照管道所需的格式转换数据)
var pipeline = mlContext.Transforms.Text.FeaturizeText(inputColumnName: "SentimentText", outputColumnName: "Features")
//根据学习算法添加学习管道
.Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 20));
//得到模型
var model = pipeline.Fit(dataView);
Console.WriteLine();
//返回定型模型
return model;
}










