Tuesday, September 8, 2020

Using nearest neighbors to predict user behavior

man and woman listening to music on the phone

A typical example of an application of the nearest neighbor method is predicting user behavior in AI applications such as recommendation systems.

The idea is to use the very simple principle that users with similar past behavior tend to have similar future behavior. Imagine a music recommendation system that collects data about users’ listening behavior. Let’s say you have listened to 1980s disco music (just for the sake of argument). One day, the service provider gets their hands on a hard-to-find 1980 disco classic, and adds it into the music library. The system now needs to predict whether you will like it or not. One way of doing this is to use information about the genre, the artist, and other metadata, entered by the good people of the service provider. However, this information is relatively scarce and coarse and it will only be able to give rough predictions.

What current recommendation systems use instead of the manually entered metadata, is something called collaborative filtering. The collaborative aspect of it is that it uses other users’ data to predict your preferences. The word “filter” refers to the fact that you will be only recommended content that passes through a filter: content that you are likely to enjoy will pass, other content will not (these kind of filters may lead to the so called filter bubbles.

Now let’s say that other users who have listened to 80s disco music enjoy the new release and keep listening to it again and again. The system will identify the similar past behavior that you and other 80s disco fanatics share, and since other users like you enjoy the new release, the system will predict that you will too. Hence it will show up at the top of your recommendation list. In an alternative reality, maybe the added song is not so great and other users with similar past behavior as yours don't really like it. In that case, the system wouldn't bother recommending it to you, or at least it wouldn't be at the top of the list of recommendations for you.

Let's build a simple recommendation system for an online shopping application where the users' purchase history is recorded and used to predict which products the user is likely to buy next.

We have data from six users. For each user, we have recorded their recent shopping history of four items and the item they bought after buying these four items:



UserShopping HistoryPurchase
Sanniboxing glovesMoby Dick (novel)headphonessunglassescoffee beans
Jounit-shirtcoffee beanscoffee makercoffee beanscoffee beans
Janinasunglassessneakerst-shirtsneakersragg wool socks
Henrik2001: A Space Odyssey (dvd)headphonest-shirtboxing glovesflip flops
Villet-shirtflip flopssunglassesMoby Dick (novel)sunscreen
TeemuMoby Dick (novel)coffee beans2001: A Space Odyssey (dvd)headphonescoffee beans

The most recent purchase is the one in the rightmost column, so for example, after buying a t-shirt, flip flops, sunglasses, and Moby Dick (novel), Ville bought sunscreen. Our hypothesis is that after buying similar items, other users are also likely to buy sunscreen.

To apply the nearest neighbor method, we need to define what we mean by nearest. This can be done in many different ways, some of which work better than others. Let’s use the shopping history to define the similarity (“nearness”) by counting how many of the items have been purchased by both users.

For example, users Ville and Henrik have both bought a t-shirt, so their similarity is 1. Note that flip flops doesn't count because we don't include the most recent purchase when calculating the similarity — it is reserved for another purpose.

Our task is to predict the next purchase of customer Travis who has bought the following products:


UserShopping HistoryPurchase
Travisgreen teat-shirtsunglassesflip flops?

You can think of Travis being our test data, and the above six users make our training data.

Proceed as follows:

  1. Calculate the similarity of Travis relative to the six users in the training data (done by adding together the number of similar purchases by the users).
  2. Having calculated the similarities, identify the user who is most similar to Travis by selecting the largest of the calculated similarities.
  3. Predict what Travis is likely to purchase next by looking at the most recent purchase (the rightmost column in the table) of the most similar user from the previous step.
When you calculate the similarities between Travis and all the other users, Ville and Travis will have the largest similarity with a similarity of 3. Since Ville's latest purchase was sunscreen, we will recommend it also to Travis.

In the above example, we only had six users’ data and our prediction was probably very unreliable. However, online shopping sites often have millions of users, and the amount of data they produce is massive. In many cases, there are a horde of users whose past behavior is very similar to yours, and whose purchase history gives a pretty good indication of your interests.

These predictions can also be self-fulfilling prophecies in the sense that you are more likely to buy a product if it is recommended to you by the system, which makes it tricky to evaluate how well they actually work. The same kind of recommendation systems are also used to recommend music, movies, news, and social media content to users. In the context of news and social media, filters created by such systems can lead to filter bubbles.

Share:

0 comments:

Post a Comment