Commit dad3555b authored by DIVYANSH PAREEK's avatar DIVYANSH PAREEK

Update collab-filter.py for final movie recommendation referring to task A.3

parent 3e4969bd
......@@ -39,6 +39,7 @@ userFile.close()
#data is complete at this point
# Pearson Correlation Basic Implementation Using for Loop
pearson = []
rows = len(data)
users = data[rows-1]
......@@ -68,9 +69,35 @@ for x in range(1,rows-1): # does not include (rows-1)
summ = nume / deno
else:
summ = 1
pearson.append((summ,lis[0]))
data[x].append(summ)
else:
pearson.append((0,lis[0]))
data[x].append(0)
# explicit choice to take summ = 1 for the corner case ... However, we were told no such "Pathological Cases" would be given
# print(data)
# Humanity's Answer : Suggesting Movies
review = []
for i in range(1,cols):
if(float(users[i]) == -1):
movie_name = data[0][i]
movie_rate = 0
sumcorrcoeff = 0
for g in range(1,rows-1):
if(float(data[g][i]) >= 0):
movie_rate += float(data[g][i])*float(data[g][cols])
sumcorrcoeff += abs(float(data[g][cols]))
if(sumcorrcoeff > 0):
movie_rate = movie_rate / sumcorrcoeff
else: # 0/0 format
movie_rate = 0
review.append((movie_rate,movie_name))
review.sort(reverse=True)
number = len(review)
number = min(3,number)
print("The top movie recommendations for this particular user are :")
for x in range(number):
print(review[x][1])
pearson.sort(reverse=True)
print(pearson)
\ No newline at end of file
# Tried this, but didn't work, TypeError: map() takes 1 positional argument but 2 were given
# movie_rate = sum(map(lambda g: func(float(data[g][i]))*float(data[g][cols]), range(1,rows-1)))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment