Commit 59e137ab authored by RISHABH GARG's avatar RISHABH GARG

Initial

parents
import csv
csvfile=open('movie-ratings.csv')
read=csv.reader(csvfile)
reading=[]
for line in read:
reading.append(line)
f2 = open ("user_preference.csv")
data = csv.reader(f2)
preference = []
a=input("Your name :")
preference.append(a)
for line in data:
preference.append(line)
prefdict = dict( zip(preference[1],preference[2]))
print(prefdict)
\ No newline at end of file
Name,The Godfather,Pulp Fiction,Gone With the Wind,12 Angry Men,Lawrence of Arabia,Raging Bull,The Clockwork Orange,Cinema Paradiso,Wild Strawberries,Amadeus
Albert Einstein,9.5,7,10,10,-1,-1,6,10,8,10
The Joker,8,10,4,4,8,-1,10,9.5,7.5,8
Marilyn Monroe,8,-1,10,8.5,-1,8.5,6.5,-1,8,5
Steve Jobs,-1,8.5,8,10,9,7.5,9,8,8.5,9
Mahatma Gandhi,6,-1,-1,9,8,8,5,7,7,7
Pikachu,7,-1,5,-1,-1,9,-1,5,-1,10
Jon Snow,-1,7,10,8,8,8,8,8,8,-1
Walter White,9,10,6,6,7,-1,10,7,10,-1
J K Rowling,9,9,-1,9,8,7,9,10,10,9
Diego Maradona,-1,8,7,6,7,9,6,8,7,-1
Barrack Obama,7,9,10,10,7,9,-1,-1,-1,8
import csv
f1 = open ("movie-ratings.csv" , "rb")
data = csv.reader(f1)
rating = []
for line in data:
rating.append(line)
f2 = open ("user_preference.csv" , "rb")
data = csv.reader(f2)
preference = []
preference.append(input("Your name :"))
for line in data:
preference.append(line)
prefdict = dict( zip(preference[1],preference[2]))
header = rating[0]
del rating[0]
reviewers = [ s[0] for s in rating]
reviewdic = []
for line in rating:
dic = dict( (header[i],line[i]) for i in range(1,len(line)))
reviewdic.append(dic)
from math import sqrt
euc = []
for i in reviewdic:
diff = [(float(i[s])-float(prefdict[s]))**2 for s in prefdict.keys()]
euc.append(sqrt(sum(diff)))
ordereuc = sorted(zip(reviewers,euc), key=lambda x: x[1])
pear = []
x = [float(i) for i in prefdict.values()]
x2 = [ i**2 for i in x]
n = len(x)
for i in reviewdic:
y = [float(i[j]) for j in prefdict.keys()]
y2 = [ i**2 for i in y]
xy = [ x[i]*y[i] for i in range(n)]
corr = (n*sum(xy) - sum(x)*sum(y))/(sqrt(n*sum(x2) - sum(x)**2)*sqrt(n*sum(y2) - sum(y)**2))
pear.append(corr)
orderpear = sorted(zip(reviewers,pear), key=lambda x: x[1], reverse = True)
finalrating = {}
for movie in header[1:]:
finalrating[movie] = sum([pear[i]*float(reviewdic[i][movie]) for i in range(len(pear))])
finalorder = sorted(finalrating.items(), key=lambda x: x[1], reverse = True)
towatch = []
for movie,rating in finalorder:
if movie in prefdict.keys():
continue
else:
towatch.append(movie)
if len(towatch)>=3:
for i in range(3):
print (towatch[i])
else:
for movie,rating in finalorder:
print (movie)
The Godfather,Pulp Fiction,The Clockwork Orange,Cinema Paradiso,Wild Strawberries,Amadeus
9,7,6,10,8,10
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