Skip to content

Ranking Sample - Hotel Search Results (changed to Bing Search Engine … #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions samples/csharp/getting-started/Ranking_Web/README.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions samples/csharp/getting-started/Ranking_Web/WebRanking.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebRanking", "WebRanking\WebRanking.csproj", "{D502394E-930B-401A-812F-2A996751B80A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D502394E-930B-401A-812F-2A996751B80A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D502394E-930B-401A-812F-2A996751B80A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D502394E-930B-401A-812F-2A996751B80A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D502394E-930B-401A-812F-2A996751B80A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92FA42B0-28BF-4531-B744-F3125DAAC91A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.ML;
using Microsoft.ML.Data;
using WebRanking.DataStructures;
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebRanking.Common
{
public class ConsoleHelper
{
// To evaluate the accuracy of the model's predicted rankings, prints out the Discounted Cumulative Gain and Normalized Discounted Cumulative Gain for search queries.
public static void EvaluateMetrics(MLContext mlContext, IDataView predictions)
{
// Evaluate the metrics for the data using NDCG; by default, metrics for the up to 3 search results in the query are reported (e.g. NDCG@3).
RankingMetrics metrics = mlContext.Ranking.Evaluate(predictions);

Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}");

Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}\n");
}

// Performs evaluation with the truncation level set up to 10 search results within a query.
// This is a temporary workaround for this issue: https://github.com/dotnet/machinelearning/issues/2728.
public static void EvaluateMetrics(MLContext mlContext, IDataView predictions, int truncationLevel)
{
if (truncationLevel < 1 || truncationLevel > 10)
{
throw new InvalidOperationException("Currently metrics are only supported for 1 to 10 truncation levels.");
}

// Uses reflection to set the truncation level before calling evaluate.
var mlAssembly = typeof(TextLoader).Assembly;
var rankEvalType = mlAssembly.DefinedTypes.Where(t => t.Name.Contains("RankingEvaluator")).First();

var evalArgsType = rankEvalType.GetNestedType("Arguments");
var evalArgs = Activator.CreateInstance(rankEvalType.GetNestedType("Arguments"));

var dcgLevel = evalArgsType.GetField("DcgTruncationLevel");
dcgLevel.SetValue(evalArgs, truncationLevel);

var ctor = rankEvalType.GetConstructors().First();
var evaluator = ctor.Invoke(new object[] { mlContext, evalArgs });

var evaluateMethod = rankEvalType.GetMethod("Evaluate");
RankingMetrics metrics = (RankingMetrics)evaluateMethod.Invoke(evaluator, new object[] { predictions, "Label", "GroupId", "Score" });

Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}");

Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}\n");
}

// Prints out the the individual scores used to determine the relative ranking.
public static void PrintScores(IEnumerable<SearchResultPrediction> predictions)
{
foreach (var prediction in predictions)
{
Console.WriteLine($"GroupId: {prediction.GroupId}, Score: {prediction.Score}");
}
}
}
}
Loading