<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Baptiste Rocca on Medium]]></title>
        <description><![CDATA[Stories by Baptiste Rocca on Medium]]></description>
        <link>https://medium.com/@baptiste.rocca?source=rss-20ad1309823a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GMEE-JrFBerusVnk-iKbZw.jpeg</url>
            <title>Stories by Baptiste Rocca on Medium</title>
            <link>https://medium.com/@baptiste.rocca?source=rss-20ad1309823a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 15:35:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baptiste.rocca/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to recommender systems]]></title>
            <link>https://medium.com/data-science/introduction-to-recommender-systems-6c66cf15ada?source=rss-20ad1309823a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c66cf15ada</guid>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Baptiste Rocca]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 23:39:46 GMT</pubDate>
            <atom:updated>2019-06-12T07:48:22.390Z</atom:updated>
            <content:encoded><![CDATA[<h4>Overview of some major recommendation algorithms.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kjouN-zV6BgpmCl5SnEjGQ.jpeg" /><figcaption>Credit: <a href="https://pixabay.com/fr/users/stocksnap-894430/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2596809">StockSnap</a> on <a href="https://pixabay.com/">Pixabay</a></figcaption></figure><p><em>This post was co-written with </em><a href="https://medium.com/u/b17ebd108358"><em>Joseph Rocca</em></a><em>.</em></p><h3>Introduction</h3><p>During the last few decades, with the rise of Youtube, Amazon, Netflix and many other such web services, recommender systems have taken more and more place in our lives. From e-commerce (suggest to buyers articles that could interest them) to online advertisement (suggest to users the right contents, matching their preferences), recommender systems are today unavoidable in our daily online journeys.</p><p>In a very general way, recommender systems are algorithms aimed at suggesting relevant items to users (items being movies to watch, text to read, products to buy or anything else depending on industries).</p><p>Recommender systems are really critical in some industries as they can generate a huge amount of income when they are efficient or also be a way to stand out significantly from competitors. As a proof of the importance of recommender systems, we can mention that, a few years ago, Netflix organised a challenges (the “Netflix prize”) where the goal was to produce a recommender system that performs better than its own algorithm with a prize of 1 million dollars to win.</p><p>In this article, we will go through different paradigms of recommender systems. For each of them, we will present how they work, describe their theoretical basis and discuss their strengths and weaknesses.</p><h4>Outline</h4><p>In the first section we are going to overview the two major paradigms of recommender systems : collaborative and content based methods. The next two sections will then describe various methods of collaborative filtering, such as user-user, item-item and matrix factorization. The following section will be dedicated to content based methods and how they work. Finally, we will discuss how to evaluate a recommender system.</p><h3>Collaborative versus content</h3><p>The purpose of a recommender system is to suggest relevant items to users. To achieve this task, there exist two major categories of methods : collaborative filtering methods and content based methods. Before digging more into details of particular algorithms, let’s discuss briefly these two main paradigms.</p><h4>Collaborative filtering methods</h4><p>Collaborative methods for recommender systems are methods that are based solely on the past interactions recorded between users and items in order to produce new recommendations. These interactions are stored in the so-called “user-item interactions matrix”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*m_Z6Da5FZ62KN2yH-x_GOQ@2x.png" /><figcaption>Illustration of the user-item interactions matrix.</figcaption></figure><p>Then, the main idea that rules collaborative methods is that these past user-item interactions are sufficient to detect similar users and/or similar items and make predictions based on these estimated proximities.<br> <br>The class of collaborative filtering algorithms is divided into two sub-categories that are generally called memory based and model based approaches. Memory based approaches directly works with values of recorded interactions, assuming no model, and are essentially based on nearest neighbours search (for example, find the closest users from a user of interest and suggest the most popular items among these neighbours). Model based approaches assume an underlying “generative” model that explains the user-item interactions and try to discover it in order to make new predictions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*yV3-_A1q37WheNJCvzutqg@2x.png" /><figcaption>Overview of the collaborative filtering methods paradigm.</figcaption></figure><p>The main advantage of collaborative approaches is that they require no information about users or items and, so, they can be used in many situations. Moreover, the more users interact with items the more new recommendations become accurate: for a fixed set of users and items, new interactions recorded over time bring new information and make the system more and more effective.</p><p>However, as it only consider past interactions to make recommendations, collaborative filtering suffer from the “cold start problem”: it is impossible to recommend anything to new users or to recommend a new item to any users and many users or items have too few interactions to be efficiently handled. This drawback can be addressed in different way: recommending random items to new users or new items to random users (random strategy), recommending popular items to new users or new items to most active users (maximum expectation strategy), recommending a set of various items to new users or a new item to a set of various users (exploratory strategy) or, finally, using a non collaborative method for the early life of the user or the item.</p><p>In the following sections, we will mainly present three classical collaborative filtering approaches: two memory based methods (user-user and item-item) and one model based approach (matrix factorisation).</p><h4>Content based methods</h4><p>Unlike collaborative methods that only rely on the user-item interactions, content based approaches use additional information about users and/or items. If we consider the example of a movies recommender system, this additional information can be, for example, the age, the sex, the job or any other personal information for users as well as the category, the main actors, the duration or other characteristics for the movies (items).</p><p>Then, the idea of content based methods is to try to build a model, based on the available “features”, that explain the observed user-item interactions. Still considering users and movies, we will try, for example, to model the fact that young women tend to rate better some movies, that young men tend to rate better some other movies and so on. If we manage to get such model, then, making new predictions for a user is pretty easy: we just need to look at the profile (age, sex, …) of this user and, based on this information, to determine relevant movies to suggest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*ReuY4yOoqKMatHNJupcM5A@2x.png" /><figcaption>Overview of the content based methods paradigm.</figcaption></figure><p>Content based methods suffer far less from the cold start problem than collaborative approaches: new users or items can be described by their characteristics (content) and so relevant suggestions can be done for these new entities. Only new users or items with previously unseen features will logically suffer from this drawback, but once the system old enough, this has few to no chance to happen.</p><p>Later in this post, we will further discuss content based approaches and see that, depending on our problem, various classification or regression models can be used, ranging from very simple to much more complex models.</p><h4>Models, bias and variance</h4><p>Let’s focus a bit more on the main differences between the previously mentioned methods. More especially let’s see the implication that the modelling level has on the bias and the variance.</p><p>In memory based collaborative methods, no latent model is assumed. The algorithms directly works with the user-item interactions: for example, users are represented by their interactions with items and a nearest neighbours search on these representations is used to produce suggestions. As no latent model is assumed, these methods have theoretically a low bias but a high variance.</p><p>In model based collaborative methods, some latent interaction model is assumed. The model is trained to reconstruct user-item interactions values from its own representation of users and items. New suggestions can then be done based on this model. The users and items latent representations extracted by the model have a mathematical meaning that can be hard to interpret for a human being. As a (pretty free) model for user-item interactions is assumed, this methods has theoretically a higher bias but a lower variance than methods assuming no latent model.</p><p>Finally, in content based methods some latent interaction model is also assumed. However, here, the model is provided with content that define the representation of users and/or items: for example, users are represented by given features and we try to model for each item the kind of user profile that likes or not this item. Here, as for model based collaborative methods, a user-item interactions model is assumed. However, this model is more constrained (because representation of users and/or items are given) and, so, the method tends to have the highest bias but the lowest variance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*rCK9VjrPgpHUvSNYw7qcuQ@2x.png" /><figcaption>Summary of the different types of recommender systems algorithms.</figcaption></figure><h3>Memory based collaborative approaches</h3><p>The main characteristics of user-user and item-item approaches it that they use only information from the user-item interaction matrix and they assume no model to produce new recommendations.</p><h4>User-user</h4><p>In order to make a new recommendation to a user, user-user method roughly tries to identify users with the most similar “interactions profile” (nearest neighbours) in order to suggest items that are the most popular among these neighbours (and that are “new” to our user). This method is said to be “user-centred” as it represent users based on their interactions with items and evaluate distances between users.</p><p>Assume that we want to make a recommendation for a given user. First, every user can be represented by its vector of interactions with the different items (“its line” in the interaction matrix). Then, we can compute some kind of “similarity” between our user of interest and every other users. That similarity measure is such that two users with similar interactions on the same items should be considered as being close. Once similarities to every users have been computed, we can keep the k-nearest-neighbours to our user and then suggest the most popular items among them (only looking at the items that our reference user has not interacted with yet).</p><p>Notice that, when computing similarity between users, the number of “common interactions” (how much items have already been considered by both users?) should be considered carefully! Indeed, most of the time, we want to avoid that someone that only have one interaction in common with our reference user could have a 100% match and be considered as being “closer” than someone having 100 common interactions and agreeing “only” on 98% of them. So, we consider that two users are similar if they have interacted with a lot of common items in the same way (similar rating, similar time hovering…).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*nmoJhj4LgqrbhFV1V4-ErA@2x.png" /><figcaption>Illustration of the user-user method. The same colour code will be used in the remaining of the post.</figcaption></figure><h4>Item-item</h4><p>To make a new recommendation to a user, the idea of item-item method is to find items similar to the ones the user already “positively” interacted with. Two items are considered to be similar if most of the users that have interacted with both of them did it in a similar way. This method is said to be “item-centred” as it represent items based on interactions users had with them and evaluate distances between those items.</p><p>Assume that we want to make a recommendation for a given user. First, we consider the item this user liked the most and represent it (as all the other items) by its vector of interaction with every users (“its column” in the interaction matrix). Then, we can compute similarities between the “best item” and all the other items. Once the similarities have been computed, we can then keep the k-nearest-neighbours to the selected “best item” that are new to our user of interest and recommend these items.</p><p>Notice that in order to get more relevant recommendations, we can do this job for more than only the user’s favourite item and consider the n preferred items instead. In this case, we can recommend items that are close to several of these preferred items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*Q3HCUZtlQP05Vp2U-19P6w@2x.png" /><figcaption>Illustration of the item-item method.</figcaption></figure><h4>Comparing user-user and item-item</h4><p>The user-user method is based on the search of similar users in terms of interactions with items. As, in general, every user have only interacted with a few items, it makes the method pretty sensitive to any recorded interactions (high variance). On the other hand, as the final recommendation is only based on interactions recorded for users similar to our user of interest, we obtain more personalized results (low bias).</p><p>Conversely, the item-item method is based on the search of similar items in terms of user-item interactions. As, in general, a lot of users have interacted with an item, the neighbourhood search is far less sensitive to single interactions (lower variance). As a counterpart, interactions coming from every kind of users (even users very different from our reference user) are then considered in the recommendation, making the method less personalised (more biased). Thus, this approach is less personalized than the user-user approach but more robust.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*J7bZ-K-6RwmwlYUqoXFOOQ@2x.png" /><figcaption>Illustration of the difference between item-item and user-user methods.</figcaption></figure><h4>Complexity and side effect</h4><p>One of the biggest flaw of memory based collaborative filtering is that they do not scale easily: generating a new recommendation can be extremely time consuming for big systems. Indeed, for systems with millions of users and millions of items, the nearest neighbours search step can become intractable if not carefully designed (KNN algorithm has a complexity of O(ndk) with n the number of users, d the number of items and k the number of considered neighbours). In order to make computations more tractable for huge systems, we can both take advantage of the sparsity of the interaction matrix when designing our algorithm or use approximate nearest neighbours methods (ANN).</p><p>In most of the recommendation algorithms, it is necessary to be extremely careful to avoid a “rich-get-richer” effect for popular items and to avoid getting users stuck into what could be called an “information confinement area”. In other words, we do not want that our system tend to recommend more and more only popular items as well as we do not want that our users only receive recommendations for items extremely close to the one they already liked with no chance to get to know new items they might like too (as these items are not “close enough” to be suggested). If, as we mentioned, these problems can arise in most of the recommendation algorithms, it is especially true for memory based collaborative ones. Indeed, with the lack of model “to regularise”, this kind of phenomenon can be accentuated and observed more frequently.</p><h3>Model based collaborative approaches</h3><p>Model based collaborative approaches only rely on user-item interactions information and assume a latent model supposed to explain these interactions. For example, matrix factorisation algorithms consists in decomposing the huge and sparse user-item interaction matrix into a product of two smaller and dense matrices: a user-factor matrix (containing users representations) that multiplies a factor-item matrix (containing items representations).</p><h4>Matrix factorisation</h4><p>The main assumption behind matrix factorisation is that there exists a pretty low dimensional latent space of features in which we can represent both users and items and such that the interaction between a user and an item can be obtained by computing the dot product of corresponding dense vectors in that space.</p><p>For example, consider that we have a user-movie rating matrix. In order to model the interactions between users and movies, we can assume that:</p><ul><li>there exists some features describing (and telling apart) pretty well movies.</li><li>these features can also be used to describe user preferences (high values for features the user likes, low values otherwise)</li></ul><p>However we don’t want to give explicitly these features to our model (as it could be done for content based approaches that we will describe later). Instead, we prefer to let the system discover these useful features by itself and make its own representations of both users and items. As they are learned and not given, extracted features taken individually have a mathematical meaning but no intuitive interpretation (and, so, are difficult, if not impossible, to understand as human). However, it is not unusual to ends up having structures emerging from that type of algorithm being extremely close to intuitive decomposition that human could think about. Indeed, the consequence of such factorisation is that close users in terms of preferences as well as close items in terms of characteristics ends up having close representations in the latent space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*E9EE5LXxty1EB8fn_s1jkQ@2x.png" /><figcaption>Illustration of the matrix factorization method.</figcaption></figure><h4>Mathematics of matrix factorisation</h4><p>In this subsection, we will give a simple mathematical overview of matrix factorization. More especially, we describe a classical iterative approach based on gradient descent that makes possible to obtain factorisations for very large matrices without loading all the data at the same time in computer’s memory.</p><p>Let’s consider an interaction matrix M (nxm) of ratings where only some items have been rated by each user (most of the interactions are set to None to express the lack of rating). We want to factorise that matrix such that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/97/1*2n4LR_G9S3aGZYDFCuNKpA@2x.png" /></figure><p>where X is the “user matrix” (nxl) whose rows represent the n users and where Y is the “item matrix” (mxl) whose rows represent the m items:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*t0C_FBvsYGGPxecMLrlVgA@2x.png" /></figure><p>Here l is the dimension of the latent space in which users and item will be represented. So, we search for matrices X and Y whose dot product best approximate the existing interactions. Denoting E the ensemble of pairs (i,j) such that M_ij is set (not None), we want to find X and Y that minimise the “rating reconstruction error”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*WDlTqvO5Kcc-CDkdKgLwvQ@2x.png" /></figure><p>Adding a regularisation factor and dividing by 2, we get</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*1r30m0-gsvc_W3OL8455zA@2x.png" /></figure><p>The matrices X and Y can then be obtained following a gradient descent optimisation process for which we can notice two things. First, the gradient do not have to be computed over all the pairs in E at each step and we can consider only a subset of these pairs so that we optimise our objective function “by batch”. Second, values in X and Y do not have to be updated simultaneously and the gradient descent can be done alternatively on X and Y at each step (doing so, we consider one matrix fixed and optimise for the other before doing the opposite at the next iteration).</p><p>Once the matrix has been factorised, we have less information to manipulate in order to make a new recommendation: we can simply multiply a user vector by any item vector in order to estimate the corresponding rating. Notice that we could also use user-user and item-item methods with these new representations of users and items: (approximate) nearest neighbours searches wouldn’t be done over huge sparse vectors but over small dense ones making some approximation techniques more tractable.</p><h4>Extensions of matrix factorisation</h4><p>We can finally notice that the concept of this basic factorisation can be extended to more complex models with, for example, more general neural network like “decomposition” (we cannot strictly speak about “factorisation” anymore). The first direct adaptation we can think of concerns boolean interactions. If we want to reconstruct boolean interactions, a simple dot product is not well adapted. If, however, we add a logistic function on top of that dot product, we get a model that takes its value in [0, 1] and, so, better fit the problem. In such case, the model to optimise is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*dCVFQWzIhtfl7FFZD5oidQ@2x.png" /></figure><p>with f(.) our logistic function. Deeper neural network models are often used to achieve near state of the art performances in complex recommender systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*nY12kY9YzaaAbeqjlRzSvg@2x.png" /><figcaption>Matrix factorization can be generalized with the use of a model on top of users and items embeddings.</figcaption></figure><h3>Content based approaches</h3><p>In the previous two sections we mainly discussed user-user, item-item and matrix factorisation approaches. These methods only consider the user-item interaction matrix and, so, belong to the collaborative filtering paradigm. Let’s now describe the content based paradigm.</p><h4>Concept of content-based methods</h4><p>In content based methods, the recommendation problem is casted into either a classification problem (predict if a user “likes” or not an item) or into a regression problem (predict the rating given by a user to an item). In both cases, we are going to set a model that will be based on the user and/or item features at our disposal (the “content” of our “content-based” method).</p><p>If our classification (or regression) is based on users features, we say the approach is item-centred: modelling, optimisations and computations can be done “by item”. In this case, we build and learn one model by item based on users features trying to answer the question “what is the probability for each user to like this item?” (or “what is the rate given by each user to this item?”, for regression). The model associated to each item is naturally trained on data related to this item and it leads, in general, to pretty robust models as a lot of users have interacted with the item. However, the interactions considered to learn the model come from every users and even if these users have similar characteristic (features) their preferences can be different. This mean that even if this method is more robust, it can be considered as being less personalised (more biased) than the user-centred method thereafter.</p><p>If we are working with items features, the method is then user-centred: modelling, optimisations and computations can be done “by user”. We then train one model by user based on items features that tries to answer the question “what is the probability for this user to like each item?” (or “what is the rate given by this user to each item?”, for regression). We can then attach a model to each user that is trained on its data: the model obtained is, so, more personalised than its item-centred counterpart as it only takes into account interactions from the considered user. However, most of the time a user has interacted with relatively few items and, so, the model we obtain is a far less robust than an item-centred one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*DSMBC24ZnJNViN7ownzZAA@2x.png" /><figcaption>Illustration of the difference between item-centred and user-centred content based methods.</figcaption></figure><p>From a practical point of view, we should underline that, most of the time, it is much more difficult to ask some information to a new user (users do not want to answer too much questions) than to ask lot of information about a new item (people adding them have an interest in filling these information in order to make their items recommended to the right users). We can also notice that, depending on the complexity of the relation to express, the model we build can be more or less complex, ranging from basic models (logistic/linear regression for classification/regression) to deep neural networks. Finally, let’s mention that content based methods can also be neither user nor item centred: both informations about user and item can be used for our models, for example by stacking the two features vectors and making them go through a neural network architecture.</p><h4>Item-centred Bayesian classifier</h4><p>Let’s first consider the case of an item-centred classification: for each item we want to train a Bayesian classifier that takes user features as inputs and output either “like” or “dislike”. So, to achieve the classification task, we want to compute</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*eP8j7cw-sG8EleomwQPbZg@2x.png" /></figure><p>the ratio between the probability for a user with given features to like the considered item and its probability to dislike it. This ratio of conditional probabilities that defines our classification rule (with a simple threshold) can be expressed following the Bayes formula</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*4-k2KU3jD5Coru-zVZVDbg@2x.png" /></figure><p>where</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*3i8qchbPh86sX5gQlvzPCA@2x.png" /></figure><p>are priors computed from the data whereas</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*cdyoUd5jlC1SOmEYwYfn9w@2x.png" /></figure><p>are likelihoods assumed to follow Gaussian distributions with parameters to be determined also from data. Various hypothesis can be done about the covariance matrices of these two likelihood distributions (no assumption, equality of matrices, equality of matrices and features independence) leading to various well known models (quadratic discriminant analysis, linear discriminant analysis, naive Bayes classifier). We can underline once more that, here, likelihood parameters have to be estimated only based on data (interactions) related to the considered item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*xOm9KhtWr_NrybG8WS93ZQ@2x.png" /><figcaption>Illustration of the item-centred content based Bayesian classifier.</figcaption></figure><h4>User-centred linear regression</h4><p>Let’s now consider the case of a user-centred regression: for each user we want to train a simple linear regression that takes item features as inputs and output the rating for this item. We still denote M the user-item interaction matrix, we stack into a matrix X row vectors representing users coefficients to be learned and we stack into a matrix Y row vectors representing items features that are given. Then, for a given user i, we learn the coefficients in X_i by solving the following optimisation problem</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*4HGNnVq0jSQPoxMAIu-RpQ@2x.png" /></figure><p>where one should keep in mind that i is fixed and, so, the first summation is only over (user, item) pairs that concern user i. We can observe that if we solve this problem for all the users at the same time, the optimisation problem is exactly the same as the one we solve in “alternated matrix factorisation” when we keep items fixed. This observation underlines the link we mentioned in the first section: model based collaborative filtering approaches (such as matrix factorisation) and content based methods both assume a latent model for user-item interactions but model based collaborative approaches have to learn latent representations for both users and items whereas content-based methods build a model upon human defined features for users and/or items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*trc0BNNW1J5_1CXyIJuR9w@2x.png" /><figcaption>Illustration of the user-centred content based regression.</figcaption></figure><h3>Evaluation of a recommender system</h3><p>As for any machine learning algorithm, we need to be able to evaluate the performances of our recommender systems in order to decide which algorithm fit the best our situation. Evaluation methods for recommender systems can mainly be divided in two sets: evaluation based on well defined metrics and evaluation mainly based on human judgment and satisfaction estimation.</p><h4>Metrics based evaluation</h4><p>If our recommender system is based on a model that outputs numeric values such as ratings predictions or matching probabilities, we can assess the quality of these outputs in a very classical manner using an error measurement metric such as, for example, mean square error (MSE). In this case, the model is trained only on a part of the available interactions and is tested on the remaining ones.</p><p>Still if our recommender system is based on a model that predicts numeric values, we can also binarize these values with a classical thresholding approach (values above the threshold are positive and values bellow are negative) and evaluate the model in a more “classification way”. Indeed, as the dataset of user-item past interactions is also binary (or can be binarized by thresholding), we can then evaluate the accuracy (as well as the precision and the recall) of the binarized outputs of the model on a test dataset of interactions not used for training.</p><p>Finally, if we now consider a recommender system not based on numeric values and that only returns a list of recommendations (such as user-user or item-item that are based on a knn approach), we can still define a precision like metric by estimating the proportion of recommended items that really suit our user. To estimate this precision, we can not take into account recommended items that our user has not interacted with and we should only consider items from the test dataset for which we have a user feedback.</p><h4>Human based evaluation</h4><p>When designing a recommender system, we can be interested not only to obtain model that produce recommendations we are very sure about but we can also expect some other good properties such as diversity and explainability of recommendations.</p><p>As mentioned in the collaborative section, we absolutely want to avoid having a user being stuck in what we called earlier an information confinement area. The notion of “serendipity” is often used to express the tendency a model has or not to create such a confinement area (diversity of recommendations). Serendipity, that can be estimated by computing the distance between recommended items, should not be too low as it would create confinement areas, but should also not be too high as it would mean that we do not take enough into account our users interests when making recommendations (exploration vs exploitation). Thus, in order to bring diversity in the suggested choices, we want to recommend items that both suit our user very well and that are not too similar from each others. For example, instead of recommending a user “Start Wars” 1, 2 and 3, it seems better to recommend “Star wars 1”, “Start trek into darkness” and “Indiana Jones and the raiders of the lost ark”: the two later may be seen by our system as having less chance to interest our user but recommending 3 items that look too similar is not a good option.</p><p>Explainability is another key point of the success of recommendation algorithms. Indeed, it has been proven that if users do not understand why they had been recommended as specific item, they tend to loose confidence into the recommender system. So, if we design a model that is clearly explainable, we can add, when making recommendations, a little sentence stating why an item has been recommended (“people who liked this item also liked this one”, “you liked this item, you may be interested by this one”, …).</p><p>Finally, on top of the fact that diversity and explainability can be intrinsically difficult to evaluate, we can notice that it is also pretty difficult to assess the quality of a recommendation that do not belong to the testing dataset: how to know if a new recommendation is relevant before actually recommending it to our user? For all these reasons, it can sometimes be tempting to test the model in “real conditions”. As the goal of the recommender system is to generate an action (watch a movie, buy a product, read an article etc…), we can indeed evaluate its ability to generate the expected action. For example, the system can be put in production, following an A/B testing approach, or can be tested only on a sample of users. Such processes require, however, having a certain level of confidence in the model.</p><h3>Takeaways</h3><p>The main takeaways of this article are:</p><ul><li>recommendation algorithms can be divided in two great paradigms: collaborative approaches (such as user-user, item-item and matrix factorisation) that are only based on user-item interaction matrix and content based approaches (such as regression or classification models) that use prior information about users and/or items</li><li>memory based collaborative methods do not assume any latent model and have then low bias but high variance ; model based collaborative approaches assume a latent interactions model that needs to learn both users and items representations from scratch and have, so, a higher bias but a lower variance ; content based methods assume a latent model build around users and/or items features explicitly given and have, thus, the highest bias and lowest variance</li><li>recommender systems are more and more important in many big industries and some scales considerations have to be taken into account when designing the system (better use of sparsity, iterative methods for factorisation or optimisation, approximate techniques for nearest neighbours search…)</li><li>recommender systems are difficult to evaluate: if some classical metrics such that MSE, accuracy, recall or precision can be used, one should keep in mind that some desired properties such as diversity (serendipity) and explainability can’t be assessed this way ; real conditions evaluation (like A/B testing or sample testing) is finally the only real way to evaluate a new recommender system but requires a certain confidence in the model</li></ul><p>We should notice that we have not discussed hybrid approaches in this introductory post. These methods, that combine collaborative filtering and content based approaches, achieves state-of-the-art results in many cases and are, so, used in many large scale recommender systems nowadays. The combination made in hybrid approaches can mainly take two forms: we can either train two models independently (one collaborative filtering model and one content based model) and combine their suggestions or directly build a single model (often a neural network) that unify both approaches by using as inputs prior information (about user and/or item) as well as “collaborative” interactions information.</p><p>As we mentioned in the introduction of this post, recommender systems are becoming essential in many industries and, hence, have received always more attention in the recent years. In this article, we have introduced basics notions required for a better understanding of the questions related to these systems, but we highly encourage interested readers to explore this field further… without giving, ironically, any specific reading recommendations!</p><p>Thanks for reading!</p><p>Last articles written with <a href="https://medium.com/u/b17ebd108358">Joseph Rocca</a>:</p><ul><li><a href="https://towardsdatascience.com/ensemble-methods-bagging-boosting-and-stacking-c9214a10a205">Ensemble methods: bagging, boosting and stacking</a></li><li><a href="https://towardsdatascience.com/brief-introduction-to-markov-chains-2c8cab9c98ab">Introduction to Markov chains</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c66cf15ada" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/introduction-to-recommender-systems-6c66cf15ada">Introduction to recommender systems</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling imbalanced datasets in machine learning]]></title>
            <link>https://medium.com/data-science/handling-imbalanced-datasets-in-machine-learning-7a0e84220f28?source=rss-20ad1309823a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a0e84220f28</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[getting-started]]></category>
            <dc:creator><![CDATA[Baptiste Rocca]]></dc:creator>
            <pubDate>Sun, 27 Jan 2019 22:37:00 GMT</pubDate>
            <atom:updated>2021-02-01T15:54:33.091Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/getting-started">Getting Started</a></h4><h4>What should and should not be done when facing an imbalanced classes problem?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zC1UpvCJV6EF2g0mttq9dA.jpeg" /></figure><p><em>This post was co-written with </em><a href="https://medium.com/u/b17ebd108358"><em>Joseph Rocca</em></a><em>.</em></p><h3>Introduction</h3><p>Suppose that you are working in a given company and you are asked to create a model that, based on various measurements at your disposal, predicts whether a product is defective or not. You decide to use your favourite classifier, train it on the data and voila : you get a 96.2% accuracy ! <br>Your boss is astonished and decides to use your model without any further tests. A few weeks later he enters your office and underlines the uselessness of your model. Indeed, the model you created has not found any defective product from the time it has been used in production.<br>After some investigations, you find out that there is only around 3.8% of the product made by your company that are defective and your model just always answers “not defective”, leading to a 96.2% accuracy. The kind of “naive” results you obtained is due to the imbalanced dataset you are working with. The goal of this article is to review the different methods that can be used to tackle classification problems with imbalanced classes.</p><h4>Outline</h4><p>First we will give an overview of different evaluation metrics that can help to detect “naive behaviours”. We will then discuss a whole bunch of methods consisting in reworking the dataset and show that these methods can be misleading. Finally, we will show that reworking the problem is, most of the time, the best way to proceed.</p><p>Some subsections indicated by a (∞) symbol contains more mathematical details and can be skipped without impacting the overall comprehension of this post. Notice also that in most of what follows, we will consider two classes classification problems but reasoning can easily be extended to multi-classes cases.</p><h3>Detect a “naive behaviour”</h3><p>In this first section, we would like to remind different ways to evaluate a trained classifier in order to be sure to detect any kind of “naive behaviour”. As we saw in the introduction’s example, accuracy, while being an important and unavoidable metric, can be misleading and therefore should be used cautiously and alongside other metrics. Let’s see what other tools can then be used.</p><h4>Confusion matrix, precision, recall and F1</h4><p>A good and yet simple metric that should always be used when dealing with classification problem is the confusion matrix. This metric gives an interesting overview of how well a model is doing. Thus, it is a great starting point for any classification model evaluation. We summarise most of the metrics that can be derived from the confusion matrix in the following graphic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yslau43QN1pEU4jkGiq-pw.png" /><figcaption>The confusion matrix and the metrics that can be derived from it.</figcaption></figure><p>Let us give a short description of these metrics. The accuracy of the model is basically the total number of correct predictions divided by total number of predictions. The precision of a class define how trustable is the result when the model answer that a point belongs to that class. The recall of a class expresses how well the model is able to detect that class. The F1 score of a class is given by the harmonic mean of precision and recall (2×precision×recall / (precision + recall)), it combines precision and recall of a class in one metric.</p><p>For a given class, the different combinations of recall and precision have the following meanings :</p><ul><li>high recall + high precision : the class is perfectly handled by the model</li><li>low recall + high precision : the model can’t detect the class well but is highly trustable when it does</li><li>high recall + low precision : the class is well detected but the model also include points of other classes in it</li><li>low recall + low precision : the class is poorly handled by the model</li></ul><p>In our introductory example, we have the following confusion matrix for 10000 products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z_C1mS6HpRZ8rwNAn7bcDQ.png" /><figcaption>The confusion matrix of our introductory example. Notice that the “defective” precision can’t be computed.</figcaption></figure><p>The accuracy is 96.2% as said earlier. The non defective class precision is 96.2% and the defective class precision is not computable. The recall of the non defective class is 1.0 which is perfect (all the non defective products have been labelled as such). But the recall of the defective class is 0.0 which is the worse case (no defective products were detected). Thus, we can conclude our model is not doing well <strong>for this class</strong>. The F1 score is not computable for the defective products and is 0.981 for the non defective products. In this example, looking at the confusion matrix could have led to re-think our model or our goal (as we will see in the following sections). It could have prevented using a useless model.</p><h4>ROC and AUROC</h4><p>Another interesting metric is the ROC curve (standing for Receiver Operating Characteristic), defined with respect to a given class (that we will denote C in the following).</p><p>Suppose that for a given point x, we have a model that outputs the probability that this point belongs to C: P(C | x). Based on this probability, we can define a decision rule that consists in saying that x belongs to class C if and only if P(C | x)≥T, where T is a given threshold defining our decision rule. If T=1, a point is labelled as belonging to C only if the model is 100% confident it does. If T=0, every points are labelled as belonging to C.</p><p>Each value of the threshold T generates a point (false positive, true positive) and, then, the ROC curve is the curve described by the ensemble of points generated when T varies from 1 to 0. This curve starts at point (0,0), ends at point (1,1) and is increasing. A good model will have a curve that increases quickly from 0 to 1 (meaning that only a little precision has to be sacrificed to get a high recall).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*thHBCWlaKWIkouryKBh6Wg.jpeg" /><figcaption>Illustration of possible ROC curves depending on the effectiveness of the model. On the left, the model has to sacrifice a lot of precision to get a high recall. On the right, the model is highly effective: it can reach a high recall while keeping a high precision.</figcaption></figure><p>Based on the ROC curve, we can build another metric, easier to use, to evaluate the model: the AUROC which is the Area Under the ROC curve. AUROC acts a little bit as a scalar value that summarises the entire ROC curve. As it can be seen, the AUROC tend towards 1.0 for the best case and towards 0.5 for the worst case.<br>Here again, a good AUROC score means that the model we are evaluating does not sacrifice a lot of precision to get a good recall on the observed class (often the minority class).</p><h3>What really is the problem?</h3><p>Before trying to tackle the problem, let’s try to better understand it. For this, we are going to consider a very simple example that will allow us both to review quickly some basic aspects of a two classes classification and to better grasp the fundamental problem of imbalanced dataset. This example will also be used in the following sections.</p><h4>An imbalanced example</h4><p>Let’s suppose that we have two classes: C0 and C1. Points from the class C0 follow a one dimensional Gaussian distribution of mean 0 and variance 4. Points from the class C1 follow a one dimensional Gaussian distribution of mean 2 and variance 1. Suppose also that in our problem the class C0 represent 90% of the dataset (and, so, the class C1 represent the remaining 10%). In the following picture, we have depicted a representative dataset containing 50 points along with the theoretical distributions of both classes in the right proportions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XASKuQuNP6ynLvKBN3TkyQ.jpeg" /><figcaption>Illustration of our imbalanced example. Dotted lines represent the probability densities of each class independently. Solid lines also take into account the proportions.</figcaption></figure><p>In this example we can see that the curve of the C0 class is always above the curve of the C1 class and, so, for any given point the probability that this point was drawn from class C0 is always greater than the probability it was drawn from class C1. Mathematically, using basic Bayes rule, we can write</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*eMjHZOrPO4XfMBNrRaAQPQ@2x.png" /></figure><p>where we can clearly see the effect of the priors and how it can lead to a situation where a class is always more likely than the other.</p><p>All this implies that even from a perfect theoretical point of view we know that if we had to train a classifier on these data, <strong>the accuracy of the classifier would be maximal when always answering C0</strong>. So, if the goal is to train a classifier to get the best possible accuracy then it should not be seen as a problem but just as a fact: with these features, the best we can do (in terms of accuracy) is to always answer C0. We have to accept it.</p><h4>About the separability</h4><p>In the given example, we can observe that the two classes are not well separable (they are not far apart from each other). However, we can notice that facing an imbalanced dataset doesn’t necessarily mean that the two classes are not well separable and, so, that the classifier can’t do a pretty good job on the minority class. For example, consider that we still have two classes C0 (90%) and C1 (10%). Data in C0 follow a one dimensional Gaussian distribution of mean 0 and variance 4 whereas data in C1 follow a one dimensional Gaussian distribution of mean 10 and variance 1. If we plot the data as before, we then have</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1n5UsEdkuis44B6uC2cQg.jpeg" /><figcaption>In our Gaussian example, if the means are different enough with respect to the variances, even imbalanced classes can be well separable.</figcaption></figure><p>Here we see that contrarily to the previous case the C0 curve is not always above the C1 curve and, so, there are points that are more likely to be drawn from class C1 than from class C0. In this case, the two classes are separated enough to compensate the imbalance: a classifier will not necessarily answer C0 all the time.</p><h4>Theoretical minimal error probability (∞)</h4><p>Finally, we should keep in mind that a classifier has a theoretical minimal error probability. For a classifier of this kind (one feature, two classes), we can mention that, graphically, the theoretical minimal error probability is given by the area under the minimum of the two curves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4YVk2qmNam23rcX1xuyx7Q.jpeg" /><figcaption>Illustration of the theoretical minimal error for different degree of separability of two classes.</figcaption></figure><p>We can recover this intuition mathematically. Indeed, from a theoretical point of view, the best possible classifier will choose for each point x the most likely of the two classes. It naturally implies that for a given point x, the best theoretical error probability is given by the less likely of these two classes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*bqEaI884GeSVJ03uugOmaQ@2x.png" /></figure><p>Then we can express the overall error probability</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*KctgrhvfvKNEllf-q9n7hQ@2x.png" /></figure><p>Which is the area under the min of the two curves represented above.</p><h3>Reworking the dataset is not always a solution</h3><p>To begin, the very first possible reaction when facing an imbalanced dataset is to consider that data are not representative of the reality: if so, we assume that real data are almost balanced but that there is a proportions bias (due to the gathering method, for example) in the collected data. In this case, it is almost mandatory to try collecting more representative data.<br>Let’s see, now, what can be done when the dataset is imbalanced because reality is so. In the the next two subsections we present some methods that are often mentioned to tackle imbalanced classes and that deal with the dataset itself. In particular, we discuss the risks related to undersampling, oversampling and generating synthetic data as well as the benefits of getting more features.</p><h4>Undersampling, oversampling and generating synthetic data</h4><p>These methods are often presented as great ways to balance the dataset before fitting a classifier on it. In a few words, these methods act on the dataset as follows:</p><ul><li>undersampling consists in sampling from the majority class in order to keep only a part of these points</li><li>oversampling consists in replicating some points from the minority class in order to increase its cardinality</li><li>generating synthetic data consists in creating new synthetic points from the minority class (see SMOTE method for example) to increase its cardinality</li></ul><p>All these approaches aim at rebalancing (partially or fully) the dataset. But should we rebalance the dataset to have as much data of both classes ? Or should the majority class stay the most represented ? If so, in what proportions should we rebalance ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eylA4rNzLooyaA4hOmU3xQ.jpeg" /><figcaption>Illustration of the effect that different degrees of majority class undersampling have on the model decisions.</figcaption></figure><p>When using a resampling method (for example to get as much data from C0 than from C1), <strong>we show the wrong proportions of the two classes to the classifier during the training</strong>. The classifier learned this way will then have a lower accuracy on the future real test data than the classifier trained on the unchanged dataset. Indeed, the true proportions of classes are important to know for classifying a new point and that information has been lost when resampling the dataset.</p><p>So, if these methods have not to be completely rejected, they should be used cautiously: it can lead to a relevant approach if new proportions are chosen with purpose (we will see that in the next section), but it can also be a nonsense to just rebalance the classes without any further thoughts about the problem. To conclude this subsection, let’s say that <strong>modifying the dataset with resampling-like methods is changing the reality, so it requires to be careful </strong>and to have in mind what it means for the outputted results of our classifier.</p><h4>Getting additional features</h4><p>We discussed in the previous subsection the fact that resampling the training dataset (modifying the classes proportions) can be or not a good idea depending on the real purpose of the classifier. We saw in particular that if the two classes are imbalanced, not well separable and that we target a classifier with the best possible accuracy, then getting a classifier that always answer the same class is not necessarily a problem but just a fact: there is nothing better to do with these variables.</p><p>However, it remains possible to obtain better results in terms of accuracy by enriching the dataset with an additional feature (or more). Let’s go back to our first example where classes were not well separable: maybe can we find a new additional feature that can help distinguish between the two classes and, so, improve the classifier accuracy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_wRI5bHQ1GhqbLtJXfsnow.jpeg" /><figcaption>Looking for additional features can help separate two classes that were not initially separable.</figcaption></figure><p>Compared to the approaches mentioned in the previous subsection that suggest to change the reality of data, this approach that consists in enriching data with more information from the reality is a far better idea when it is possible.</p><h3>Reworking the problem is better</h3><p>Up to now the conclusion is pretty disappointing: if the dataset is representative of the true data, if we can’t get any additional feature and if we target a classifier with the best possible accuracy, then a “naive behaviour” (answering always the same class) is not necessarily a problem and should just be accepted as a fact (if the naive behaviour is not due to the limited capacity of the chosen classifier, of course).</p><p>So what if we are still unhappy with these results? In this case, it means that, in one way or another, <strong>our problem is not well stated</strong> (otherwise we should accept results as they are) and that we should rework it in order to get more satisfying results. Let’s see an example.</p><h4>Cost-based classification</h4><p>The feeling that obtained results are not good can come from the fact that the objective function was not well defined. Up to now, we have assumed that we target a classifier with high accuracy, assuming at the same time that both kinds of errors (“false positive” and “false negative”) have the same cost. In our example it means we assumed that predicting C0 when true label is C1 is as bad as predicting C1 when true label is C0. Errors are then symmetric.</p><p>Let’s consider our introductory example with defective (C1) and not defective (C0) products. In this case, we can imagine that not detecting a defective product will cost more to the company (customer service costs, possible juridical costs if dangerous defects, …) than wrongly labelling a not defective product as defective (production cost lost). Now, predicting C0 when true label is C1 is far worse than predicting C1 when true label is C0. Errors are no longer symmetric.</p><p>Consider then more particularly that we have the following costs:</p><ul><li>predicting C0 when true label is C1 costs P01</li><li>predicting C1 when true label is C0 costs P10 (with 0 &lt; P10 &lt;&lt; P01)</li></ul><p>Then, we can redefine our objective function: we don’t target the best accuracy anymore but we look for the lower prediction cost instead.</p><h4>Theoretical minimal cost (∞)</h4><p>From a theoretical point of view, we don’t want to minimise the error probability defined above but the expected prediction cost given by</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*GxwQDk7DQHfRXGfC4e6F5g@2x.png" /></figure><p>where C(.) defines the classifier function. So, if we want to minimise the expected prediction cost, the theoretical best classifier C(.) minimises</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*THszxi6D1k1RhfOMRn6jPg@2x.png" /></figure><p>or equivalently, dividing by the density of x, C(.) minimises</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*Eo0TdLVh7Aj-ct4vaHICXA@2x.png" /></figure><p>So, with this objective function, the best classifier from a theoretical point of view will then be such that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*C0ORH4vEA1eQ3k0DMH5Ubw@2x.png" /></figure><p>Notice that we recover the expression of the “classic” classifier (focus on accuracy) when costs are equal.</p><h4>Probability threshold</h4><p>One first possible way to take into account the cost in our classifier is to do it after the training. The idea is, first, to train a classifier the basic way to output the following probabilities</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*ED-SlbziUZWDv9TSGqreVQ@2x.png" /></figure><p>without assuming any costs. Then, the predicted class will be C0 if</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*Zct_ayV-0Pe94TXFAOMy6Q@2x.png" /></figure><p>and C1 otherwise.</p><p>Here, it doesn’t matter which classifier we are using as long as it outputs the probability of each class for a given point. In our main example, we can fit a Bayes classifier on our data and we can then reweight the obtained probabilities to adjust the classifier with the costs errors as described.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lcVyEbP7DZb86HE8MmFtTw.jpeg" /><figcaption>Illustration of the probability threshold approach: the outputted probabilities are reweighted such that costs are taken into account in the final decision rule.</figcaption></figure><h4>Classes reweight</h4><p>The idea of class reweight is to take into account the asymmetry of cost errors directly during the classifier training. Doing so, the outputted probabilities for each class will already embed the cost error information and could then be used to define a classification rule with a simple 0.5 threshold.</p><p>For some models (for example Neural Network classifiers), taking the cost into account during the training can consist in adjusting the objective function. We still want our classifier to output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*MRIenuGmEohCT957HCQtqA@2x.png" /></figure><p>but this time it is trained such as to minimise the following cost function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*5Pcz-3yfbOgHwvamuudGtQ@2x.png" /></figure><p>For some other models (for example Bayes classifier), resampling methods can be used to bias the classes proportions such that to enter the cost error information inside the classes proportions. If we consider the costs P01 and P10 (such that P01 &gt; P10), we can either:</p><ul><li>oversample the minority class by a factor P01/P10 (cardinality of the minority class should be multiplied by P01/P10)</li><li>undersample the majority class by a factor P10/P01 (cardinality of the majority class should be multiplied by P10/P01)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aztqu436sJ4ai-c4OAqFlw.jpeg" /><figcaption>Illustration of the class reweight approach: the majority class is undersampled with a proportion that is chosen carefully to introduce the cost information directly inside the class proportions.</figcaption></figure><h3>Takeaways</h3><p>The main takeaways of this article are:</p><ul><li>whenever using a machine learning algorithm, evaluation metrics for the model have to be chosen cautiously: we must use the metrics that gives us the best overview of how well our model is doing with regards to our goals</li><li>when dealing with an imbalanced dataset, if classes are not well separable with the given variables and if our goal is to get the best possible accuracy, the best classifier can be a “naive” one that always answer the majority class</li><li>resampling methods can be used but have to be thought carefully: they should not be used as stand alone solutions but have to be coupled with a rework of the problem to serve a specific goal</li><li>reworking the problem itself is often the best way to tackle an imbalanced classes problem: the classifier and the decision rule have to be set with respect to a well chosen goal that can be, for example, minimising a cost</li></ul><p>We should notice that we have not discussed at all techniques like “stratified sampling” that can be useful when batch training a classifier. When facing an imbalanced classes problem, such techniques ensure more stability during the training (by removing the proportions variance inside batches).</p><p>Finally, let’s say that the main keyword of this article is “goal”. Knowing exactly what you want to obtain will help overcome imbalanced dataset problems and will ensure having the best possible results. Defining the goal perfectly should always be the first thing to do and is the starting point of any choice that have to be done in order to create a machine learning model.</p><p>Thanks for reading!</p><p>Last article written with <a href="https://medium.com/u/b17ebd108358">Joseph Rocca</a>:</p><p><a href="https://towardsdatascience.com/understanding-generative-adversarial-networks-gans-cd6e4651a29">Understanding Generative Adversarial Networks (GANs)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a0e84220f28" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/handling-imbalanced-datasets-in-machine-learning-7a0e84220f28">Handling imbalanced datasets in machine learning</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>