日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ML.net 3-情绪预测

發布時間:2024/3/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ML.net 3-情绪预测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 加載測試數據(csv)

2.加載模型

3.訓練數據

4.預測一句話的情緒

實現:

using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.ML; using Microsoft.ML.Data; using Microsoft.ML.Models; using Microsoft.ML.Runtime.Api; using Microsoft.ML.Trainers; using Microsoft.ML.Transforms;namespace _02_SentimentAnalysis {public static class SentimentAnalysisExecutor{static readonly string _dataPath = Path.Combine(Environment.CurrentDirectory, "wikipedia-detox-250-line-data.tsv");static readonly string _testDataPath = Path.Combine(Environment.CurrentDirectory, "wikipedia-detox-250-line-test.tsv");static readonly string _modelpath = Path.Combine(Environment.CurrentDirectory, "Model.zip");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;public override string ToString(){return Sentiment ? "Positive" : "Negtive";}}public static async Task<IEnumerable<SentimentPrediction>> Run(IEnumerable<SentimentData> inputs){var model = await Train();Evoluate(model);var results = model.Predict(inputs);return results;}private static void Evoluate(PredictionModel<SentimentData, SentimentPrediction> model){var testData = new TextLoader(_testDataPath).CreateFrom<SentimentData>();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}");}private static async Task<PredictionModel<SentimentData, SentimentPrediction>> Train(){var pipeline = new LearningPipeline();pipeline.Add(new TextLoader(_dataPath).CreateFrom<SentimentData>());pipeline.Add(new TextFeaturizer("Features", "SentimentText"));pipeline.Add(new FastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });PredictionModel<SentimentData, SentimentPrediction> model =pipeline.Train<SentimentData, SentimentPrediction>();await model.WriteAsync(_modelpath);return model;}} }

2. 調用:

using System;namespace _02_SentimentAnalysis {class Program{static void Main(string[] args){var ret = SentimentAnalysisExecutor.Run(new[]{new SentimentAnalysisExecutor.SentimentData{SentimentText = "Please refrain from adding nonsense to Wikipedia."},new SentimentAnalysisExecutor.SentimentData{SentimentText = "He is the best, and the article should say that."},new SentimentAnalysisExecutor.SentimentData{SentimentText = "I'm not sure If that is correct."},}).Result;foreach (var sentimentPrediction in ret){Console.WriteLine(sentimentPrediction);}Console.ReadLine();}} }


?

總結

以上是生活随笔為你收集整理的ML.net 3-情绪预测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。