Skip to content

Commit f50535f

Browse files
nicolehaugenCESARDELATORRE
authored andcommitted
Ranking Sample - Hotel Search Results (changed to Bing Search Engine … (#549)
* Ranking Sample - Hotel Search Results (changed to Bing Search Engine result ranking) (#533) * Created ranking sample * removed todo * Fixed wording in ReadMe * Fixed typos * Modified RankingMetric code * Incorporated Justin's feedback * Fixed minor inconsistencies * Converted to new dataset * Changed code to download dataset since its zip is too large * fixed using statement * Removed unneeded license info for dataset * Renamed solution and minor changes * minor fixes * Justin's feedback for PR into master * fixed period and spacing inconsistencies
1 parent 888a2c7 commit f50535f

File tree

8 files changed

+1091
-2
lines changed

8 files changed

+1091
-2
lines changed

samples/csharp/getting-started/Ranking_Web/README.md

+199
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.705
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebRanking", "WebRanking\WebRanking.csproj", "{D502394E-930B-401A-812F-2A996751B80A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D502394E-930B-401A-812F-2A996751B80A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D502394E-930B-401A-812F-2A996751B80A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D502394E-930B-401A-812F-2A996751B80A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D502394E-930B-401A-812F-2A996751B80A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {92FA42B0-28BF-4531-B744-F3125DAAC91A}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.ML;
2+
using Microsoft.ML.Data;
3+
using WebRanking.DataStructures;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace WebRanking.Common
9+
{
10+
public class ConsoleHelper
11+
{
12+
// To evaluate the accuracy of the model's predicted rankings, prints out the Discounted Cumulative Gain and Normalized Discounted Cumulative Gain for search queries.
13+
public static void EvaluateMetrics(MLContext mlContext, IDataView predictions)
14+
{
15+
// 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).
16+
RankingMetrics metrics = mlContext.Ranking.Evaluate(predictions);
17+
18+
Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}");
19+
20+
Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}\n");
21+
}
22+
23+
// Performs evaluation with the truncation level set up to 10 search results within a query.
24+
// This is a temporary workaround for this issue: https://github.com/dotnet/machinelearning/issues/2728.
25+
public static void EvaluateMetrics(MLContext mlContext, IDataView predictions, int truncationLevel)
26+
{
27+
if (truncationLevel < 1 || truncationLevel > 10)
28+
{
29+
throw new InvalidOperationException("Currently metrics are only supported for 1 to 10 truncation levels.");
30+
}
31+
32+
// Uses reflection to set the truncation level before calling evaluate.
33+
var mlAssembly = typeof(TextLoader).Assembly;
34+
var rankEvalType = mlAssembly.DefinedTypes.Where(t => t.Name.Contains("RankingEvaluator")).First();
35+
36+
var evalArgsType = rankEvalType.GetNestedType("Arguments");
37+
var evalArgs = Activator.CreateInstance(rankEvalType.GetNestedType("Arguments"));
38+
39+
var dcgLevel = evalArgsType.GetField("DcgTruncationLevel");
40+
dcgLevel.SetValue(evalArgs, truncationLevel);
41+
42+
var ctor = rankEvalType.GetConstructors().First();
43+
var evaluator = ctor.Invoke(new object[] { mlContext, evalArgs });
44+
45+
var evaluateMethod = rankEvalType.GetMethod("Evaluate");
46+
RankingMetrics metrics = (RankingMetrics)evaluateMethod.Invoke(evaluator, new object[] { predictions, "Label", "GroupId", "Score" });
47+
48+
Console.WriteLine($"DCG: {string.Join(", ", metrics.DiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}");
49+
50+
Console.WriteLine($"NDCG: {string.Join(", ", metrics.NormalizedDiscountedCumulativeGains.Select((d, i) => $"@{i + 1}:{d:F4}").ToArray())}\n");
51+
}
52+
53+
// Prints out the the individual scores used to determine the relative ranking.
54+
public static void PrintScores(IEnumerable<SearchResultPrediction> predictions)
55+
{
56+
foreach (var prediction in predictions)
57+
{
58+
Console.WriteLine($"GroupId: {prediction.GroupId}, Score: {prediction.Score}");
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)