Ranking

Learn how to apply ranking in ML.NET.

Ranking involves learning to rank a list of items based on their relevance or preference for a given user or query. These tasks are commonly encountered in recommendation systems, search engines, information retrieval, and other applications where the goal is to present a ranked list of items to the user. The following diagram summarizes how ranking works:

Press + to interact
Ranking illustrated
Ranking illustrated

The goal of ranking is to provide users with personalized and relevant recommendations or search results by effectively ordering the items based on their estimated relevance. The ranking models learn from historical data and user feedback, continually improving their ability to generate accurate and useful rankings.

Ranking application structure

Currently, there is no dedicated mlnet CLI command to train a ranking model. Therefore, we have to write code for our own training pipeline to train such a model. Fortunately, the process is relatively simple, as the following playground demonstrates:

using Microsoft.ML.Data;

namespace RankingModelExample;

internal class Input
{
    [LoadColumn(0), ColumnName("Label")]
    public float Score { get; set; }

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

    [LoadColumn(2, 133)]
    [VectorType(133)]
    public float[] Features { get; set; }
}
Ranking model

For the convenience of demonstration, the entire logic has been placed in the Program.cs file.

Loading input data

Before we can start the training process, we need to create an instance of the MLContext class, which we do in ...