-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser_Based_collaborative.py
More file actions
209 lines (160 loc) · 5.61 KB
/
User_Based_collaborative.py
File metadata and controls
209 lines (160 loc) · 5.61 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import numpy as np
import scipy.stats
import scipy.spatial
from sklearn.cross_validation import KFold
import random
from sklearn.metrics import mean_squared_error
from math import sqrt
import math
import warnings
import sys
#from sklearn.utils.extmath import np.dot
warnings.simplefilter("error")
users = 6040
items = 3952
def readingFile(filename):
f = open(filename,"r")
data = []
for row in f:
r = row.split(',')
e = [int(r[0]), int(r[1]), int(r[2])]
data.append(e)
return data
def similarity_user(data):
print "Hello User"
user_similarity_cosine = np.zeros((users,users))
user_similarity_jaccard = np.zeros((users,users))
user_similarity_pearson = np.zeros((users,users))
for user1 in range(users):
print user1
for user2 in range(users):
if np.count_nonzero(data[user1]) and np.count_nonzero(data[user2]):
user_similarity_cosine[user1][user2] = 1-scipy.spatial.distance.cosine(data[user1],data[user2])
user_similarity_jaccard[user1][user2] = 1-scipy.spatial.distance.jaccard(data[user1],data[user2])
try:
if not math.isnan(scipy.stats.pearsonr(data[user1],data[user2])[0]):
user_similarity_pearson[user1][user2] = scipy.stats.pearsonr(data[user1],data[user2])[0]
else:
user_similarity_pearson[user1][user2] = 0
except:
user_similarity_pearson[user1][user2] = 0
#f_i_d.write(str(user1) + "," + str(user2) + "," + str(user_similarity_cosine[user1][user2]) + "," + str(user_similarity_jaccard[user1][user2]) + "," + str(user_similarity_pearson[user1][user2]) + "\n")
#f_i_d.close()
return user_similarity_cosine, user_similarity_jaccard, user_similarity_pearson
def crossValidation(data):
k_fold = KFold(n=len(data), n_folds=10)
Mat = np.zeros((users,items))
for e in data:
Mat[e[0]-1][e[1]-1] = e[2]
sim_user_cosine, sim_user_jaccard, sim_user_pearson = similarity_user(Mat)
rmse_cosine = []
rmse_jaccard = []
rmse_pearson = []
for train_indices, test_indices in k_fold:
train = [data[i] for i in train_indices]
test = [data[i] for i in test_indices]
M = np.zeros((users,items))
for e in train:
M[e[0]-1][e[1]-1] = e[2]
true_rate = []
pred_rate_cosine = []
pred_rate_jaccard = []
pred_rate_pearson = []
for e in test:
user = e[0]
item = e[1]
true_rate.append(e[2])
pred_cosine = 3.0
pred_jaccard = 3.0
pred_pearson = 3.0
#user-based
if np.count_nonzero(M[user-1]):
sim_cosine = sim_user_cosine[user-1]
sim_jaccard = sim_user_jaccard[user-1]
sim_pearson = sim_user_pearson[user-1]
ind = (M[:,item-1] > 0)
#ind[user-1] = False
normal_cosine = np.sum(np.absolute(sim_cosine[ind]))
normal_jaccard = np.sum(np.absolute(sim_jaccard[ind]))
normal_pearson = np.sum(np.absolute(sim_pearson[ind]))
if normal_cosine > 0:
pred_cosine = np.dot(sim_cosine,M[:,item-1])/normal_cosine
if normal_jaccard > 0:
pred_jaccard = np.dot(sim_jaccard,M[:,item-1])/normal_jaccard
if normal_pearson > 0:
pred_pearson = np.dot(sim_pearson,M[:,item-1])/normal_pearson
if pred_cosine < 0:
pred_cosine = 0
if pred_cosine > 5:
pred_cosine = 5
if pred_jaccard < 0:
pred_jaccard = 0
if pred_jaccard > 5:
pred_jaccard = 5
if pred_pearson < 0:
pred_pearson = 0
if pred_pearson > 5:
pred_pearson = 5
print str(user) + "\t" + str(item) + "\t" + str(e[2]) + "\t" + str(pred_cosine) + "\t" + str(pred_jaccard) + "\t" + str(pred_pearson)
pred_rate_cosine.append(pred_cosine)
pred_rate_jaccard.append(pred_jaccard)
pred_rate_pearson.append(pred_pearson)
rmse_cosine.append(sqrt(mean_squared_error(true_rate, pred_rate_cosine)))
rmse_jaccard.append(sqrt(mean_squared_error(true_rate, pred_rate_jaccard)))
rmse_pearson.append(sqrt(mean_squared_error(true_rate, pred_rate_pearson)))
print str(sqrt(mean_squared_error(true_rate, pred_rate_cosine))) + "\t" + str(sqrt(mean_squared_error(true_rate, pred_rate_jaccard))) + "\t" + str(sqrt(mean_squared_error(true_rate, pred_rate_pearson)))
#raw_input()
#print sum(rms) / float(len(rms))
rmse_cosine = sum(rmse_cosine) / float(len(rmse_cosine))
rmse_pearson = sum(rmse_pearson) / float(len(rmse_pearson))
rmse_jaccard = sum(rmse_jaccard) / float(len(rmse_jaccard))
print str(rmse_cosine) + "\t" + str(rmse_jaccard) + "\t" + str(rmse_pearson)
f_rmse = open("rmse_user.txt","w")
f_rmse.write(str(rmse_cosine) + "\t" + str(rmse_jaccard) + "\t" + str(rmse_pearson) + "\n")
rmse = [rmse_cosine, rmse_jaccard, rmse_pearson]
req_sim = rmse.index(min(rmse))
print req_sim
f_rmse.write(str(req_sim))
f_rmse.close()
if req_sim == 0:
sim_mat_user = sim_user_cosine
if req_sim == 1:
sim_mat_user = sim_user_jaccard
if req_sim == 2:
sim_mat_user = sim_user_pearson
#predictRating(Mat, sim_mat_user)
return Mat, sim_mat_user
def predictRating(recommend_data):
M, sim_user = crossValidation(recommend_data)
f = open(sys.argv[2],"r")
toBeRated = {"user":[], "item":[]}
for row in f:
r = row.split(',')
toBeRated["item"].append(int(r[1]))
toBeRated["user"].append(int(r[0]))
f.close()
pred_rate = []
fw_w = open('result1.csv','w')
l = len(toBeRated["user"])
for e in range(l):
user = toBeRated["user"][e]
item = toBeRated["item"][e]
pred = 3.0
#user-based
if np.count_nonzero(M[user-1]):
sim = sim_user[user-1]
ind = (M[:,item-1] > 0)
#ind[user-1] = False
normal = np.sum(np.absolute(sim[ind]))
if normal > 0:
pred = np.dot(sim,M[:,item-1])/normal
if pred < 0:
pred = 0
if pred > 5:
pred = 5
pred_rate.append(pred)
print str(user) + "," + str(item) + "," + str(pred)
fw_w.write(str(pred) + "\n")
fw_w.close()
recommend_data = readingFile(sys.argv[1])
predictRating(recommend_data)