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)
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