Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.ML.Models;
- using Microsoft.ML.Runtime;
- using Microsoft.ML.Runtime.Api;
- using Microsoft.ML.Trainers;
- using Microsoft.ML.Transforms;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.ML;
- using Microsoft.ML.Data;
- using System.Threading.Tasks;
- namespace MLNETTest
- {
- public class SentimentData
- {
- [Column(ordinal: "0", name: "Label")]
- public float Sentiment;
- [Column(ordinal: "1")]
- public string SentimentText;
- }
- public class SentimentPrediction
- {
- [ColumnName("PredictedLabel")]
- public bool Sentiment;
- }
- class Program
- {
- const string _dataPath = @"wikipedia-detox-250-line-data.tsv";
- const string _testDataPath = @"wikipedia-detox-250-line-test.tsv";
- static async Task Main(string[] args)
- {
- var model = await TrainAsync();
- Evaluate(model);
- Predict(model);
- }
- public static async Task<PredictionModel<SentimentData, SentimentPrediction>> TrainAsync()
- {
- var pipeline = new LearningPipeline();
- pipeline.Add(new TextLoader(_dataPath).CreateFrom<SentimentData>(useHeader: true));
- pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
- pipeline.Add(new FastForestBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });
- PredictionModel<SentimentData, SentimentPrediction> model = pipeline.Train<SentimentData, SentimentPrediction>();
- return model;
- }
- public static void Evaluate(PredictionModel<SentimentData, SentimentPrediction> model)
- {
- var testData = new TextLoader(_testDataPath).CreateFrom<SentimentData>(useHeader: true);
- var evaluator = new BinaryClassificationEvaluator();
- BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData);
- Console.WriteLine();
- Console.WriteLine("PredictionModel quality metrics evaluation");
- Console.WriteLine("-------------------------------------");
- Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
- Console.WriteLine($"Auc: {metrics.Auc:P2}");
- Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
- }
- public static void Predict(PredictionModel<SentimentData, SentimentPrediction> model)
- {
- IEnumerable<SentimentData> sentiments = new[]
- {
- new SentimentData
- {
- SentimentText = "Please refrain from adding nonsense to Wikipedia."
- },
- new SentimentData
- {
- SentimentText = "He is the best, and the article should say that."
- }
- };
- IEnumerable<SentimentPrediction> predictions = model.Predict(sentiments);
- Console.WriteLine();
- Console.WriteLine("Sentiment Predictions");
- Console.WriteLine("---------------------");
- var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));
- foreach (var item in sentimentsAndPredictions)
- {
- Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}");
- }
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment