-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimensparsityeval.py
178 lines (144 loc) · 6.4 KB
/
timensparsityeval.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
import numpy as np
from surprise import AlgoBase
import multiprocessing
from surprise import Dataset, evaluate
from surprise import Reader
from surprise import KNNWithMeans
from surprise import KNNWithZScore
from surprise import SVD
from surprise import KNNBasic
from surprise import KNNWithMeansC
from collections import defaultdict
from surprise.model_selection import train_test_split
from surprise.model_selection import cross_validate
from surprise.accuracy import rmse
from surprise.accuracy import mae
from surprise.model_selection import GridSearchCV
from surprise.agreements import agree_trust
from surprise.agreements import agree_trust_op
from surprise.agreements import odonovan_trust_old
from surprise.agreements import agree_trust_opitmal
from surprise.agreements import agree_trust_opitmal_a_b
from surprise.model_selection import KFold
import matplotlib.pyplot as plt
import pandas as pd
import copy as cp
import os
import time
######################################### running time parameters #############################
import random
random.seed(301)
datasetname = 'ml-latest-small'
number_of_users = 10
file_path = os.path.expanduser('~') + '/.surprise_data/ml-latest-small/ratings.csv'
df = pd.read_csv(file_path)
list = df.userId.unique()
# list = df.movieId.unique()
print(len(list))
random_number = random.randint(0,len(list)-number_of_users)
# list = df.movieId.unique()
# list = random.sample(set(list), 10)
list = list[list[random_number:random_number+number_of_users]]
# df = df.loc[df['movieId'].isin(list)]
print(len(list))
print(list)
df = df.loc[df['userId'].isin(list)]
# df = df.loc[df['rating'].isin([3])]
print(df)
#reader is still required to load from dataframe
reader = Reader(rating_scale=(0.5, 5))
data = Dataset.load_from_df(df[['userId', 'movieId', 'rating']],rating_scale=(0.5, 5), reader=reader)
trainset = data.build_full_trainset()
beta = 2.5
if datasetname == 'jester':
beta = 0
###########################################################################AgreeTrust
class AgreeTrustAlgorithm(AlgoBase):
def __init__(self, k=40, min_k=1, alog=KNNWithMeans,user_based =True, beta=2.5, epsilon=0.9, lambdak=0.9, sim_options={}, verbose=True, **kwargs):
# Always call base method before doing anything.
AlgoBase.__init__(self)
self.k = k
self.min_k = min_k
self.algo = alog(k=k,sim_options=sim_options,verbose=verbose)
self.epsilon = epsilon
self.lambdak =lambdak
self.beta = beta
if user_based:
self.ptype = 'user'
else:
self.ptype = 'item'
def fit(self, trainset):
# Here again: call base method before doing anything.
AlgoBase.fit(self, trainset)
self.algo.fit(trainset)
if self.algo.verbose:
print('Ignore the above similiary matrix generation message, its not used in this algorithm')
print('Calculating AgreeTrust matrix ...')
start = time.time()
# tr, comon, noncom = agree_trust_opitmal_a_b(trainset, self.beta, self.epsilon, self.algo.sim, ptype=self.ptype, istrainset=True, activity=False)
tr, comon, noncom = agree_trust_opitmal_a_b(trainset, self.beta, self.epsilon, self.algo.sim, ptype=self.ptype, istrainset=True, activity=False)
self.algo.sim = tr**self.lambdak - (self.epsilon*noncom)
# self.algo.sim[self.algo.sim > 1] = 1
print(time.time() - start)
print('agree_trust_opitmal_a_b fit time')
return self
def estimate(self, u, i):
return self.algo.estimate(u,i)
###########################################################################OdnovanAlgorithm
class OdnovanAlgorithm(AlgoBase):
def __init__(self, k=40, min_k=1, alog=KNNWithMeans,user_based =True, alpha=0.2, sim_options={}, load=False, verbose=True, **kwargs):
self.algo = alog(k=k,sim_options=sim_options,verbose=verbose)
self.alpha = alpha
self.load = load
# self.testset = testset
if user_based:
self.ptype = 'user'
else:
self.ptype = 'item'
def fit(self, trainset):
# Here again: call base method before doing anything.
AlgoBase.fit(self, trainset)
self.algo.fit(trainset)
if self.algo.verbose:
print('Ignore the above similiary matrix generation message, its not used in this algorithm')
print('OdnovanAlgorithm here')
start = time.time()
if self.load == False:
n_process = multiprocessing.cpu_count()
self.algo.sim = odonovan_trust_old(trainset, self.algo, ptype=self.ptype, alpha=self.alpha, optimized=True, n_jobs=n_process)
print(time.time() - start)
print('OdnovanAlgorithm fit time')
print(self.algo.sim.shape)
def estimate(self, u, i):
# KNNWithMeans.test()
# self.algo.estimate(u,i)
return self.algo.estimate(u,i)
user_based = True
sim_options={'name':'pearson','user_based':user_based}
#### OdnovanAlgorithm (alog1)# ########################################################################
#### uncomment this section to run OdnovanAlgorithm, and comment other alogs
# alpha=0.01
# alpha=0.2
# predict_alog=KNNWithMeans
# algo = OdnovanAlgorithm(alog=KNNWithMeans, sim_options=sim_options,load=False, user_based=user_based, alpha=alpha, verbose=False)
# algo_name = 'OdnovanAlgorithm'
#### AgreeTrustAlgorithm (alog2)# ########################################################################
#### comment this section to run other alogrithms
epsilon=1
lambdak=0.5
predict_alog=KNNWithMeans
algo = AgreeTrustAlgorithm(k=40, alog=predict_alog, user_based =user_based, beta=beta, epsilon=epsilon, lambdak=lambdak, sim_options=sim_options, verbose=False)
algo_name = 'AgreeTrustAlgorithm'
#### KNNBasic (alog3)# ########################################################################
#### uncomment this section to run KNNBasic, and comment other alogs
# algo = KNNBasic(k=40,sim_options=sim_options,verbose=True)
# algo_name = 'KNNBasic'
######################################### running time checking #############################
# start = time.time()
algo.fit(trainset)
# print(time.time() - start)
######################################### sparcity checking #############################
# trust_matrix = algo.algo.sim #for Odonovan and AgreeTrust algorithm
# # trust_matrix = algo.sim # for kNN basic uncomment this line commment above line
# print(trust_matrix)
# print(sum(x == 0 for row in trust_matrix for x in row))