模型定型之后,我们需要创建一个方法(Evaluate)来评测该模型的质量,根据你自己的标准测试数据集与该模型的符合程度来判断,并且输出相应的指标,该指标参数根据你所调用的评估方法返回具体的根据你的算法方程返回相应的方程的参数 。代码如下所示:
public static void Evaluate(MLContext mlContext, ITransformer model)
{
var dataView = _textLoader.Read(_testDataPath);
Console.WriteLine("===============用测试数据评估模型的准确性===============");
var predictions = model.Transform(dataView);
//评测定型模型的质量
var metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
Console.WriteLine();
Console.WriteLine("模型质量量度评估");
Console.WriteLine("--------------------------------");
Console.WriteLine($"精度: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.Auc:P2}");
Console.WriteLine("=============== 模型结束评价 ===============");
Console.ReadLine();
//评测完成之后开始保存定型的模型
SaveModelAsFile(mlContext, model);
}
定义单个数据的预测方法(Predict)与批处理预测的方法(PredictWithModelLoadedFromFile):
单个数据集的预测代码如下所示:
private static void Predict(MLContext mlContext, ITransformer model)
{
//创建包装器
var predictionFunction = model.CreatePredictionEngine<SentimentData, SentimentPrediction>(mlContext);
SentimentData sampleStatement = new SentimentData
{
SentimentText = "爱车新需求开发;麦扣日志监控部分页面数据绑定;"
};
//预测结果
var resultprediction = predictionFunction.Predict(sampleStatement);
Console.WriteLine();
Console.WriteLine("===============单个测试数据预测 ===============");
Console.WriteLine();
Console.WriteLine($"日报内容: {sampleStatement.SentimentText} | 是否合格: {(Convert.ToBoolean(resultprediction.Prediction) ? "合格" : "不合格")} | 符合率: {resultprediction.Probability} ");
Console.WriteLine("=============== 预测结束 ===============");
Console.WriteLine();
Console.ReadLine();
}
批处理数据集预测方法代码如下所示:










