-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
396 lines (329 loc) · 11.5 KB
/
demo.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 1 23:46:39 2018
@author: J N BALAKUMARAN
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 1 17:11:06 2018
@author: J N BALAKUMARAN
"""
dataset={
"Alumni1":
{
'mfcs':3,
'ppl':3,
'stat':4,
'csa':3,
'ds':3,
'python':4.5
},
"Alumni2":
{
'mfcs':3,
'ds':3,
'ppl':4,
'stat':4,
'csa':3,
'python':3.5
},
"Alumni3":
{
'mfcs':4,
'ds':3,
'ppl':2,
'stat':3,
'csa':3,
'python':4.5
},
"Alumni4":
{
'mfcs':3.5,
'ds':3.5,
'ppl':2,
'csa':3,
'python':4.5
},
"Alumni5":
{
'mfcs':2.5,
'stat':3,
'ppl':2,
'csa':3
}
}
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 27 19:35:35 2018
@author: J N BALAKUMARAN
"""
#from recommendation_data import dataset
from math import sqrt
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
def pearson_correlation(person1,person2):
# To get both rated items
both_rated = {}
for item in dataset[person1]:
if item in dataset[person2]:
both_rated[item] = 1
number_of_ratings = len(both_rated)
# Checking for number of ratings in common
if number_of_ratings == 0:
return 0
# Add up all the preferences of each user
person1_preferences_sum = sum([dataset[person1][item] for item in both_rated])
person2_preferences_sum = sum([dataset[person2][item] for item in both_rated])
# Sum up the squares of preferences of each user
person1_square_preferences_sum = sum([pow(dataset[person1][item],2) for item in both_rated])
person2_square_preferences_sum = sum([pow(dataset[person2][item],2) for item in both_rated])
# Sum up the product value of both preferences for each item
product_sum_of_both_users = sum([dataset[person1][item] * dataset[person2][item] for item in both_rated])
# Calculate the pearson score
numerator_value = product_sum_of_both_users - (person1_preferences_sum*person2_preferences_sum/number_of_ratings)
denominator_value = sqrt((person1_square_preferences_sum - pow(person1_preferences_sum,2)/number_of_ratings) * (person2_square_preferences_sum -pow(person2_preferences_sum,2)/number_of_ratings))
if denominator_value == 0:
return 0
else:
r = numerator_value/denominator_value
return r
def most_similar_users(person,number_of_users):
# returns the number_of_users (similar persons) for a given specific person.
scores = [(pearson_correlation(person,other_person),other_person) for other_person in dataset if other_person != person ]
# Sort the similar persons so that highest scores person will appear at the first
scores.sort()
scores.reverse()
return scores[0:number_of_users]
def user_recommendations(person):
# Gets recommendations for a person by using a weighted average of every other user's rankings
totals = {}
simSums = {}
rankings_list =[]
for other in dataset:
# don't compare me to myself
if other == person:
continue
sim = pearson_correlation(person,other)
#print (">>>>>>>",sim)
# ignore scores of zero or lower
if sim <=0:
continue
for item in dataset[other]:
# only score movies i haven't seen yet
if item not in dataset[person] or dataset[person][item] == 0:
# Similrity * score
totals.setdefault(item,0)
totals[item] += dataset[other][item]* sim
# sum of similarities
simSums.setdefault(item,0)
simSums[item]+= sim
# Create the normalized list
rankings = [(total/simSums[item],item) for item,total in totals.items()]
rankings.sort()
rankings.reverse()
#print(rankings)
# returns the recommended items
recommendataions_list = [(recommend_item,score) for score,recommend_item in rankings]
return recommendataions_list
#scores=most_similar_users('student',5)
#print(scores)
print("Before :",dataset)
new=user_recommendations('Alumni5')
dataset['Alumni5'].update(new)
dataset['Alumni5']['ds']=round(dataset['Alumni5']['ds'],1)
dataset['Alumni5']['python']=round(dataset['Alumni5']['python'],1)
#print(dataset)
new=user_recommendations('Alumni4')
dataset["Alumni4"].update(new)
dataset['Alumni4']['stat']=round(dataset['Alumni4']['stat'],1)
print("After updated:",dataset)
csa=ppl=mfcs=ds=stat=python=0
for key in dataset:
ppl+=dataset[key]['ppl']
csa+=dataset[key]['csa']
stat+=dataset[key]['stat']
mfcs+=dataset[key]['mfcs']
ds+=dataset[key]['ds']
python+=dataset[key]['python']
print("ppl:",ppl,"csa:",csa,"stat:",stat,"mfcs:",mfcs,"ds:",ds,"python:",python)
mean=[]
print("length of dataset",len(dataset))
mean.append(ppl/len(dataset))
mean.append(csa/len(dataset))
mean.append(round(stat/len(dataset),1))
mean.append(mfcs/len(dataset))
mean.append(round(ds/len(dataset),1))
mean.append(round(python/len(dataset)))
print(mean)
mean.sort()
mean.reverse()
print(mean)
objects = ('python','stat','mfcs','ds','csa','ppl')
y_pos = np.arange(len(objects))
plt.bar(y_pos, mean, align='center', alpha=0.75)
plt.xticks(y_pos, objects)
plt.ylabel('rating')
plt.xlabel('courses')
plt.title('overall rating for sem1')
plt.show()
# -*- coding: utf-8 -*-
dataset={
"Inustry1":
{
'oops':3,
'microprocessor':3,
'ads':4,
'dbms':3,
'ot':3,
'computerfundamental':4.5
},
"Industry2":
{
'oops':3,
'ads':3,
'microprocessor':3,
'dbms':4,
'ot':3,
'computerfundamental':3.5
},
"Industry3":
{
'oops':4,
'ads':3,
'microprocessor':2,
'ot':3,
'dbms':3,
'computerfundamental':4.5
},
"Industry4":
{
'oops':3.5,
'ads':3.5,
'microprocessor':2,
'ot':3,
'computerfundamental':4.5
},
"Industry5":
{
'oops':2.5,
'ot':3,
'microprocessor':2,
'dbms':3
}
}
for i in dataset:
print(dataset[i].items(),"\n")
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 27 19:35:35 2018
@author: J N BALAKUMARAN
"""
#from recommendation_data import dataset
from math import sqrt
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
def pearson_correlation(person1,person2):
# To get both rated items
both_rated = {}
for item in dataset[person1]:
if item in dataset[person2]:
both_rated[item] = 1
number_of_ratings = len(both_rated)
# Checking for number of ratings in common
if number_of_ratings == 0:
return 0
# Add up all the preferences of each user
person1_preferences_sum = sum([dataset[person1][item] for item in both_rated])
person2_preferences_sum = sum([dataset[person2][item] for item in both_rated])
# Sum up the squares of preferences of each user
person1_square_preferences_sum = sum([pow(dataset[person1][item],2) for item in both_rated])
person2_square_preferences_sum = sum([pow(dataset[person2][item],2) for item in both_rated])
# Sum up the product value of both preferences for each item
product_sum_of_both_users = sum([dataset[person1][item] * dataset[person2][item] for item in both_rated])
# Calculate the pearson score
numerator_value = product_sum_of_both_users - (person1_preferences_sum*person2_preferences_sum/number_of_ratings)
denominator_value = sqrt((person1_square_preferences_sum - pow(person1_preferences_sum,2)/number_of_ratings) * (person2_square_preferences_sum -pow(person2_preferences_sum,2)/number_of_ratings))
if denominator_value == 0:
return 0
else:
r = numerator_value/denominator_value
return r
def most_similar_users(person,number_of_users):
# returns the number_of_users (similar persons) for a given specific person.
scores = [(pearson_correlation(person,other_person),other_person) for other_person in dataset if other_person != person ]
# Sort the similar persons so that highest scores person will appear at the first
scores.sort()
scores.reverse()
return scores[0:number_of_users]
def user_recommendations(person):
# Gets recommendations for a person by using a weighted average of every other user's rankings
totals = {}
simSums = {}
rankings_list =[]
for other in dataset:
# don't compare me to myself
if other == person:
continue
sim = pearson_correlation(person,other)
#print (">>>>>>>",sim)
# ignore scores of zero or lower
if sim <=0:
continue
for item in dataset[other]:
# only score movies i haven't seen yet
if item not in dataset[person] or dataset[person][item] == 0:
# Similrity * score
totals.setdefault(item,0)
totals[item] += dataset[other][item]* sim
# sum of similarities
simSums.setdefault(item,0)
simSums[item]+= sim
# Create the normalized list
rankings = [(total/simSums[item],item) for item,total in totals.items()]
rankings.sort()
rankings.reverse()
#print(rankings)
# returns the recommended items
recommendataions_list = [(recommend_item,score) for score,recommend_item in rankings]
return recommendataions_list
new=user_recommendations('Industry5')
dataset['Industry5'].update(new)
dataset['Industry5']['ads']=round(dataset['Industry5']['ads'],1)
dataset['Industry5']['computerfundamental']=round(dataset['Industry5']['computerfundamental'],1)
#print(dataset)
#print(dataset)
new=user_recommendations('Industry4')
dataset["Industry4"].update(new)
dataset['Industry4']['stat']=round(dataset['Industry4']['dbms'],1)
print(dataset)
ot=microprocessor=oops=ads=dbms=computerfundamental=0
for key in dataset:
ot+=dataset[key]['ot']
microprocessor+=dataset[key]['microprocessor']
dbms+=dataset[key]['dbms']
ads+=dataset[key]['ads']
computerfundamental+=dataset[key]['computerfundamental']
oops+=dataset[key]['oops']
print("ot:",ot,"oops:",oops,"dbms:",dbms,"microprocessor:",microprocessor,"ads:",ads,"computerfundamental:",computerfundamental)
mean=[]
print("length of dataset",len(dataset))
mean.append(ot/len(dataset))
mean.append(oops/len(dataset))
mean.append(round(dbms/len(dataset),1))
mean.append(microprocessor/len(dataset))
mean.append(round(ads/len(dataset),1))
mean.append(round(computerfundamental/len(dataset)))
print(mean)
mean.sort()
mean.reverse()
print(mean)
objects = ('comp.fundmtls','ads','dbms','oops','ot','mprocessor')
y_pos = np.arange(len(objects))
plt.bar(y_pos, mean, align='center', alpha=0.75)
plt.xticks(y_pos, objects)
plt.ylabel('rating')
plt.xlabel('courses')
plt.title('overall rating for sem2')
plt.show()