{"id":726,"date":"2024-12-21T07:04:52","date_gmt":"2024-12-21T07:04:52","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2024\/12\/21\/ranking-basics-pointwise-pairwise-listwise-cd5318f86e1b\/"},"modified":"2024-12-21T07:04:52","modified_gmt":"2024-12-21T07:04:52","slug":"ranking-basics-pointwise-pairwise-listwise-cd5318f86e1b","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2024\/12\/21\/ranking-basics-pointwise-pairwise-listwise-cd5318f86e1b\/","title":{"rendered":"Ranking Basics: Pointwise, Pairwise, Listwise"},"content":{"rendered":"<p>    Ranking Basics: Pointwise, Pairwise, Listwise<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<h4>Because thy neighbour matters<\/h4>\n<figure><img data-recalc-dims=\"1\" decoding=\"async\" alt=\"\" src=\"https:\/\/i0.wp.com\/cdn-images-1.medium.com\/max\/1024\/1%2AogM3hQ5j8lYdU2Gb8xA3nw.jpeg?ssl=1\"><figcaption>Image taken from unsplash.com<\/figcaption><\/figure>\n<p>First, let\u2019s talk about where ranking comes into play. Ranking is a big deal in e-commerce and search applications\u200a\u2014\u200aessentially, any scenario where you need to organize documents based on a query. It\u2019s a little different from classic classification or regression problems. For instance, in the Titanic dataset, you predict whether a passenger survives or not, and in house price prediction, you estimate the price of a house. But with ranking, the game changes. Instead of predicting a single value or category, you\u2019re trying to order documents based on relevance.<\/p>\n<p>Take an example: You search for \u201csaree\u201d on an e-commerce website like Amazon. You don\u2019t just want a random list of sarees; you want the most relevant ones to appear at the top, right? That\u2019s where Learning to Rank (LTR) steps in\u200a\u2014\u200ait ranks documents (or products) based on how well they match your\u00a0query.<\/p>\n<p>Now that we know where ranking fits in, let\u2019s dive into the nitty-gritty of different approaches and\u00a0methods.<\/p>\n<p>There are three main methods for Learning to Rank\u00a0(LTR):<\/p>\n<ol>\n<li><strong>Pointwise<\/strong><\/li>\n<li><strong>Pairwise<\/strong><\/li>\n<li><strong>Listwise<\/strong><\/li>\n<\/ol>\n<p>To make things easier to follow, let\u2019s establish some notation that we\u2019ll use to explain these\u00a0methods.<\/p>\n<p>We\u2019ll work with a set of queries <strong><em>q1,q2,\u2026,qn <\/em><\/strong>and each query has a corresponding set of documents <strong><em>d1,d2,d3,\u2026,dm<\/em><\/strong>\u200b. For\u00a0example:<\/p>\n<ul>\n<li>Query <strong><em>q1<\/em><\/strong> is associated with documents <strong><em>d1<\/em><\/strong>,<strong><em>d2<\/em><\/strong>,<strong><em>d3<\/em><\/strong>\n<\/li>\n<li>Query <strong><em>q2<\/em><\/strong> associated with documents <strong><em>d4<\/em><\/strong>,<strong><em>d5<\/em><\/strong>.<\/li>\n<\/ul>\n<p>With this setup in mind, let\u2019s break down each method and how they approach the ranking\u00a0problem.<\/p>\n<h3>Pointwise<\/h3>\n<p>In the <strong>pointwise approach<\/strong>, we treat the ranking problem as a simple classification task. For each query-document pair, we assign a target label that indicates the relevance of the document to the query. For\u00a0example:<\/p>\n<ul>\n<li>Label 1 if the document is relevant.<\/li>\n<li>Label 0 if the document is not relevant.<\/li>\n<\/ul>\n<p>Using our earlier example, the data would look like\u00a0this:<\/p>\n<ul>\n<li>\n<strong><em>q1,d1<\/em><\/strong>\u2192label: 1<\/li>\n<li>\n<strong><em>q1,d2<\/em><\/strong>\u2192label: 0<\/li>\n<li>\n<strong><em>q1,d3<\/em><\/strong>\u2192label: 1<\/li>\n<li>\n<strong><em>q2,d4<\/em><\/strong>\u2192label: 0<\/li>\n<li>\n<strong><em>q2,d5<\/em><\/strong>\u2192label: 1<\/li>\n<\/ul>\n<p>We train the model using this labeled data, leveraging features from both the queries and the documents to predict the label. After training, the model predicts the relevance of each document to a given query as a probability (ranging from 0 to 1). This probability can be interpreted as the relevance score.<\/p>\n<p>For example, after training, the model might produce the following scores:<\/p>\n<ul>\n<li>\n<strong><em>q1\u200b,d1<\/em><\/strong>\u200b\u2192score: 0.6<\/li>\n<li>\n<strong><em>q1,d2<\/em><\/strong>\u2192score: 0.1<\/li>\n<li>\n<strong><em>q1,d3<\/em><\/strong>\u2192score: 0.4<\/li>\n<\/ul>\n<p>Using these scores, we re-rank the documents in descending order of relevance: <strong><em>d1,d3,d2<\/em><\/strong>. This new ranking order is then presented to the user, ensuring the most relevant documents appear at the\u00a0top.<\/p>\n<h3><strong>Pairwise<\/strong><\/h3>\n<p>The main drawback of the <strong>pointwise approach<\/strong> is that it misses the <strong>context<\/strong> in which the user interacts with a document. When a user clicks on or finds a document relevant, there are often multiple factors at play\u200a\u2014\u200aone of the most important being the <strong>neighboring items<\/strong>.<\/p>\n<p>For instance, if a user clicks on a document, it might not necessarily mean that the document is highly relevant. It could simply be that the other documents presented were of poor quality. Similarly, if you had shown a different set of documents for the same query, the user\u2019s interaction might have been entirely different.<\/p>\n<p>Imagine presenting <strong><em>d4<\/em><\/strong>\u200b for query <strong><em>q1<\/em><\/strong>\u200b. If <strong><em>d4\u200b<\/em><\/strong> is more relevant than <strong><em>d1<\/em><\/strong>\u200b, the user might have clicked on <strong><em>d4\u200b<\/em><\/strong> instead. This context\u200a\u2014\u200ahow documents compare to each other is completely overlooked in the pointwise approach.<\/p>\n<p>To capture this <strong>relative relevance<\/strong>, we turn to the <strong>pairwise approach<\/strong>.<\/p>\n<p>In the pairwise method, instead of looking at query-document pairs in isolation, we focus on <strong>pairs of documents<\/strong> for the same query and try to predict which one is more relevant. This helps incorporate the context of comparison between documents.<\/p>\n<p>We\u2019ll generate the data similarly for now, but the way we use it will be slightly more complex. Let\u2019s break that down\u00a0next.<\/p>\n<p>Imagine the training data for the <strong>pairwise approach<\/strong> structured as\u00a0follows:<\/p>\n<ul>\n<li>\n<strong><em>q1,(d1,d2)<\/em><\/strong>\u2192label: 1(indicating <strong><em>d1<\/em><\/strong>\u200b is more relevant than\u00a0<strong><em>d2<\/em><\/strong>\u200b)<\/li>\n<li>\n<strong><em>q1,(d2,d3)<\/em><\/strong>\u2192label: 0 (indicating <strong><em>d2<\/em><\/strong>\u200b is less relevant than\u00a0<strong><em>d3<\/em><\/strong>\u200b)<\/li>\n<li>\n<strong><em>q1,(d1,d3)<\/em><\/strong>\u2192label: 1 (indicating <strong><em>d1<\/em><\/strong> is more relevant than\u00a0<strong><em>d3\u200b<\/em><\/strong>)<\/li>\n<li>\n<strong><em>q2,(d4,d5)<\/em><\/strong>\u2192label: 0(indicating <strong><em>d4<\/em><\/strong> is less relevant than\u00a0<strong><em>d5<\/em><\/strong>\u200b)<\/li>\n<\/ul>\n<p>Here, we assign the labels based on user interactions. For instance, <strong><em>d1<\/em><\/strong>\u200b and <strong><em>d3<\/em><\/strong>\u200b both being clicked indicates they are relevant, so we maintain their order for simplicity in this explanation.<\/p>\n<h4>Model Training\u00a0Process:<\/h4>\n<p>Although the training data is in pairs, the model doesn\u2019t directly process these pairs. Instead, we treat it similarly to a classification problem, where each <strong>query-document pair<\/strong> is passed to the model separately.<\/p>\n<p>For example:<\/p>\n<ul>\n<li><strong><em>s1 =\u00a0f(q1,d1)<\/em><\/strong><\/li>\n<li><strong><em>s2 =\u00a0f(q1,d2)<\/em><\/strong><\/li>\n<li><strong><em>s3 =\u00a0f(q1,d3)<\/em><\/strong><\/li>\n<\/ul>\n<p>The model generates scores <strong><em>s1,s2,s3<\/em><\/strong>\u200b for the documents. These scores are used to compare the relevance of document\u00a0pairs.<\/p>\n<p><strong>Penalizing the\u00a0Model:<\/strong><\/p>\n<p>If the model predicts scores that violate the true order of relevance, it is penalized. For\u00a0example:<\/p>\n<ul>\n<li>If <strong><em>s1&lt;s2<\/em><\/strong>, but the training data indicates <strong><em>d1&gt;d2<\/em><\/strong>\u200b, the model is penalized because it failed to rank <strong><em>d1<\/em><\/strong>\u200b higher than\u00a0<strong><em>d2<\/em><\/strong>\u200b.<\/li>\n<li>If <strong><em>s2&lt;s3<\/em><\/strong>\u200b, and the training data indicates <strong><em>d2&lt;d3<\/em><\/strong>\u200b, the model did the right thing, so no penalty is\u00a0applied.<\/li>\n<\/ul>\n<p><strong>This pairwise comparison helps the model learn the relative order of documents for a query, rather than just predicting a standalone relevance score like in the pointwise approach.<\/strong><\/p>\n<p><strong>Challenges:<\/strong><\/p>\n<p>One of the main challenges of implementing pairwise models is the <strong>computational complexity<\/strong>\u200a\u2014\u200asince we need to compare all possible pairs of documents, the process scales as O(n\u00b2). Additionally, pairwise methods don\u2019t consider the <strong>global ranking<\/strong> of documents; they focus only on individual pairs during comparisons, which can lead to inconsistencies in the overall\u00a0ranking.<\/p>\n<h3>Listwise<\/h3>\n<p>In listwise ranking, the goal is to optimize the entire list of documents based on their relevance to a query. Instead of treating individual documents separately, the focus is on the order in which they appear in the\u00a0list.<\/p>\n<p>Here\u2019s a breakdown of how this works in ListNet and LambdaRank:<\/p>\n<p><strong>NDCG (Normalized Discounted Cumulative Gain)<\/strong>: I\u2019ll dive deeper into NDCG in another blog, but for now, think of it as a way to measure how well the ordering of items matches their relevance. It rewards relevant items appearing at the top of the list and normalizes the score for easier comparison.<\/p>\n<p>In listwise ranking, if you have a list of documents (d1, d2, d3), the model considers all possible permutations of these documents:<\/p>\n<ul>\n<li><strong><em>(d1, d2,\u00a0d3)<\/em><\/strong><\/li>\n<li><strong><em>(d1, d3,\u00a0d2)<\/em><\/strong><\/li>\n<li><strong><em>(d2, d1,\u00a0d3)<\/em><\/strong><\/li>\n<li><strong><em>(d2, d3,\u00a0d1)<\/em><\/strong><\/li>\n<li><strong><em>(d3, d1,\u00a0d2)<\/em><\/strong><\/li>\n<li><strong><em>(d3, d2,\u00a0d1)<\/em><\/strong><\/li>\n<\/ul>\n<p><strong>Training Process:<\/strong><\/p>\n<ol>\n<li>\n<strong>Score Prediction<\/strong>: The model predicts a score for each document in the list, and the documents are ranked according to these scores.For example: <strong><em>s1 = f(q1,d1), s2 =\u00a0f(q1,d2)<\/em><\/strong>\n<\/li>\n<li>\n<strong>Ideal Ranking<\/strong>: The ideal ranking is calculated by sorting the documents based on their <strong>true relevance<\/strong>. For example, <strong><em>d1<\/em><\/strong> might be the most relevant, followed by <strong><em>d2<\/em><\/strong>, and then\u00a0<strong><em>d3.<\/em><\/strong>\n<\/li>\n<li>\n<strong>NDCG Calculation<\/strong>: NDCG is calculated for each permutation of the document list. It checks how close the predicted ranking is to the ideal ranking, considering both relevance and the positions of the documents.<\/li>\n<li>\n<strong>Penalizing Incorrect Rankings<\/strong>: If the predicted ranking differs from the ideal, the NDCG score will drop. For example, if the ideal ranking is <strong><em>(d1, d3, d2)<\/em><\/strong> but the model ranks <strong><em>(d2, d1, d3)<\/em><\/strong>, the NDCG score will be lower because the most relevant document (<strong><em>d1<\/em><\/strong>) isn\u2019t ranked at the\u00a0top.<\/li>\n<li>\n<strong>Gradient Calculation<\/strong>: The model calculates gradients based on how much the NDCG score would change if the order of documents was adjusted. These gradients guide the model on how to improve its predictions.<\/li>\n<\/ol>\n<p>This process helps the model learn to optimize the entire ranking list, improving the relevance of documents presented to\u00a0users.<\/p>\n<h3><strong>Summary<\/strong><\/h3>\n<p>When it comes to Learning to Rank, there\u2019s no one-size-fits-all approach. Pointwise models are super easy to set up and update, but they don\u2019t always take into account how documents relate to each other. That said, if you need something simple and fast, they\u2019re a great\u00a0option.<\/p>\n<p>On the other hand, <strong><em>pairwise<\/em><\/strong> and <strong><em>listwise<\/em><\/strong> methods are more powerful because they look at how documents compare to one another. But with that <strong>power comes more complexity<\/strong> \ud83d\ude1b, and listwise can be a real challenge because of its high complexity in training.<\/p>\n<p>Personally, I find the <strong><em>pairwise<\/em><\/strong> approach to be the sweet spot. It strikes a good balance between complexity and performance, making it ideal for many situations.<\/p>\n<p>At the end of the day, the method you choose really depends on your situation. How big and complicated is your dataset? Knowing the pros and cons of each method will help you pick the one that works best for what you\u2019re trying to\u00a0do.<\/p>\n<p>That\u2019s a wrap for today! Stay tuned for the next part, Until then happy ranking!\u00a0\ud83d\ude0a<\/p>\n<h4>References:<\/h4>\n<p><a href=\"https:\/\/www.microsoft.com\/en-us\/research\/uploads\/prod\/2016\/02\/MSR-TR-2010-82.pdf\">From RankNet to LambdaRank to LambdaMART: An Overview<\/a><br \/><a href=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2016\/02\/tr-2007-40.pdf\">Learning to Rank: From Pairwise Approach to Listwise Approach<\/a><br \/><a href=\"https:\/\/everdark.github.io\/k9\/notebooks\/ml\/learning_to_rank\/learning_to_rank.html\">Introduction to Learning to\u00a0Rank<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=cd5318f86e1b\" width=\"1\" height=\"1\" alt=\"\"><\/p>\n<hr>\n<p><a href=\"https:\/\/towardsdatascience.com\/ranking-basics-pointwise-pairwise-listwise-cd5318f86e1b\">Ranking Basics: Pointwise, Pairwise, Listwise<\/a> was originally published in <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a> on Medium, where people are continuing the conversation by highlighting and responding to this story.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Kunal Santosh Sawant<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/medium.com\/m\/global-identity-2?redirectUrl=https%3A%2F%2Ftowardsdatascience.com%2Franking-basics-pointwise-pairwise-listwise-cd5318f86e1b\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ranking Basics: Pointwise, Pairwise, Listwise Because thy neighbour matters Image taken from unsplash.com First, let\u2019s talk about where ranking comes into play. Ranking is a big deal in e-commerce and search applications\u200a\u2014\u200aessentially, any scenario where you need to organize documents based on a query. It\u2019s a little different from classic classification or regression problems. For [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,83,826,70,827,537],"tags":[829,828,590],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-data-science","category-learning-to-rank","category-machine-learning","category-ranking","category-recommendation-system","tag-documents","tag-label","tag-ranking"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/726"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}