How to recommend items using Machine Learning

Recommending items, How to recommend items/products to user in machine learning using python, Use machine learning to create recommandations, Python program for recommending items

Category: Machine Learning Tags: Python, Python 3


    In our daily life we see many applications recommend us items based on our interests. Suppose you like to listen classical music then your YouTube wall will be filled with many other classical songs recommended by YouTube. In previous articles we learned about Euclidean Distance and Pearson Score which gives us similarity scores, in this article we will see how to use the score for recommending items. We recommend you to go through our previous article Euclidean Distance first.


    Suppose you have to show recommended songs to a user which he didn’t rate/listen yet. Now if you show all songs which are not rated by current user there will be possibility you end up recommending him a song which was not rated by any other users or may be good rating by few user but mostly users gave bad reviews about it. Now to overcome this situation we have to calculate weighted score. Let’s see how we calculate weighted scores of singers for user Donald below (This data has been taken from previous article Euclidean Distance):

Calculate Weighted score for Recommanding Items in Machine Learning
Fig 1: Calculate Weighted score for Recommanding Items


    In above table Chandler is missing because he rated all the singers which are rated by Donald too so chandler list doesn’t have any recommendations for Donald. Now we see in 2nd column all the similarity scores and 3rd, 5th columns are respective ratings for singers here Sam didn’t rate Demi, and Zoya, Robert didn’t rate MJ. In 4th and 6th column we can see Total Score is sum of similarity scores which users rated, Total of Products are sum of Singer*Score(product of singer's rating with score i.e. Demi*Score) and finally we have Final Score which is division of Total of Products and Total Score. By Final Score MJ has more score means MJ will be recommended before Demi.

    We will use same data we used in our previous article Euclidean Distance and we will use this method to calculate our score in below code we are injecting method euclidean_distance, now let's see the code below:

import sys
import math
from OnlineShopping import online_music
from EuclideanDistanceScore import euclidean_distnce

def getRecommendations(music_data, person, similiraty=euclidean_distnce):
sumScores = {} #keeps sum of scores of all users rated perticular singer
sumProducts = {} #keeps sum of singer*scores of all users rated perticular singer
for other in music_data.keys():
if other != person:
score = similiraty(online_music, 'Donald', other)

if score == 0: continue

for item in online_music[other]:
if item not in online_music[person] or online_music[person][item] == 0:
sumScores.setdefault(item, 0)
sumScores[item] += score #sum of scores for particular singer
sumProducts.setdefault(item, 0)
sumProducts[item] += score * online_music[other][item]
#ranking = sumProduct/sumScore
rankings = [(sumProducts[item]/sumScores[item], item) for item in sumScores]
return rankings

#executing above method
rankings = getRecommendations(online_music, "Donald")

[(3.718491035931837, 'MJ'), (2.9879739405525845, 'Demi Lovato')]

Above we created a method getRecommendations and finding recommendation for donald by executing ending lines. If we go line by line the code we can understand we are doing same we did in our table above and we can see same results.


We have seen how to generate weighted score to create recommendations and this is really useful in many applications everyday life. Weighted ranking is similar to weighted average actually we calculated kind of weighted average rating only if you observe carefully this is nothing more than a mathematical formula.

Like 0 People
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 138
Questions: 12
Given Best Solutions: 12 *


No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User