ML.NET Sentiment Demo

    0

    0

    The code first creates a new ML context. From this context, it loads the training data and splits it into a training and test set. Next, it defines the model's training algorithm and trains the model on the training set. Finally, it uses the model to make predictions on the test set.

    using Microsoft.ML;
    using Microsoft.ML.Data;
    using Microsoft.ML.Trainers;
    
    namespace SentimentAnalysis
    {
      class Program
      {
        static void Main(string[] args)
        {
          // Create a new ML context
          var mlContext = new MLContext();
    
          // Load the training data
          var data = mlContext.Data.LoadFromTextFile<SentimentData>(
            "./sentiment.csv",
            hasHeader: true,
            separatorChar: ',');
    
          // Split the data into training and test sets
          var split = mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
    
          // Define the model's training algorithm
          var pipeline = mlContext.Transforms.Text.FeaturizeText(
              outputColumnName: "Features",
              inputColumnName: nameof(SentimentData.SentimentText))
            .Append(mlContext.BinaryClassification.Trainers.FastTree(
              labelColumnName: "Label",
              featureColumnName: "Features"));
    
          // Train the model
          var model = pipeline.Fit(split.TrainSet);
    
          // Use the model to make predictions on the test set
          var predictions = model.Transform(split.TestSet);
    
          // Evaluate the model's performance
          var metrics = mlContext.BinaryClassification.Evaluate(
            data: predictions,
            labelColumnName: "Label",
            scoreColumnName: "Score");
    
          Console.WriteLine("Accuracy: {0:P2}", metrics.Accuracy);
          Console.WriteLine("AUC: {0:P2}", metrics.AreaUnderRocCurve);
        }
      }
    
      // Define the data schema for the input data
      class SentimentData
      {
        [LoadColumn(0)]
        public bool Sentiment;
    
        [LoadColumn(1)]
        public string SentimentText;
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.