<jupyter_start><jupyter_text><NAME> -> Cientista <NAME> 👋(<EMAIL>)[![Linkedin Badge](https://img.shields.io/badge/-LinkedIn-blue?style=flat-square&logo=Linkedin&logoColor=white&link=https://www.linkedin.com/in/davi-ramos/)](https://www.linkedin.com/in/davi-ramos/)[![Twitter Badge](https://img.shields.io/badge/-Twitter-1DA1F2?style=flat-square&logo=Twitter&logoColor=white&link=https://twitter.com/Daviinfo/)](https://twitter.com/Daviinfo/)  ML.Net - Análise de Sentimentos Este exemplo irá criar um modelo para Análise  de sentimentos em Comentários Tóxicos<jupyter_code>// Instalar os Pacotes do Nuget

// ML.NET
#r "nuget:Microsoft.ML"  
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
public class ModelInput
{
    [ColumnName("Sentiment"), LoadColumn(0)]
    public bool Sentiment { get; set; }


    [ColumnName("SentimentText"), LoadColumn(1)]
    public string SentimentText { get; set; }
}

public class ModelOutput
{
    // ColumnName attribute is used to change the column name from
    // its default value, which is the name of the field.
    [ColumnName("PredictedLabel")]
    public bool Prediction { get; set; }

    public float Score { get; set; }
}
private static string TRAIN_DATA_FILEPATH = @"./datasets/wikipedia_detox_250/wikipedia-detox-250-line-data.tsv";
private static string MODEL_FILEPATH = @"./datasets/wikipedia_detox_250/MLModel.zip";
public static void PrintBinaryClassificationMetrics(BinaryClassificationMetrics metrics)
{
    Console.WriteLine($"************************************************************");
    Console.WriteLine($"*       Metrics for binary classification model      ");
    Console.WriteLine($"*-----------------------------------------------------------");
    Console.WriteLine($"*       Accuracy: {metrics.Accuracy:P2}");
    Console.WriteLine($"*       Auc:      {metrics.AreaUnderRocCurve:P2}");
    Console.WriteLine($"************************************************************");
}


public static void PrintBinaryClassificationFoldsAverageMetrics(IEnumerable<TrainCatalogBase.CrossValidationResult<BinaryClassificationMetrics>> crossValResults)
{
    var metricsInMultipleFolds = crossValResults.Select(r => r.Metrics);

    var AccuracyValues = metricsInMultipleFolds.Select(m => m.Accuracy);
    var AccuracyAverage = AccuracyValues.Average();
    var AccuraciesStdDeviation = CalculateStandardDeviation(AccuracyValues);
    var AccuraciesConfidenceInterval95 = CalculateConfidenceInterval95(AccuracyValues);


    Console.WriteLine($"*************************************************************************************************************");
    Console.WriteLine($"*       Metrics for Binary Classification model      ");
    Console.WriteLine($"*------------------------------------------------------------------------------------------------------------");
    Console.WriteLine($"*       Average Accuracy:    {AccuracyAverage:0.###}  - Standard deviation: ({AccuraciesStdDeviation:#.###})  - Confidence Interval 95%: ({AccuraciesConfidenceInterval95:#.###})");
    Console.WriteLine($"*************************************************************************************************************");
}

public static double CalculateStandardDeviation(IEnumerable<double> values)
{
    double average = values.Average();
    double sumOfSquaresOfDifferences = values.Select(val => (val - average) * (val - average)).Sum();
    double standardDeviation = Math.Sqrt(sumOfSquaresOfDifferences / (values.Count() - 1));
    return standardDeviation;
}

public static double CalculateConfidenceInterval95(IEnumerable<double> values)
{
    double confidenceInterval95 = 1.96 * CalculateStandardDeviation(values) / Math.Sqrt((values.Count() - 1));
    return confidenceInterval95;
}
private static MLContext mlContext = new MLContext(seed: 1);
// Load Data
IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
                                path: TRAIN_DATA_FILEPATH,
                                hasHeader: true,
                                separatorChar: '\t',
                                allowQuoting: true,
                                allowSparse: false);
// Data process configuration with pipeline data transformations 
var dataProcessPipeline = mlContext.Transforms.Text.FeaturizeText("SentimentText_tf", "SentimentText")
                          .Append(mlContext.Transforms.CopyColumns("Features", "SentimentText_tf"))
                          .Append(mlContext.Transforms.NormalizeMinMax("Features", "Features"))
                          .AppendCacheCheckpoint(mlContext);

// Set the training algorithm 
var trainer = mlContext.BinaryClassification.Trainers.AveragedPerceptron(labelColumnName: "Sentiment", numberOfIterations: 10, featureColumnName: "Features");
var trainingPipeline = dataProcessPipeline.Append(trainer);
// Cross-Validate with single dataset (since we don't have two datasets, one for training and for evaluate)
// in order to evaluate and get the model's accuracy metrics
Console.WriteLine("=============== Cross-validating to get model's accuracy metrics ===============");
var crossValidationResults = mlContext.BinaryClassification.CrossValidateNonCalibrated(trainingDataView, trainingPipeline, numberOfFolds: 5, labelColumnName: "Sentiment");
PrintBinaryClassificationFoldsAverageMetrics(crossValidationResults);
Console.WriteLine("=============== Training  model ===============");

model = trainingPipeline.Fit(trainingDataView);

Console.WriteLine("=============== End of training process ===============");
return model;
// Save/persist the trained model to a .ZIP file
Console.WriteLine($"=============== Saving the model  ===============");
mlContext.Model.Save(model, trainingDataView.Schema, MODEL_FILEPATH);
Console.WriteLine("The model is saved to {0}", MODEL_FILEPATH);<jupyter_output>=============== Saving the model  ===============
The model is saved to ./datasets/wikipedia_detox_250/MLModel.zip
{'fraction_non_alphanumeric': 0.20977630488815244, 'fraction_numerical': 0.0072908036454018225, 'mean_word_length': 4.128292268479185, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '525603', 'license': 'MIT', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1522, 'n_words': 339, 'path': 'Notebooks/ML.Net - Analise de Sentimentos.ipynb', 'repo_name': 'DaviRamos/ML.Net', 'size': '6053'}