まず、解決すべき問題は、
問題:多くの場合、いくつかのユニットや組織が会議の入力会議の記録時間を必要とする、我々は自動的に、判断合否するためにユーザが入力したテキストのための機械学習に必要です。(また、同様のスパムメッセージが検出された同じ問題が、作業は品質分析をログに記録します。)
アイデアを処理:まだアルゴリズムの決定木学習アルゴリズム、これらのレコードのためのモデルを形成する学習することによって、マークの合否、当社の既存の人工的な裁判官を満たすことが、今回の高速なバイナリ分類し、最後の記事を使用しています入力値の特性は、もはやフロートが、中国語のテキストです。ここでは、テキストの特徴抽出に関係する必要があります。
なぜテキスト機能は、それを抽出しますか?テキストは人間の言語であるため、言葉のシンボルシーケンスは、アルゴリズムに直接渡すことはできません。コンピュータプログラムのアルゴリズムのみデジタル固有ベクトル固定長を有する(フロートまたはフロートアレイ)を受け付け、文書は、テキスト可変長を理解することはできません。
通常のテキスト特徴抽出方法は以下の通り:
これらは、ちょうど私達はそれにプラットフォームのネイティブメソッドを使用する必要がある、テキスト特徴抽出アルゴリズムを達成する必要はありません、一般的な意味を理解する必要があります。
メソッドのシステムは、入力は、文字列であり、英語の語彙の文が空間分割によって生まれたスペースで区切った単語の声明が必要ですが、中国の文章ではないので、我々は、セグメントワードに必要な、テキスト処理機能が付属しています次のように操作、プロセスは次のとおりです。
第二に、コード
コード全体的なプロセスについての記事と基本的に同じ説明は、簡単にするために、我々は、モデル記憶を省略して読みました。
データセットを見てください:
コードは以下の通りであります:
namespace BinaryClassification_TextFeaturize { class Program { static readonly string DataPath = Path.Combine(Environment.CurrentDirectory, "Data", "meeting_data_full.csv"); static void Main(string[] args) { MLContext mlContext = new MLContext(); var fulldata = mlContext.Data.LoadFromTextFile<MeetingInfo>(DataPath, separatorChar: ',', hasHeader: false); var trainTestData = mlContext.Data.TrainTestSplit(fulldata, testFraction: 0.15); var trainData = trainTestData.TrainSet; var testData = trainTestData.TestSet; var trainingPipeline = mlContext.Transforms.CustomMapping<JiebaLambdaInput, JiebaLambdaOutput>(mapAction: JiebaLambda.MyAction, contractName: "JiebaLambda") .Append(mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: "JiebaText")) .Append(mlContext.BinaryClassification.Trainers.FastTree(labelColumnName: "Label", featureColumnName: "Features")); ITransformer trainedModel = trainingPipeline.Fit(trainData); //评估 var predictions = trainedModel.Transform(testData); var metrics = mlContext.BinaryClassification.Evaluate(data: predictions, labelColumnName: "Label"); Console.WriteLine($"Evalution Accuracy: {metrics.Accuracy:P2}"); //创建预测引擎 var predEngine = mlContext.Model.CreatePredictionEngine<MeetingInfo, PredictionResult>(trainedModel); //预测1 MeetingInfo sampleStatement1 = new MeetingInfo { Text = "支委会。" }; var predictionresult1 = predEngine.Predict(sampleStatement1); Console.WriteLine($"{sampleStatement1.Text}:{predictionresult1.PredictedLabel}"); //预测2 MeetingInfo sampleStatement2 = new MeetingInfo { Text = "开展新时代中国特色社会主义思想三十讲党员答题活动。" }; var predictionresult2 = predEngine.Predict(sampleStatement2); Console.WriteLine($"{sampleStatement2.Text}:{predictionresult2.PredictedLabel}"); Console.WriteLine("Press any to exit!"); Console.ReadKey(); } } public class MeetingInfo { [LoadColumn(0)] public bool Label { get; set; } [LoadColumn(1)] public string Text { get; set; } } public class PredictionResult : MeetingInfo { public string JiebaText { get; set; } public float[] Features { get; set; } public bool PredictedLabel; public float Score; public float Probability; } }
三、代码分析
和上一篇文章中相似的内容我就不再重复解释了,重点介绍一下学习管道的建立。
var trainingPipeline = mlContext.Transforms.CustomMapping<JiebaLambdaInput, JiebaLambdaOutput>(mapAction: JiebaLambda.MyAction, contractName: "JiebaLambda") .Append(mlContext.Transforms.Text.FeaturizeText(outputColumnName: "Features", inputColumnName: "JiebaText")) .Append(mlContext.BinaryClassification.Trainers.FastTree(labelColumnName: "Label", featureColumnName: "Features"));
首先,在进行文本特征转换之前,我们需要对文本进行分词操作,您可以对样本数据进行预处理,形成分词的结果再进行学习,我们没有采用这个方法,而是自定义了一个分词处理的数据处理管道,通过这个管道进行分词,其定义如下:
namespace BinaryClassification_TextFeaturize { public class JiebaLambdaInput { public string Text { get; set; } } public class JiebaLambdaOutput { public string JiebaText { get; set; } } public class JiebaLambda { public static void MyAction(JiebaLambdaInput input, JiebaLambdaOutput output) { JiebaNet.Segmenter.JiebaSegmenter jiebaSegmenter = new JiebaNet.Segmenter.JiebaSegmenter(); output.JiebaText = string.Join(" ", jiebaSegmenter.Cut(input.Text)); } } }
最后我们新建了两个对象进行实际预测:
//预测1 MeetingInfo sampleStatement1 = new MeetingInfo { Text = "支委会。" }; var predictionresult1 = predEngine.Predict(sampleStatement1); Console.WriteLine($"{sampleStatement1.Text}:{predictionresult1.PredictedLabel}"); //预测2 MeetingInfo sampleStatement2 = new MeetingInfo { Text = "开展新时代中国特色社会主义思想三十讲党员答题活动。" }; var predictionresult2 = predEngine.Predict(sampleStatement2); Console.WriteLine($"{sampleStatement2.Text}:{predictionresult2.PredictedLabel}");
预测结果如下:
四、调试
上一篇文章提到,当我们运行Transform方法时,会对所有记录进行转换,转换后的数据集是什么样子呢,我们可以写一个调试程序看一下。
var predictions = trainedModel.Transform(testData); DebugData(mlContext, predictions); private static void DebugData(MLContext mlContext, IDataView predictions) { var trainDataShow = new List<PredictionResult>(mlContext.Data.CreateEnumerable<PredictionResult>(predictions, false, true)); foreach (var dataline in trainDataShow) { dataline.PrintToConsole(); } } public class PredictionResult { public string JiebaText { get; set; } public float[] Features { get; set; } public bool PredictedLabel; public float Score; public float Probability; public void PrintToConsole() { Console.WriteLine($"JiebaText={JiebaText}"); Console.WriteLine($"PredictedLabel:{PredictedLabel},Score:{Score},Probability:{Probability}"); Console.WriteLine($"TextFeatures Length:{Features.Length}"); if (Features != null) { foreach (var f in Features) { Console.Write($"{f},"); } Console.WriteLine(); } Console.WriteLine(); } }
通过对调试结果的分析,可以看到整个数据处理管道的工作流程。
五、资源获取
源码下载地址:https://github.com/seabluescn/Study_ML.NET
工程名称:BinaryClassification_TextFeaturize