Guest User

Untitled

a guest
Jun 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. using System;
  2. using Microsoft.ML.Models;
  3. using Microsoft.ML.Runtime;
  4. using Microsoft.ML.Runtime.Api;
  5. using Microsoft.ML.Trainers;
  6. using Microsoft.ML.Transforms;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using Microsoft.ML;
  10. using Microsoft.ML.Data;
  11. using System.Threading.Tasks;
  12.  
  13. namespace MLNETTest
  14. {
  15. public class SentimentData
  16. {
  17. [Column(ordinal: "0", name: "Label")]
  18. public float Sentiment;
  19.  
  20. [Column(ordinal: "1")]
  21. public string SentimentText;
  22. }
  23.  
  24. public class SentimentPrediction
  25. {
  26. [ColumnName("PredictedLabel")]
  27. public bool Sentiment;
  28. }
  29.  
  30. class Program
  31. {
  32. const string _dataPath = @"wikipedia-detox-250-line-data.tsv";
  33. const string _testDataPath = @"wikipedia-detox-250-line-test.tsv";
  34.  
  35. static async Task Main(string[] args)
  36. {
  37. var model = await TrainAsync();
  38.  
  39. Evaluate(model);
  40.  
  41. Predict(model);
  42. }
  43.  
  44. public static async Task<PredictionModel<SentimentData, SentimentPrediction>> TrainAsync()
  45. {
  46. var pipeline = new LearningPipeline();
  47.  
  48. pipeline.Add(new TextLoader(_dataPath).CreateFrom<SentimentData>(useHeader: true));
  49.  
  50. pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
  51.  
  52. pipeline.Add(new FastForestBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });
  53.  
  54. PredictionModel<SentimentData, SentimentPrediction> model = pipeline.Train<SentimentData, SentimentPrediction>();
  55.  
  56. return model;
  57. }
  58.  
  59. public static void Evaluate(PredictionModel<SentimentData, SentimentPrediction> model)
  60. {
  61. var testData = new TextLoader(_testDataPath).CreateFrom<SentimentData>(useHeader: true);
  62.  
  63. var evaluator = new BinaryClassificationEvaluator();
  64.  
  65. BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData);
  66.  
  67. Console.WriteLine();
  68. Console.WriteLine("PredictionModel quality metrics evaluation");
  69. Console.WriteLine("-------------------------------------");
  70. Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
  71. Console.WriteLine($"Auc: {metrics.Auc:P2}");
  72. Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
  73.  
  74. }
  75.  
  76. public static void Predict(PredictionModel<SentimentData, SentimentPrediction> model)
  77. {
  78. IEnumerable<SentimentData> sentiments = new[]
  79. {
  80. new SentimentData
  81. {
  82. SentimentText = "Please refrain from adding nonsense to Wikipedia."
  83. },
  84.  
  85. new SentimentData
  86. {
  87. SentimentText = "He is the best, and the article should say that."
  88. }
  89. };
  90.  
  91. IEnumerable<SentimentPrediction> predictions = model.Predict(sentiments);
  92.  
  93. Console.WriteLine();
  94. Console.WriteLine("Sentiment Predictions");
  95. Console.WriteLine("---------------------");
  96.  
  97. var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));
  98.  
  99. foreach (var item in sentimentsAndPredictions)
  100. {
  101. Console.WriteLine($"Sentiment: {item.sentiment.SentimentText} | Prediction: {(item.prediction.Sentiment ? "Positive" : "Negative")}");
  102. }
  103. Console.ReadLine();
  104. }
  105. }
  106. }
Add Comment
Please, Sign In to add comment