ML.NET 教程 - 10 分钟入门
使用模型
ML.NET CLI 生成了经过训练的模型和代码,因此现在可以按照以下步骤在 .NET 应用程序(例如,SentimentModel
控制台应用)中使用该模型:
- 在命令行中,导航到
consumeModelApp
目录。Terminalcd SentimentModel
-
在任何代码编辑器中打开
Program.cs
然后检查代码。代码应如下所示:Program.csusing System; namespace SentimentModel.ConsoleApp { class Program { static void Main(string[] args) { // Add input data SentimentModel.ModelInput sampleData = new SentimentModel.ModelInput() { Col0 = @"Wow... Loved this place." }; // Make a single prediction on the sample data and print results var predictionResult = SentimentModel.Predict(sampleData); Console.WriteLine("Using model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data...\n\n"); Console.WriteLine($"Col0: @{"Wow... Loved this place."}"); Console.WriteLine($"Col1: {1F}"); Console.WriteLine($"\n\nPredicted Col1: {predictionResult.PredictedLabel}\n\n"); Console.WriteLine("=============== End of process, hit any key to finish ==============="); Console.ReadKey(); } } }
-
运行
SentimentModel.ConsoleApp
。可以通过在终端中运行以下命令来执行此操作(请确保你位于SentimentModel
中):Terminaldotnet run
输出应如下所示:
TerminalUsing model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data... Col0: Wow... Loved this place. Col1: 1 Class Score ----- ----- 1 0.9651076 0 0.034892436 =============== End of process, hit any key to finish ===============