-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prediction_model.py
60 lines (48 loc) · 1.7 KB
/
Prediction_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 13 16:41:56 2018
@author:Onkar Mumbrekar
Website: www.onkarmumbrekar.co.in
"""
import numpy as np
#def Prediction(user,item,w,userdata,p,sources):
# ri_dash = cal_rating_mean_ri_dash(userdata,item)
# #print("ri_dash ", ri_dash)
# sigma_i = cal_variance(userdata,item)
# n=(p-1)*sources
# numerator = cal_numerator(user,item,userdata,w,n)
# denominator = cal_denominator(item,w,n)
# intermediatedata =numerator/denominator
# prediction_i = ri_dash + (sigma_i * intermediatedata)
# return prediction_i
def Prediction(user,w,userdata,p,sources):
prediction_user = np.zeros(4)
start = (p-1)*sources
end = (p*sources)
for item in range(start,end):
ri_dash = cal_rating_mean_ri_dash(userdata,item)
sigma_i = cal_variance(userdata,item)
n=(p-1)*sources
numerator = cal_numerator(user,item,userdata,w,n)
denominator = cal_denominator(item,w,n)
intermediatedata =numerator/denominator
prediction_user[item-((p-1)*sources)] = ri_dash + (sigma_i * intermediatedata)
return prediction_user
def cal_rating_mean_ri_dash(userdata,item):
r_dash = userdata.mean(axis=0)[item]
return r_dash
def cal_variance(userdata,item):
sigma =userdata.var(axis=0)[item]
return sigma
def cal_numerator(user,item,userdata,w,n):
numerator=0
for j in range(n-1):
ruj = userdata[user,j]
rj_dash = cal_rating_mean_ri_dash(userdata,j)
sigma_j =cal_variance(userdata,j)
wij = w[item,j]
numerator += ((ruj-rj_dash)/sigma_j)*wij
return numerator
def cal_denominator(item,w,n):
wij_sum = w[item,:n-1].sum()
return wij_sum