教學課程:在 ML.NET 中使用二元分類來分析網站評論的情感Tutorial: Analyze sentiment of website comments with binary classification in ML.NET

本教學課程示範如何建立 .NET Core 主控台應用程式,來分類網站評論中的情感並採取適當動作。This tutorial shows you how to create a .NET Core console application that classifies sentiment from website comments and takes the appropriate action. 二元情感分類器在 Visual Studio 2017 中使用 C#。The binary sentiment classifier uses C# in Visual Studio 2017.

在本教學課程中,您將了解如何:In this tutorial, you learn how to:

  • 建立主控台應用程式Create a console application
  • 準備資料Prepare data
  • 載入資料Load the data
  • 建置和定型模型Build and train the model
  • 評估模型Evaluate the model
  • 使用模型來進行預測Use the model to make a prediction
  • 查看結果See the results

您可以在 dotnet/samples 存放庫中找到本教學課程的原始程式碼。You can find the source code for this tutorial at the dotnet/samples repository.

必要條件Prerequisites

建立主控台應用程式Create a console application

  1. 建立稱為 "SentimentAnalysis" 的 .NET Core 主控台應用程式Create a .NET Core Console Application called "SentimentAnalysis".

  2. 在專案中建立一個名為 Data 的目錄以儲存資料集檔案。Create a directory named Data in your project to save your data set files.

  3. 安裝「Microsoft.ML NuGet 套件」 :Install the Microsoft.ML NuGet Package:

    在 [方案總管] 中,於您的專案上按一下滑鼠右鍵,然後選取 [管理 NuGet 套件] 。In Solution Explorer, right-click on your project and select Manage NuGet Packages. 選擇 "nuget.org" 作為套件來源,然後選取 [瀏覽] 索引標籤。搜尋 Microsoft.ML,選取您想要的套件,然後選取 [安裝] 按鈕。Choose "nuget.org" as the package source, and then select the Browse tab. Search for Microsoft.ML, select the package you want, and then select the Install button. 同意您所選套件的授權條款,以繼續進行安裝。Proceed with the installation by agreeing to the license terms for the package you choose. Microsoft.ML.FastTree NuGet 套件執行相同的動作。Do the same for the Microsoft.ML.FastTree NuGet package.

準備您的資料Prepare your data

注意

此教學課程的資料集是來自 'From Group to Individual Labels using Deep Features' (從群組到使用深度特徵的個別標籤) (Kotzias 等人The datasets for this tutorial are from the 'From Group to Individual Labels using Deep Features', Kotzias et. al,al,. KDD 2015),而且裝載於「UCI Machine Learning Repository (UCI 機器學習存放庫)」- Dua, D. and Karra Taniskidou, E.(2017)。KDD 2015, and hosted at the UCI Machine Learning Repository - Dua, D. and Karra Taniskidou, E. (2017). 「UCI Machine Learning Repository (UCI 機器學習存放庫)」[http://archive.ics.uci.edu/ml ]。UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. 爾灣,加利福尼亞州:加州大學,資訊與電腦科學學院。Irvine, CA: University of California, School of Information and Computer Science.

  1. 下載並解壓縮 UCI 情感標記句子資料集 ZIP 檔案Download UCI Sentiment Labeled Sentences dataset ZIP file, and unzip.

  2. yelp_labelled.txt 檔案複製到您所建立的 Data 目錄。Copy the yelp_labelled.txt file into the Data directory you created.

  3. 在 [方案總管] 中,以滑鼠右鍵按一下 yelp_labeled.txt 檔案,並選取 [內容] 。In Solution Explorer, right-click the yelp_labeled.txt file and select Properties. 在 [進階] 底下,將 [複製到輸出目錄] 的值變更為 [有更新時才複製] 。Under Advanced, change the value of Copy to Output Directory to Copy if newer.

建立類別及定義路徑Create classes and define paths

  1. Program.cs 檔案頂端新增下列額外的 using 陳述式:Add the following additional using statements to the top of the Program.cs file:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Microsoft.ML;
    using Microsoft.ML.Data;
    using static Microsoft.ML.DataOperationsCatalog;
    using Microsoft.ML.Trainers;
    using Microsoft.ML.Transforms.Text;
    
  2. 建立兩個全域欄位,以保存最近所下載資料集檔案路徑與儲存的模型檔案路徑:Create two global fields to hold the recently downloaded dataset file path and the saved model file path:

    • _dataPath 包含用來將模型定型的資料集路徑。_dataPath has the path to the dataset used to train the model.
    • _modelPath 包含用來儲存定型模型的路徑。_modelPath has the path where the trained model is saved.
  3. 將下列程式碼新增至緊接在 Main 方法上方的一行,以指定路徑:Add the following code to the line right above the Main method to specify the paths:

    static readonly string _dataPath = Path.Combine(Environment.CurrentDirectory, "Data", "yelp_labelled.txt");
    
  4. 接下來,為輸入資料和預測建立類別。Next, create classes for your input data and predictions. 將新類別新增至專案:Add a new class to your project:

    • 在 [方案總管] 中,於專案上按一下滑鼠右鍵,然後選取 [新增] > [新增項目] 。In Solution Explorer, right-click the project, and then select Add > New Item.

    • 在 [新增項目] 對話方塊中,選取 [類別] ,然後將 [名稱] 欄位變更為 SentimentData.csIn the Add New Item dialog box, select Class and change the Name field to SentimentData.cs. 接著,選取 [新增] 按鈕。Then, select the Add button.

  5. SentimentData.cs 檔案隨即在程式碼編輯器中開啟。The SentimentData.cs file opens in the code editor. 將以下 using 陳述式新增至 SentimentData.cs 頂端:Add the following using statement to the top of SentimentData.cs:

    using Microsoft.ML.Data;
    
  6. 移除現有的類別定義,然後將下列程式碼 (具有 SentimentDataSentimentPrediction 這兩個類別) 新增至 SentimentData.cs 檔案:Remove the existing class definition and add the following code, which has two classes SentimentData and SentimentPrediction, to the SentimentData.cs file:

    public class SentimentData
    {
        [LoadColumn(0)]
        public string SentimentText;
    
        [LoadColumn(1), ColumnName("Label")]
        public bool Sentiment;
    }
    
    public class SentimentPrediction : SentimentData
    {
    
        [ColumnName("PredictedLabel")]
        public bool Prediction { get; set; }
    
        public float Probability { get; set; }
    
        public float Score { get; set; }
    }
    

如何準備資料How the data was prepared

輸入資料集類別 SentimentData 具有使用者評論 (SentimentText) 的 string,以及代表情感的 bool (Sentiment) 值 1 (正面) 或 0 (負面)。The input dataset class, SentimentData, has a string for user comments (SentimentText) and a bool (Sentiment) value of either 1 (positive) or 0 (negative) for sentiment. 這兩個欄位都有 LoadColumn 附加屬性,其描述每個欄位的資料檔案順序。Both fields have LoadColumn attributes attached to them, which describes the data file order of each field. 此外,Sentiment 屬性具有 ColumnName 屬性來將它指定為 Label 欄位。In addition, the Sentiment property has a ColumnName attribute to designate it as the Label field. 下列範例檔案沒有標頭資料列,且看起來像這樣:The following example file doesn't have a header row, and looks like this:

SentimentTextSentimentText 情感 (標籤)Sentiment (Label)
女服務生的服務速度有點慢。Waitress was a little slow in service. 00
不夠酥脆。Crust is not good. 00
哇...喜歡這個地方。Wow... Loved this place. 11
服務很迅速。Service was very prompt. 11

SentimentPrediction 是在模型定型後所使用的預測類別。SentimentPrediction is the prediction class used after model training. 它繼承自 SentimentData,以便輸入 SentimentText 可以和輸出預測一起顯示。It inherits from SentimentData so that the input SentimentText can be displayed along with the output prediction. Prediction 布林值是在提供新輸入 SentimentText 時,模型預測的值。The Prediction boolean is the value that the model predicts when supplied with new input SentimentText.

輸出類別 SentimentPrediction 包含模型計算的兩個其他屬性:Score - 模型計算的原始分數,和 Probability - 針對文字具有正面情感之可能性所校正的分數。The output class SentimentPrediction contains two other properties calculated by the model: Score - the raw score calculated by the model, and Probability - the score calibrated to the likelihood of the text having positive sentiment.

本教學課程中最重要的屬性是 PredictionFor this tutorial, the most important property is Prediction.

載入資料Load the data

ML.NET 中的資料以 IDataView 類別 表示。Data in ML.NET is represented as an IDataView class. IDataView 是彈性且有效率的表格式資料描述方式 (數值和文字)。IDataView is a flexible, efficient way of describing tabular data (numeric and text). 資料可以從文字或即時 (例如 SQL 資料庫或記錄檔) 載入至 IDataView 物件。Data can be loaded from a text file or in real time (for example, SQL database or log files) to an IDataView object.

MLContext 類別是所有 ML.NET 作業的起點。The MLContext class is a starting point for all ML.NET operations. mlContext 初始化會建立新的 ML.NET 環境,可在模型建立工作流程物件間共用。Initializing mlContext creates a new ML.NET environment that can be shared across the model creation workflow objects. 就概念而言,類似於 Entity Framework 中的 DBContextIt's similar, conceptually, to DBContext in Entity Framework.

您可以準備資料,然後載入資料:You prepare the app, and then load data:

  1. Main 方法中的 Console.WriteLine("Hello World!") 程式碼行取代為下列程式碼,以宣告 mlContext 變數並將它初始化:Replace the Console.WriteLine("Hello World!") line in the Main method with the following code to declare and initialize the mlContext variable:

    MLContext mlContext = new MLContext();
    
  2. 將下列程式碼加入為 Main() 方法中的下一行程式碼:Add the following as the next line of code in the Main() method:

    TrainTestData splitDataView = LoadData(mlContext);
    
  3. 請使用下列程式碼,在緊接著 Main() 方法之後,建立 LoadData() 方法:Create the LoadData() method, just after the Main() method, using the following code:

    public static TrainTestData LoadData(MLContext mlContext)
    {
    
    }
    

    LoadData() 方法會執行下列工作:The LoadData() method executes the following tasks:

    • 載入資料。Loads the data.
    • 將載入的資料集分割為定型與測試資料集。Splits the loaded dataset into train and test datasets.
    • 傳回分割的定型與測試資料集。Returns the split train and test datasets.
  4. 將下列程式碼新增為 LoadData() 方法的第一行:Add the following code as the first line of the LoadData() method:

    IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath, hasHeader: false);
    

    LoadFromTextFile() 會定義資料結構描述並讀入檔案中。The LoadFromTextFile() defines the data schema and reads in the file. 會接受資料路徑變數然後傳回 IDataViewIt takes in the data path variables and returns an IDataView.

分割資料集以進行模型定型與測試Split the dataset for model training and testing

準備模型時,您可以使用部分資料集來定型它,並使用部分資料集來測試模型的準確度。When preparing a model, you use part of the dataset to train it and part of the dataset to test the model's accuracy.

  1. 若要將載入的資料分割成所需的資料集,請將下列程式碼加入為 LoadData() 方法中的下一行:To split the loaded data into the needed datasets, add the following code as the next line in the LoadData() method:

    TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
    

    上述程式碼使用 TrainTestSplit() 方法將載入的資料集分割為定型與測試資料集,然後將它們包含在 TrainTestData 類別中並傳回。The previous code uses the TrainTestSplit() method to split the loaded dataset into train and test datasets and return them in the TrainTestData class. 您可以使用 testFraction 參數來指定資料的測試集百分比。Specify the test set percentage of data with the testFractionparameter. 預設值是 10%,但您在本例中會使用 20% 以評估更多資料。The default is 10%, in this case you use 20% to evaluate more data.

  2. LoadData() 方法的結尾傳回 splitDataViewReturn the splitDataView at the end of the LoadData() method:

    return splitDataView;
    

建置和定型模型Build and train the model

  1. 將下列呼叫新增至 BuildAndTrainModel 方法作為 Main() 方法中的下一行程式碼:Add the following call to the BuildAndTrainModelmethod as the next line of code in the Main() method:

    ITransformer model = BuildAndTrainModel(mlContext, splitDataView.TrainSet);
    

    BuildAndTrainModel() 方法會執行下列工作:The BuildAndTrainModel() method executes the following tasks:

    • 擷取並轉換資料。Extracts and transforms the data.
    • 將模型定型。Trains the model.
    • 根據測試資料預測情感。Predicts sentiment based on test data.
    • 傳回模型。Returns the model.
  2. 請使用下列程式碼,在緊接著 Main() 方法之後,建立 BuildAndTrainModel() 方法:Create the BuildAndTrainModel() method, just after the Main() method, using the following code:

    public static ITransformer BuildAndTrainModel(MLContext mlContext, IDataView splitTrainSet)
    {
    
    }
    

擷取並轉換資料Extract and transform the data

  1. 呼叫 FeaturizeText 作為下一行程式碼:Call FeaturizeText as the next line of code:

    var estimator = mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
    

    上述程式碼中的 FeaturizeText() 方法會將文字資料行 (SentimentText) 轉換成機器學習演算法所使用的數值索引鍵類型 Features 資料行,並將它新增為新的資料集資料行:The FeaturizeText() method in the previous code converts the text column (SentimentText) into a numeric key type Features column used by the machine learning algorithm and adds it as a new dataset column:

    SentimentTextSentimentText 情感Sentiment 功能Features
    女服務生的服務速度有點慢。Waitress was a little slow in service. 00 [0.76, 0.65, 0.44, …][0.76, 0.65, 0.44, …]
    不夠酥脆。Crust is not good. 00 [0.98, 0.43, 0.54, …][0.98, 0.43, 0.54, …]
    哇...喜歡這個地方。Wow... Loved this place. 11 [0.35, 0.73, 0.46, …][0.35, 0.73, 0.46, …]
    服務很迅速。Service was very prompt. 11 [0.39, 0, 0.75, …][0.39, 0, 0.75, …]

新增學習演算法Add a learning algorithm

此應用程式使用分類演算法來分類多個項目或資料列。This app uses a classification algorithm that categorizes items or rows of data. 應用程式會將網站評論分類為正面或負面,因此請使用二元分類工作。The app categorizes website comments as either positive or negative, so use the binary classification task.

透過將下列內容新增為 BuildAndTrainModel() 中的下一行程式碼,將機器學習工作附加到資料轉換定義:Append the machine learning task to the data transformation definitions by adding the following as the next line of code in BuildAndTrainModel():

.Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features"));

SdcaLogisticRegressionBinaryTrainer 是您的分類定型演算法。The SdcaLogisticRegressionBinaryTrainer is your classification training algorithm. 這會附加到 estimator,並接受特徵化 SentimentText (Features) 和 Label 輸入參數,以從歷史資料學習。This is appended to the estimator and accepts the featurized SentimentText (Features) and the Label input parameters to learn from the historic data.

將模型定型Train the model

將下列內容新增為 BuildAndTrainModel() 方法中的下一行程式碼,調整模型為合適於 splitTrainSet 資料並傳回已定型模型:Fit the model to the splitTrainSet data and return the trained model by adding the following as the next line of code in the BuildAndTrainModel() method:

Console.WriteLine("=============== Create and Train the Model ===============");
var model = estimator.Fit(splitTrainSet);
Console.WriteLine("=============== End of training ===============");
Console.WriteLine();

Fit() 方法會透過轉換資料集和套用定型來定型模型。The Fit() method trains your model by transforming the dataset and applying the training.

傳回經訓練以供評估使用的模型Return the model trained to use for evaluation

BuildAndTrainModel() 方法的結尾傳回模型:Return the model at the end of the BuildAndTrainModel() method:

return model;

評估模型Evaluate the model

定型模型之後,使用您的測試資料來驗證模型效能。After your model is trained, use your test data validate the model's performance.

  1. BuildAndTrainModel() 之後,使用下列程式碼建立 Evaluate() 方法:Create the Evaluate() method, just after BuildAndTrainModel(), with the following code:

    public static void Evaluate(MLContext mlContext, ITransformer model, IDataView splitTestSet)
    {
    
    }
    

    Evaluate() 方法會執行下列工作:The Evaluate() method executes the following tasks:

    • 載入測試資料集。Loads the test dataset.
    • 建立 BinaryClassification 評估工具。Creates the BinaryClassification evaluator.
    • 評估模型並建立計量。Evaluates the model and creates metrics.
    • 顯示計量。Displays the metrics.
  2. 請使用下列程式碼,在緊接著 BuildAndTrainModel() 方法呼叫底下,從 Main() 方法新增對新方法的呼叫:Add a call to the new method from the Main() method, right under the BuildAndTrainModel() method call, using the following code:

    Evaluate(mlContext, model, splitDataView.TestSet);
    
  3. 將下列程式碼新增至 Evaluate() 以轉換 splitTestSet 資料:Transform the splitTestSet data by adding the following code to Evaluate():

    Console.WriteLine("=============== Evaluating Model accuracy with Test data===============");
    IDataView predictions = model.Transform(splitTestSet);
    

    上述程式碼使用 Transform() 方法對多個提供的測試資料集輸入資料列進行預測。The previous code uses the Transform() method to make predictions for multiple provided input rows of a test dataset.

  4. 將下列內容新增為 Evaluate() 方法中的下一行程式碼來評估模型:Evaluate the model by adding the following as the next line of code in the Evaluate() method:

    CalibratedBinaryClassificationMetrics metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");
    

在您設定好預測 (predictions) 後,Evaluate() 方法會評估模型,將預測值與測試資料集中的實際 Labels 進行比較,並傳回與模型執行情況相關的 CalibratedBinaryClassificationMetrics 物件。Once you have the prediction set (predictions), the Evaluate() method assesses the model, which compares the predicted values with the actual Labels in the test dataset and returns a CalibratedBinaryClassificationMetrics object on how the model is performing.

顯示模型驗證的計量Displaying the metrics for model validation

使用下列程式碼來顯示計量:Use the following code to display the metrics:

Console.WriteLine();
Console.WriteLine("Model quality metrics evaluation");
Console.WriteLine("--------------------------------");
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"Auc: {metrics.AreaUnderRocCurve:P2}");
Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
Console.WriteLine("=============== End of model evaluation ===============");
  • Accuracy 計量會取得模型的準確度,這是資料集中正確預測的比例。The Accuracy metric gets the accuracy of a model, which is the proportion of correct predictions in the test set.

  • AreaUnderRocCurve 計量會指出模型正確分類正面和負面類別的自信程度。The AreaUnderRocCurve metric indicates how confident the model is correctly classifying the positive and negative classes. 您會希望 AreaUnderRocCurve 盡可能接近 1。You want the AreaUnderRocCurve to be as close to one as possible.

  • F1Score 計量會取得模型的 F1 分數,這是精確度重新叫用率之間的平衡量值。The F1Score metric gets the model's F1 score, which is a measure of balance between precision and recall. 您會希望 F1Score 盡可能接近 1。You want the F1Score to be as close to one as possible.

預測測試資料結果Predict the test data outcome

  1. 請使用下列程式碼,在緊接著 Evaluate() 方法之後,建立 UseModelWithSingleItem() 方法:Create the UseModelWithSingleItem() method, just after the Evaluate() method, using the following code:

    private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
    {
    
    }
    

    UseModelWithSingleItem() 方法會執行下列工作:The UseModelWithSingleItem() method executes the following tasks:

    • 建立單一評論的測試資料。Creates a single comment of test data.
    • 根據測試資料預測情感。Predicts sentiment based on test data.
    • 合併測試資料和預測來進行報告。Combines test data and predictions for reporting.
    • 顯示預測的結果。Displays the predicted results.
  2. 請使用下列程式碼,在緊接著 Evaluate() 方法呼叫底下,從 Main() 方法新增對新方法的呼叫:Add a call to the new method from the Main() method, right under the Evaluate() method call, using the following code:

    UseModelWithSingleItem(mlContext, model);
    
  3. 新增下列程式碼,建立作為 UseModelWithSingleItem() 方法中的第一行:Add the following code to create as the first line in the UseModelWithSingleItem() Method:

    PredictionEngine<SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);
    

    PredictionEngine 是一種便利的 API,可讓您傳遞並在單一資料執行個體上接著執行預測。The PredictionEngine is a convenience API, which allows you to pass in and then perform a prediction on a single instance of data.

  4. 透過建立 SentimentData 的執行個體,在 UseModelWithSingleItem() 方法中新增評論,以測試定型模型的預測:Add a comment to test the trained model's prediction in the UseModelWithSingleItem() method by creating an instance of SentimentData:

    SentimentData sampleStatement = new SentimentData
    {
        SentimentText = "This was a very bad steak"
    };
    
  5. 透過將下列內容新增為 Prediction Engine 方法中的後續幾行程式碼,將測試評論資料傳遞到 UseModelWithSingleItem()Pass the test comment data to the Prediction Engine by adding the following as the next lines of code in the UseModelWithSingleItem() method:

    var resultPrediction = predictionFunction.Predict(sampleStatement);
    

    Predict() 函式會對單一資料列進行預測。The Predict() function makes a prediction on a single row of data.

  6. 使用下列程式碼來顯示 SentimentText 和對應的情感預測:Display SentimentText and corresponding sentiment prediction using the following code:

    Console.WriteLine();
    Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");
    
    Console.WriteLine();
    Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
    
    Console.WriteLine("=============== End of Predictions ===============");
    Console.WriteLine();
    

使用模型來進行預測Use the model for prediction

部署並預測批次項目Deploy and predict batch items

  1. 請使用下列程式碼,在緊接著 UseModelWithSingleItem() 方法之後,建立 UseModelWithBatchItems() 方法:Create the UseModelWithBatchItems() method, just after the UseModelWithSingleItem() method, using the following code:

    public static void UseModelWithBatchItems(MLContext mlContext, ITransformer model)
    {
    
    }
    

    UseModelWithBatchItems() 方法會執行下列工作:The UseModelWithBatchItems() method executes the following tasks:

    • 建立批次測試資料。Creates batch test data.
    • 根據測試資料預測情感。Predicts sentiment based on test data.
    • 合併測試資料和預測來進行報告。Combines test data and predictions for reporting.
    • 顯示預測的結果。Displays the predicted results.
  2. 請使用下列程式碼,在緊接著 UseModelWithSingleItem() 方法呼叫底下,從 Main 方法新增對新方法的呼叫:Add a call to the new method from the Main method, right under the UseModelWithSingleItem() method call, using the following code:

    UseModelWithBatchItems(mlContext, model);
    
  3. 新增一些評論以測試 UseModelWithBatchItems() 方法中定型模型的預測:Add some comments to test the trained model's predictions in the UseModelWithBatchItems() method:

    IEnumerable<SentimentData> sentiments = new[]
    {
        new SentimentData
        {
            SentimentText = "This was a horrible meal"
        },
        new SentimentData
        {
            SentimentText = "I love this spaghetti."
        }
    };
    

預測評論情感Predict comment sentiment

利用 Transform() 方法,使用該模型來預測評論資料情感:Use the model to predict the comment data sentiment using the Transform() method:

IDataView batchComments = mlContext.Data.LoadFromEnumerable(sentiments);

IDataView predictions = model.Transform(batchComments);

// Use model to predict whether comment data is Positive (1) or Negative (0).
IEnumerable<SentimentPrediction> predictedResults = mlContext.Data.CreateEnumerable<SentimentPrediction>(predictions, reuseRowObject: false);

合併並顯示預測Combine and display the predictions

使用下列程式碼來為預測建立標頭:Create a header for the predictions using the following code:

Console.WriteLine();

Console.WriteLine("=============== Prediction Test of loaded model with multiple samples ===============");

因為 SentimentPrediction 繼承自 SentimentData,所以 Transform() 方法會使用預測欄位填入 SentimentTextBecause SentimentPrediction is inherited from SentimentData, the Transform() method populated SentimentText with the predicted fields. 隨著 ML.NET 處理的進行,每個元件都會新增資料行,使其易於顯示結果:As the ML.NET process processes, each component adds columns, and this makes it easy to display the results:

foreach (SentimentPrediction prediction  in predictedResults)
{
    Console.WriteLine($"Sentiment: {prediction.SentimentText} | Prediction: {(Convert.ToBoolean(prediction.Prediction) ? "Positive" : "Negative")} | Probability: {prediction.Probability} ");

}
Console.WriteLine("=============== End of predictions ===============");

結果Results

您的結果應該與以下類似。Your results should be similar to the following. 處理期間會顯示訊息。During processing, messages are displayed. 您可能會看到警告或處理訊息。You may see warnings, or processing messages. 為了清晰起見,下列結果中已將這些都移除。These have been removed from the following results for clarity.

Model quality metrics evaluation
--------------------------------
Accuracy: 83.96%
Auc: 90.51%
F1Score: 84.04%

=============== End of model evaluation ===============

=============== Prediction Test of model with a single sample and test dataset ===============

Sentiment: This was a very bad steak | Prediction: Negative | Probability: 0.1027377
=============== End of Predictions ===============

=============== Prediction Test of loaded model with a multiple samples ===============

Sentiment: This was a horrible meal | Prediction: Negative | Probability: 0.1369192
Sentiment: I love this spaghetti. | Prediction: Positive | Probability: 0.9960636
=============== End of predictions ===============

=============== End of process ===============
Press any key to continue . . .

恭喜您!Congratulations! 您現在已成功建置可對訊息情感進行分類和預測的機器學習模型。You've now successfully built a machine learning model for classifying and predicting messages sentiment.

建立成功的模型是一個需要反覆嘗試的程序。Building successful models is an iterative process. 此模型一開始的品質較低,因為此教學課程是使用小型的資料集來提供快速的模型定型。This model has initial lower quality as the tutorial uses small datasets to provide quick model training. 如果您對於模型的品質感到不滿意,可以嘗試為它提供較大的定型資料集,或選擇不同的定型演算法,並針對每個演算法搭配不同超參數來改善它。If you aren't satisfied with the model quality, you can try to improve it by providing larger training datasets or by choosing different training algorithms with different hyper-parameters for each algorithm.

您可以在 dotnet/samples 存放庫中找到本教學課程的原始程式碼。You can find the source code for this tutorial at the dotnet/samples repository.

後續步驟Next steps

在本教學課程中,您將了解如何:In this tutorial, you learned how to:

  • 建立主控台應用程式Create a console application
  • 準備資料Prepare data
  • 載入資料Load the data
  • 建置和定型模型Build and train the model
  • 評估模型Evaluate the model
  • 使用模型來進行預測Use the model to make a prediction
  • 查看結果See the results

前進到下一個教學課程來深入了解Advance to the next tutorial to learn more