Commit 4f8d298d authored by RISHABH GARG's avatar RISHABH GARG

Pearson Commit

parent 59e137ab
import csv
from math import sqrt
csvfile=open('movie-ratings.csv')
read=csv.reader(csvfile)
reading=[]
for line in read:
reading.append(line)
reading=list(csv.reader(csvfile))
#########################
f2 = open ("user_preference.csv")
data = csv.reader(f2)
user_data = csv.reader(f2)
preference = []
a=input("Your name :")
preference.append(a)
for line in data:
preference.append(input("Your name :"))
for line in user_data:
preference.append(line)
prefdict = dict( zip(preference[1],preference[2]))
print(prefdict)
\ No newline at end of file
####################
header = reading[0]
del reading[0]
reviewers = [ s[0] for s in reading]
reviewdic = []
for line in reading:
dic = dict( (header[i],line[i]) for i in range(1,len(line)))
reviewdic.append(dic)
dist=[]
for i in reviewdic:
sum1=0
euclid_temp=[sqrt((float(i[j])-float(prefdict[j]))**2) for j in prefdict.keys() if float(i[j])!=-1 and float(prefdict[j])!=-1]
dist.append(sum(euclid_temp)/len(euclid_temp))
order_dist = sorted(zip(reviewers,dist), key=lambda x: x[1],reverse=True)
#############################################
temp_movieUser=[i for i in prefdict.keys() if float(prefdict[i])!=-1]
pear=[]
for i in reviewdic:
temp_movieRev=[j for j in prefdict.keys() if float(i[j])!=-1]
var=list(set(temp_movieUser).intersection(temp_movieRev))
temp=[float(i[j]) for j in var if float(i[j])!=-1]
temp1=[float(prefdict[j]) for j in var if float(prefdict[j])!=-1]
mean=sum(temp)/len(temp)
avgUser=sum(temp1)/len(temp1)
stdUser=sqrt((sum(map(lambda x: x*x,temp1))/len(temp1))-avgUser)
stdRev=sqrt((sum(map(lambda x: x*x,temp))/len(temp))-mean)
pear.append(sum([(float(prefdict[j])-avgUser)*(float(i[j])-mean)/(len(var)*stdRev*stdUser) for j in var]))
order_pear=sorted(zip(reviewers,pear), key=lambda x:x[1], reverse=True)
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
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
The Godfather,Pulp Fiction,The Clockwork Orange,Cinema Paradiso,Wild Strawberries,Amadeus
9,7,6,10,8,10
The Godfather,Pulp Fiction,Gone With the Wind,12 Angry Men,Lawrence of Arabia,Raging Bull,The Clockwork Orange,Cinema Paradiso,Wild Strawberries,Amadeus
9,7,6,10,8,10,-1,-1,-1,-1
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