Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Dotnet Machinelearning MLContext Constructor

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Software Engineering, .NET
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for creating the central ML.NET context object provided by ML.NET.

Description

The MLContext constructor instantiates the root object that exposes every ML.NET capability through strongly-typed catalog properties. Internally, the constructor initializes an IHostEnvironment that carries the random seed, logging infrastructure, and component registration. Each catalog property (BinaryClassification, MulticlassClassification, Regression, Clustering, Ranking, AnomalyDetection, Forecasting, Transforms, Model, Data) is lazily or eagerly wired to this shared environment, ensuring consistent seed propagation and logging behavior across all operations.

When a non-null seed is supplied, every stochastic operation (shuffling, sampling, weight initialization) uses deterministic sequences derived from that seed. When seed is null, the runtime uses a time-based default for non-deterministic behavior.

Usage

Import and instantiate MLContext at the start of any ML.NET program. Pass a fixed seed for reproducible experiments, unit tests, or benchmarks. Omit the seed for production exploration.

Code Reference

Source Location

  • Repository: ML.NET
  • File: src/Microsoft.ML.Data/MLContext.cs:L21-145

Signature

public MLContext(int? seed = null)

Import

using Microsoft.ML;

I/O Contract

Inputs

Name Type Required Description
seed int? No Optional random seed for reproducibility. When null, a time-dependent default is used.

Outputs

Name Type Description
(return) MLContext Fully initialized context exposing all ML.NET catalogs and operations.

Exposed Properties:

Property Type Description
BinaryClassification BinaryClassificationCatalog Trainers and evaluators for two-class classification.
MulticlassClassification MulticlassClassificationCatalog Trainers and evaluators for multi-class classification.
Regression RegressionCatalog Trainers and evaluators for regression tasks.
Clustering ClusteringCatalog Trainers and evaluators for clustering tasks.
Ranking RankingCatalog Trainers and evaluators for ranking tasks.
AnomalyDetection AnomalyDetectionCatalog Trainers and evaluators for anomaly detection.
Forecasting ForecastingCatalog Trainers for time-series forecasting.
Transforms TransformsCatalog Feature engineering and data transforms.
Model ModelOperationsCatalog Model save/load operations.
Data DataOperationsCatalog Data loading, saving, caching, filtering, and splitting.

Usage Examples

Basic Example

using Microsoft.ML;

// Create context with a fixed seed for reproducible results
var mlContext = new MLContext(seed: 42);

// Access catalogs for pipeline construction
var dataView = mlContext.Data.LoadFromTextFile<SentimentData>(
    "data.csv", separatorChar: ',', hasHeader: true);

var pipeline = mlContext.Transforms.Text.FeaturizeText("Features", "Text")
    .Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression());

var model = pipeline.Fit(dataView);

// Save the trained model
mlContext.Model.Save(model, dataView.Schema, "model.zip");

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment