---
title: "How can you perform sentiment analysis using C# and ML.NET?"  
description: "How can you perform sentiment analysis using C# and ML.NET?"  
author: "Anubhav Sharma"  
published: 2026-05-19  
updated: 2026-05-24  
canonical: https://answers.mindstick.com/qa/116639/how-can-you-perform-sentiment-analysis-using-c-sharp-and-ml-dot-net  
category: "artificial-intelligence"  
tags: ["artificial intelligence"]  
reading_time: 3 minutes  

---

# How can you perform sentiment analysis using C# and ML.NET?

**How can you perform [sentiment analysis](https://www.mindstick.com/articles/156985/sentiment-analysis-using-python-in-tableau-with-tabpy) using C# and ML.NET?**

## Answers

### Answer by Anubhav Sharma

You can perform sentiment [analysis](https://www.mindstick.com/articles/339568/how-to-use-cohort-analysis-to-identify-high-retention-behaviors) in C# using ML.NET by training or consuming a machine learning model that classifies text as positive or negative.

A common workflow is:

- Install ML.NET packages
- Prepare training data
- Build and train a pipeline
- Evaluate the model
- Predict sentiment on new text

Official docs: [ML.NET Documentation](https://learn.microsoft.com/dotnet/machine-learning/)

## 1. Create a Console Project

```cs
dotnet new console -n SentimentAnalysisApp
cd SentimentAnalysisApp
```

Install the ML.NET package:

```cs
dotnet add package Microsoft.ML
```

ML.NET NuGet package:\
[Microsoft.ML NuGet Package](https://www.nuget.org/packages/Microsoft.ML/)

## 2. Prepare Training Data

Create a file named `sentiment-data.tsv`.

Example:

```plaintext
Sentiment	Text
1	I love this product
1	This is amazing
0	I hate this item
0	This is terrible
```

- `1` = Positive
- `0` = Negative

## 3. Create Data Models

Create a file called `SentimentData.cs`.

```cs
using Microsoft.ML.Data;

// Input data class
public class SentimentData
{
    // First column from dataset
    [LoadColumn(0)]
    public bool Sentiment { get; set; }

    // Second column from dataset
    [LoadColumn(1)]
    public string Text { get; set; }
}

// Prediction output class
public class SentimentPrediction
{
    // Predicted sentiment
    [ColumnName("PredictedLabel")]
    public bool Prediction { get; set; }

    // Confidence score
    public float Probability { get; set; }

    // Raw model score
    public float Score { get; set; }
}
```

## 4. Train the Model

Replace `Program.cs` with:

```cs
using Microsoft.ML;
using Microsoft.ML.Data;

// Create ML context
var mlContext = new MLContext();

// Load training data
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(
    path: "sentiment-data.tsv",
    hasHeader: true
);

// Split dataset into train/test sets
var splitData = mlContext.Data.TrainTestSplit(
    dataView,
    testFraction: 0.2
);

// Build ML pipeline
var pipeline = mlContext.Transforms.Text.FeaturizeText(
        outputColumnName: "Features",
        inputColumnName: nameof(SentimentData.Text)
    )
    .Append(
        mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(
            labelColumnName: nameof(SentimentData.Sentiment),
            featureColumnName: "Features"
        )
    );

// Train model
var model = pipeline.Fit(splitData.TrainSet);

// Evaluate model
var predictions = model.Transform(splitData.TestSet);

var metrics = mlContext.BinaryClassification.Evaluate(
    predictions,
    labelColumnName: nameof(SentimentData.Sentiment)
);

// Print evaluation metrics
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"F1 Score: {metrics.F1Score:P2}");
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:P2}");

// Create prediction engine
var predictionEngine = mlContext.Model.CreatePredictionEngine
    <SentimentData, SentimentPrediction>(model);

// Example prediction
var sample = new SentimentData
{
    Text = "This app is fantastic!"
};

// Predict sentiment
var prediction = predictionEngine.Predict(sample);

// Display result
Console.WriteLine($"Text: {sample.Text}");
Console.WriteLine($"Prediction: {(prediction.Prediction ? "Positive" : "Negative")}");
Console.WriteLine($"Probability: {prediction.Probability:P2}");
```

## 5. Run the Application

```cs
dotnet run
```

Example output:

```plaintext
Accuracy: 95.00%
F1 Score: 94.00%
AUC: 97.00%

Text: This app is fantastic!
Prediction: Positive
Probability: 98.12%
```

## How ML.NET Processes Text

The important step is:

```cs
FeaturizeText()
```

This converts raw text into numerical vectors using NLP techniques such as:

- Tokenization
- N-grams
- TF-IDF weighting
- Text normalization

The classifier then learns patterns associated with positive or negative sentiment.

## Using a Pretrained Model

Instead of training from scratch, you can also use:

- ONNX models
- Azure AI services

Hugging Face transformer models exported to ONNX

ONNX with ML.NET:\
ML.NET ONNX Guide

## Real-World Use Cases

Sentiment analysis is commonly used for:

- Customer review analysis
- Social media monitoring
- Chat moderation
- Support ticket prioritization
- Product feedback analytics

## Improving Accuracy

For production systems:

- Use larger datasets
- Clean noisy text
- Remove spam/duplicates
- Use transformer-based models
- Balance positive/negative samples
- Add multilingual support if needed

## Useful ML.NET Resources

[ML.NET Official Site](https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet)

[ML.NET Tutorials](https://learn.microsoft.com/dotnet/machine-learning/tutorials/)

[ML.NET GitHub Repository](https://github.com/dotnet/machinelearning)


---

Original Source: https://answers.mindstick.com/qa/116639/how-can-you-perform-sentiment-analysis-using-c-sharp-and-ml-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
