-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal.py
184 lines (144 loc) · 6.41 KB
/
Final.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 13 22:29:10 2022
@author: sasha
"""
from surprise import Reader, SVD, Dataset, SVDpp
from surprise.model_selection import cross_validate
import numpy as np
import pandas as pd
import scipy.sparse as sp
from sklearn.metrics.pairwise import cosine_similarity
from scipy.stats import pearsonr
class Recommend:
def __init__(self):
format_ = np.loadtxt('format.dat')
train_ = np.loadtxt('train.dat')
test_ = np.loadtxt('test.dat')
self.UserConsistency = False
self.ItemConsistency = False
TestUsers = set(np.unique(test_[:,0]))
TrainUsers = set(np.unique(train_[:,0]))
TestItems = set(np.unique(test_[:,1]))
TrainItems = set(np.unique(train_[:,1]))
if len(TrainUsers.intersection(TestUsers)) == len(TestUsers):
self.UserConsistency = True #print('All test set users are available in training set')
else :
#print('Missing Users found. Use Mis_Users variable')
self.Mis_Users = TestUsers.difference(TrainUsers)
if len(TrainItems.intersection(TestItems)) == len(TestItems):
self.ItemConsistency = True #print('All test set items are available in training set')
else :
#print('Missing Items found. Use Mis_Items variable')
self.Mis_Items = TestItems.difference(TrainItems)
train_Head = ['UserID','ItemID','Ratings','Timestamp']
test_Head = ['UserID','ItemID']
#self.Training = pd.DataFrame(train_,columns=train_Head)
#self.Testing = pd.DataFrame(test_,columns=test_Head)
self.Training = pd.DataFrame(train_[0:80000,:],columns=train_Head)
self.Testing = pd.DataFrame(train_[80001:,0:2],columns=test_Head)
self.answers = pd.DataFrame(train_[80001:,2],columns=['ans'])
self.UvI = 0
return
def MakeUserVsItem(self):
Tests = self.Testing.copy()
Tests['Ratings']=0
Combine = Tests.append(self.Training.iloc[:,0:-1])
self.UvI = Combine.pivot('UserID','ItemID','Ratings')
self.UvI.fillna(0,inplace=True)
self.Users = self.UvI.index.to_list()
self.Items = self.UvI.columns.to_list()
return
def verifyUvI(self):
crossverify = []
for i in range(0,self.UvI.shape[0]):
if self.UvI[self.Testing.iloc[i,1]][self.Testing.iloc[i,0]] == 0:
crossverify.append(True)
else:
crossverify.append(False)
if not all(crossverify):
print('All testing segments are proper')
return
def GetUvIMatrix(self):
if not self.UvI ==0 :
self.MakeUserVsItem()
self.verifyUvI()
return self.UvI
def CosineSimilarity(self):
sparsematrix = sp.csr_matrix(self.UvI)
self.UUSim = cosine_similarity(sparsematrix)
self.IISim = cosine_similarity(sparsematrix.T)
return
#@jit(target="cuda")
#@cuda.jit
def JaccardSimilarity(self):
self.UUSim = np.zeros((self.UvI.shape[0],self.UvI.shape[0]))
self.IISim = np.zeros((self.UvI.shape[1],self.UvI.shape[1]))
# UU Matrix
for i in range(0,self.UvI.shape[0]):
for j in range (i,self.UvI.shape[0]):
A = set(self.UvI.T[self.UvI.T.iloc[:,i]>0].index.to_list())
B = set(self.UvI.T[self.UvI.T.iloc[:,j]>0].index.to_list())
self.UUSim[i][j] = len(A & B) / len(A | B)
return
#@jit(target="cuda")
#@cuda.jit
def PearsonSimilarity(self):
#user user matrix
self.UUSim = np.zeros((self.UvI.shape[0],self.UvI.shape[0]))
self.IISim = np.zeros((self.UvI.shape[1],self.UvI.shape[1]))
#print('ok-1')
for i in range(0,self.UvI.shape[0]):
for j in range (i,self.UvI.shape[0]):
self.UUSim[i][j] = pearsonr(self.UvI.iloc[i,:],self.UvI.iloc[j,:])[0]
#self.UUSim[j][i] = self.UUSim[i][j]
#print('ok-2')
for i in range(0,self.UvI.shape[1]):
for j in range(i,self.UvI.shape[1]):
self.IISim[i][j] = pearsonr(self.UvI.iloc[:,i],self.UvI.iloc[:,j])[0]
#self.IISim[j][i] = self.IISim[i][j]
return
def PredictRating(self):
RatingPredict = []
u_ = self.UvI.mean().mean()
for i in range(0,5):
# bxi = u + bx + bi == user ratings avg + item rating avg - u
x_ = self.Testing.iloc[i,0] #x
i_ = self.Testing.iloc[i,1] # i
bxi = self.UvI.T.mean().loc[x_]-u_ + self.UvI.mean().loc[i_]-u_ + u_
#print(bxi)
Rxj = np.array(self.UvI.loc[x_,:]) # Ratings of user to all movies
i_ind = self.UvI.columns.to_list().index(i_)
Sij = self.IISim[i_ind] #Similarity scores of Item v Item for given Item I
bx = np.array(self.UvI.mean())+self.UvI.T.mean().loc[x_]
bx = bx - u_
#print(np.max(bx))
Numer = np.sum(Sij * (Rxj - bx))
Denom = np.sum(Sij)
#print(Numer/Denom)
RatingPredict.append(bxi+(Numer/Denom))
return RatingPredict
#Sys = Recommend()
#Sys.MakeUserVsItem()
#Sys.CosineSimilarity()
#ans = Sys.PredictRating()
train_ = np.loadtxt('train.dat')
test_ = np.loadtxt('test.dat')
read = Reader(rating_scale = (0.0,5.0))
Training = pd.DataFrame(train_ ,columns=['UserID','ItemID','Ratings','Timestamp'])
#Training['Ratings'] = Training['Ratings'].apply(lambda x: x/5)
Testing = pd.DataFrame(test_ ,columns=['UserID','ItemID'])
Tr = Dataset.load_from_df(Training[['UserID','ItemID','Ratings']],read)
model = SVD(n_factors=20,verbose=False, n_epochs=30)
cross_validate(model, Tr, measures = ['RMSE'], cv=10, verbose=False)
model.fit(Tr.build_full_trainset())
Predictions=[]
Str = ''
for i in range(0,len(Testing)):
Predictions.append((model.predict(Testing.iloc[i,0],Testing.iloc[i,1]).est))
Str = Str + str((model.predict(Testing.iloc[i,0],Testing.iloc[i,1]).est))+'\n'
Name= 'Prediction3.dat'
F=open(Name,'w')
F.write(Str)
F.close()
del F