Commit 3e4969bd authored by DIVYANSH PAREEK's avatar DIVYANSH PAREEK

Update collab-filter.py for Including Pearson Calculation

parent 03be5922
......@@ -36,4 +36,41 @@ for i in range(10):
data.append(userRatings)
file.close()
userFile.close()
#data is complete at this point
\ No newline at end of file
#data is complete at this point
# Pearson Correlation Basic Implementation Using for Loop
pearson = []
rows = len(data)
users = data[rows-1]
cols = len(users)
for x in range(1,rows-1): # does not include (rows-1)
lis = data[x]
sumprod = 0
sumx = 0
sumy = 0
sumx2 = 0
sumy2 = 0
n = 0
for y in range(1,cols):
if(float(lis[y]) != -1 and float(users[y]) != -1):
sumprod += float(lis[y])*float(users[y])
sumx += float(lis[y])
sumy += float(users[y])
sumx2 += float(lis[y]) ** 2
sumy2 += float(users[y]) ** 2
n += 1
if(n > 0):
nume = n*sumprod - sumx*sumy
deno = (n*sumx2 - sumx**2) * (n*sumy2 - sumy**2)
deno = deno ** 0.5
if(deno > 0):
summ = nume / deno
else:
summ = 1
pearson.append((summ,lis[0]))
else:
pearson.append((0,lis[0]))
pearson.sort(reverse=True)
print(pearson)
\ 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