I am working on creating a recommendation system which suggests product for the user, based on the other user's data from the same region.

My dataset is as below

UserId    Product    Region    Rating
  1         A         R1        1.23
  2         A         R1        1.23
  3         A         R1        1.24  
  4         B         R1        1.45
  5         B         R1        1.23

I am using NearestNeighbour algorithm to find the k nearest neighbors

First for the preparation step, I did get dummies

dummies = pd.get_dummies(df)

This will one hot the columns (userId and Product)

Applying NearestNeighbour

Not the exact code.. Just writing a part of it.

neigh = NearestNeighbors(algorithm="euclidean")

Getting 5 nearest neighbors using kneighbours method

neigh.kneighbors(input_1,5, return_distance=True)

I passed first row of dummies as input_1.

I did receive the result. But the recommendations were row index [2,3 5]

Which makes sense too. The region, rating, and product are almost the same, and hence it returns the index 2, 3 and 5.

In this case, First two neighbours were the same product(Product A). only the third one was the different product (Product B).

In my dataset I have more than 60k rows. Even with k=50, I am getting all the index with the same product.

I tried another approach,

After generating dummies, I took the first line (for input) and deleted all the rows containing product A related, and then ran the fit and kneighbors. - but it doesn't seem to be efficient. It's more like training model again and again for each row.


Is the approach or algorithm totally wrong? How to recommend a different product using the NearestNeighbour.


1 Answer 1


A few remarks:

  • I don't see why the user id is included in the features since the goal is to find similar products. It should probably be removed.
  • Actually a different approach would be to first cluster the users based on their ratings of the products in order to find groups of similar users, and then obtain the k-NN among a specific group of similar users.
  • There might be a problem here about the fact that the instances don't contain any information to match similar products. If there's a description of the product, the words could be used to find products which are semantically similar.
  • Technically k-NN doesn't train a model, it just stores the instances and computes the similarity for every instance. In theory at least it can easily be optimized to select a subset of instances. It might not be easy to adapt the predefined library functions (I don't know), but it's easy to implement the algorithm manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.