-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapleyvalues_Random.py
290 lines (244 loc) · 15.6 KB
/
shapleyvalues_Random.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
from itertools import combinations
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from collections import Counter
from sklearn.metrics import precision_score, recall_score, f1_score,accuracy_score
#from randata import *
import time
# Function to select the top 2 subsets for each group based on Shapley values
def select_top_subsets(shapley_values, top_n=15):
top_subsets = {}
for group_name, values in shapley_values.items():
sorted_values = sorted(values.items(), key=lambda x: x[1], reverse=True)
top_subsets[group_name] = sorted_values[:top_n]
return top_subsets
def calculate_shapley_values(user_ratings, ground_truth_group_ratings, group_names):
shapley_values = {}
for group_name in group_names:
#print(f"Group name: {group_name}")
users = list(user_ratings[group_name].keys())
all_subsets = generate_all_subsets(users)
n = len(all_subsets) # Number of subsets in F
shapley_values[group_name] = {}
for subset in all_subsets:
shapley_values[group_name][subset] = calculate_shapley_value(
subset, users, user_ratings[group_name], ground_truth_group_ratings, n, group_name
)
return shapley_values
def calculate_shapley_value(subset, users, user_ratings, ground_truth_ratings, n, group_name):
shapley_value = 0
#print("subset",subset)
subset_users = set(subset)
complement_users = set(users) - subset_users
complement_subsets = generate_all_subsets(complement_users)
l = len(complement_subsets)
for complement_subset in complement_subsets:
full_subset = subset_users.union(complement_subset)
#print("complement_subset",complement_subset)
#print("full_subset",full_subset)
coalition_value = calculate_coalition_value(full_subset, users, user_ratings, ground_truth_ratings, group_name)
complement_value = calculate_coalition_value(complement_subset, users, user_ratings, ground_truth_ratings,
group_name)
shapley_value += coalition_value - complement_value
shapley_value /= l
return shapley_value
def calculate_coalition_value(subset, users, user_ratings, ground_truth_ratings, group_name):
subset_ratings = {user: user_ratings[user] for user in subset}
coalition_ratings = combine_user_ratings(subset_ratings)
ground_truth_ratings = ground_truth_ratings[group_name]
similarity = cosine_similarity([coalition_ratings], [ground_truth_ratings])
#print("similarity",similarity)
coalition_value = similarity[0][0]
#print("coalition_value",coalition_value)
return coalition_value
def combine_user_ratings(user_ratings):
return np.mean(list(user_ratings.values()), axis=0)
def generate_all_subsets(users):
all_subsets = []
for i in range(1, len(users) + 1):
all_subsets.extend(combinations(users, i))
return all_subsets
group_names = ['Group1', 'Group2', 'Group3', 'Group4', 'Group5']
user_ratings = {'Group1': {'User39': [0.35, 0.75, 0.05, 0.25, 0.35, 0.85, 0.65, 0.85, 0.35, 0.55],
'User44': [0.45, 0.45, 0.95, 0.75, 0.55, 0.25, 0.25, 0.05, 0.55, 0.75]},
'Group2': {'User53': [0.25, 0.95, 0.15, 0.25, 0.55, 0.75, 0.75, 0.65, 0.25, 0.45],
'User15': [0.15, 0.95, 0.35, 0.35, 0.15, 0.55, 0.85, 0.55, 0.65, 0.45],
'User22': [0.55, 0.45, 0.15, 0.15, 0.55, 0.65, 0.75, 0.95, 0.35, 0.45],
'User8': [0.25, 0.85, 0.65, 0.45, 0.15, 0.45, 0.75, 0.25, 0.85, 0.35]},
'Group3': {'User5': [0.35, 0.65, 0.45, 0.65, 0.25, 0.55, 0.75, 0.25, 0.35, 0.75],
'User58': [0.45, 0.35, 0.65, 0.35, 0.15, 0.35, 0.75, 0.45, 0.55, 0.85],
'User16': [0.25, 0.65, 0.45, 0.35, 0.15, 0.45, 0.65, 0.45, 0.85, 0.75],
'User19': [0.45, 0.65, 0.15, 0.15, 0.25, 0.85, 0.55, 0.85, 0.35, 0.75],
'User10': [0.65, 0.25, 0.75, 0.85, 0.65, 0.35, 0.35, 0.05, 0.65, 0.45],
'User32': [0.45, 0.55, 0.15, 0.15, 0.25, 0.65, 0.75, 0.95, 0.55, 0.45],
'User51': [0.15, 0.95, 0.15, 0.75, 0.45, 0.75, 0.55, 0.25, 0.25, 0.75]},
'Group4': {'User45': [0.55, 0.25, 0.35, 0.45, 0.75, 0.65, 0.05, 0.45, 0.25, 0.85],
'User31': [0.15, 0.95, 0.25, 0.65, 0.25, 0.65, 0.75, 0.45, 0.35, 0.55],
'User28': [0.75, 0.25, 0.35, 0.45, 0.95, 0.65, 0.25, 0.65, 0.15, 0.55],
'User12': [0.65, 0.45, 0.45, 0.65, 0.75, 0.45, 0.35, 0.45, 0.35, 0.45],
'User49': [0.45, 0.55, 0.85, 0.85, 0.75, 0.45, 0.25, 0.05, 0.35, 0.45],
'User52': [0.15, 0.85, 0.25, 0.45, 0.15, 0.45, 0.75, 0.45, 0.85, 0.65],
'User57': [0.65, 0.15, 0.55, 0.45, 0.95, 0.35, 0.05, 0.25, 0.65, 0.85],
'User33': [0.55, 0.25, 0.25, 0.45, 0.85, 0.75, 0.05, 0.55, 0.35, 0.95],
'User34': [0.55, 0.45, 0.05, 0.45, 0.15, 0.65, 0.35, 0.75, 0.45, 0.95],
'User46': [0.15, 0.95, 0.55, 0.75, 0.25, 0.55, 0.85, 0.35, 0.05, 0.55],
'User43': [0.15, 0.95, 0.25, 0.45, 0.15, 0.65, 0.85, 0.65, 0.45, 0.45],
'User37': [0.55, 0.25, 0.75, 0.75, 0.65, 0.45, 0.05, 0.15, 0.75, 0.65],
'User26': [0.35, 0.65, 0.05, 0.25, 0.35, 0.75, 0.55, 0.75, 0.35, 0.95],
'User56': [0.55, 0.65, 0.15, 0.15, 0.45, 0.65, 0.75, 0.95, 0.35, 0.35],
'User4': [0.35, 0.55, 0.15, 0.15, 0.35, 0.75, 0.75, 0.95, 0.35, 0.65]},
'Group5': {'User3': [0.65, 0.15, 0.45, 0.45, 0.85, 0.45, 0.05, 0.25, 0.75, 0.95],
'User21': [0.55, 0.35, 0.75, 0.55, 0.55, 0.25, 0.25, 0.05, 0.85, 0.85],
'User55': [0.45, 0.55, 0.15, 0.15, 0.25, 0.75, 0.65, 0.95, 0.35, 0.75],
'User25': [0.25, 0.65, 0.25, 0.15, 0.35, 0.75, 0.75, 0.75, 0.45, 0.65],
'User7': [0.45, 0.55, 0.15, 0.15, 0.25, 0.75, 0.65, 0.95, 0.35, 0.75],
'User38': [0.35, 0.65, 0.15, 0.15, 0.35, 0.85, 0.75, 0.85, 0.35, 0.55],
'User14': [0.45, 0.25, 0.25, 0.35, 0.65, 0.75, 0.35, 0.85, 0.15, 0.75],
'User11': [0.45, 0.45, 0.65, 0.65, 0.55, 0.25, 0.25, 0.05, 0.85, 0.85],
'User27': [0.25, 0.85, 0.75, 0.75, 0.45, 0.35, 0.75, 0.15, 0.45, 0.25],
'User36': [0.35, 0.55, 0.25, 0.15, 0.25, 0.85, 0.75, 0.85, 0.35, 0.65],
'User47': [0.15, 0.85, 0.55, 0.55, 0.15, 0.55, 0.85, 0.35, 0.65, 0.35],
'User40': [0.25, 0.85, 0.15, 0.35, 0.25, 0.75, 0.75, 0.75, 0.35, 0.55],
'User6': [0.35, 0.85, 0.25, 0.35, 0.35, 0.45, 0.95, 0.75, 0.45, 0.25],
'User50': [0.45, 0.45, 0.15, 0.15, 0.35, 0.65, 0.65, 0.75, 0.25, 0.65],
'User17': [0.45, 0.65, 0.15, 0.15, 0.25, 0.75, 0.75, 0.95, 0.45, 0.45],
'User2': [0.25, 0.55, 0.25, 0.65, 0.75, 0.75, 0.25, 0.45, 0.15, 0.95],
'User35': [0.15, 0.85, 0.25, 0.45, 0.15, 0.35, 0.65, 0.35, 0.85, 0.75],
'User59': [0.35, 0.75, 0.65, 0.85, 0.35, 0.55, 0.45, 0.05, 0.25, 0.75],
'User41': [0.35, 0.35, 0.25, 0.15, 0.45, 0.65, 0.85, 0.85, 0.65, 0.45],
'User54': [0.65, 0.65, 0.25, 0.35, 0.35, 0.65, 0.85, 0.75, 0.25, 0.25],
'User24': [0.45, 0.65, 0.15, 0.15, 0.25, 0.65, 0.65, 0.95, 0.35, 0.75],
'User60': [0.65, 0.35, 0.15, 0.55, 0.85, 0.75, 0.15, 0.65, 0.15, 0.65],
'User48': [0.35, 0.75, 0.15, 0.25, 0.45, 0.75, 0.55, 0.85, 0.15, 0.55],
'User42': [0.45, 0.25, 0.15, 0.15, 0.65, 0.85, 0.45, 0.85, 0.45, 0.75],
'User1': [0.35, 0.75, 0.35, 0.15, 0.35, 0.45, 0.95, 0.85, 0.65, 0.15],
'User30': [0.45, 0.65, 0.45, 0.65, 0.35, 0.45, 0.45, 0.35, 0.45, 0.65],
'User23': [0.15, 0.75, 0.65, 0.45, 0.15, 0.45, 0.85, 0.25, 0.85, 0.45],
'User20': [0.15, 0.85, 0.35, 0.55, 0.15, 0.55, 0.55, 0.35, 0.85, 0.65],
'User9': [0.45, 0.65, 0.35, 0.25, 0.55, 0.55, 0.75, 0.95, 0.15, 0.25],
'User13': [0.55, 0.45, 0.15, 0.15, 0.35, 0.75, 0.55, 0.95, 0.35, 0.75]}}
ground_truth_group_ratings = {'Group1': (0.4, 0.6, 0.5, 0.5, 0.45, 0.55, 0.45, 0.45, 0.45, 0.65), 'Group2': (
0.30000000000000004, 0.8, 0.325, 0.3, 0.35, 0.6000000000000001, 0.775, 0.6000000000000001, 0.525, 0.42500000000000004),
'Group3': (
0.39285714285714285, 0.5785714285714285, 0.39285714285714285, 0.4642857142857143,
0.3071428571428572, 0.5642857142857143, 0.6214285714285716, 0.4642857142857143,
0.5071428571428571, 0.6785714285714286), 'Group4': (
0.43666666666666665, 0.5433333333333334, 0.35, 0.4900000000000001, 0.5166666666666667, 0.5900000000000001,
0.4433333333333333, 0.5233333333333333, 0.40333333333333327, 0.6566666666666666), 'Group5': (
0.38666666666666666, 0.5966666666666666, 0.32000000000000006, 0.3600000000000001, 0.39999999999999997, 0.61,
0.6033333333333334, 0.6233333333333333, 0.4533333333333333, 0.5999999999999999)}
start_time = time.time()
# Calculate Shapley values
shapley_values = calculate_shapley_values(user_ratings, ground_truth_group_ratings, group_names)
end_time = time.time()
# Calculate the elapsed time
elapsed_time = end_time - start_time
# Print the elapsed time
print(f"Execution time: {elapsed_time} seconds")
# Display results
#for group_name, values in shapley_values.items():
#print(f"Shapley values for {group_name}:")
#for subset, value in values.items():
#print(f"Subset {subset}: {value}")
#print("\n")
# Select top 2 subsets for each group based on Shapley values
top_subsets = select_top_subsets(shapley_values)
# Select top 2 subsets for each group based on Shapley values
top_subsets = select_top_subsets(shapley_values)
# Determine the preferred items for each top subset
preferred_items = {}
for group_name, subsets in top_subsets.items():
preferred_items[group_name] = {}
for subset, _ in subsets:
subset_ratings = {user: user_ratings[group_name][user] for user in subset}
coalition_ratings = combine_user_ratings(subset_ratings)
sorted_indices = np.argsort(coalition_ratings)[::-1]
preferred_items[group_name][subset] = sorted_indices[:3] + 1 # Adding 1 to convert to 1-based indexing
# Display top 2 preferred items for each subset in top 2
for group_name, items in preferred_items.items():
print(f"Top 2 Preferred items for {group_name}:")
for subset, preferred_item_indices in items.items():
print(f"Subset {subset}: Preferred items - {preferred_item_indices}")
print("\n")
# Dictionary to store the top 2 items for each group
top_items_per_group = {}
# Iterate through each group
for group_name, subsets in preferred_items.items():
# Counter to store the counts of items in subsets of the group
counts = Counter()
# Iterate through subsets of the group
for subset, preferred_items in subsets.items():
# Increment counts for each item in the preferred items
counts.update(preferred_items)
# Select the top 2 items with the highest counts
top_items = [item for item, _ in counts.most_common(3)]
# Store the top items for the group
top_items_per_group[group_name] = top_items
print("top_items_per_group",top_items_per_group)
# Display the top 2 items for each group
for group_name, top_items in top_items_per_group.items():
print(f"Top 2 items for {group_name}: {top_items}")
def calculate_user_satisfaction(group_name, top_items_per_group, user_ratings, threshold):
top_items_for_group = top_items_per_group[group_name]
satisfaction_values = []
total_users_in_group = len(user_ratings[group_name])
for item_idx in top_items_for_group:
#print("ratings[item_idx]", item_idx)
print(user_ratings[group_name].items())
satisfied_count = sum(
#1 for user in user_ratings[group_name] if user_ratings[group_name][user][item_idx] >= threshold)
1 for user, ratings in user_ratings[group_name].items() if ratings[item_idx-1] >= threshold)
#print("satisfied_count",satisfied_count)
satisfaction_fraction = satisfied_count / total_users_in_group
# print("satisfaction_fraction",satisfaction_fraction)
satisfaction_values.append(satisfaction_fraction)
print("satisfaction_values",satisfaction_values)
return satisfaction_values
# Calculate and print user satisfaction for each group
threshold = 0.4 # Set your desired threshold value
for group_name in group_names:
satisfaction_values = calculate_user_satisfaction(group_name, top_items_per_group, user_ratings, threshold)
total_satisfaction = sum(satisfaction_values) / len(top_items_per_group[group_name])
print(f"Total User Satisfaction for Group {group_name}: {total_satisfaction * 100:.2f}%")
# Function to calculate precision, recall, f1-score, and accuracy
# Calculate and print user satisfaction for each group
threshold = 0.4 # Set your desired threshold value
total_weighted_satisfaction = 0
total_weight = 0
for group_name in group_names:
satisfaction_values = calculate_user_satisfaction(group_name, top_items_per_group, user_ratings, threshold)
total_satisfaction = sum(satisfaction_values) / len(top_items_per_group[group_name])
total_weighted_satisfaction += total_satisfaction * len(top_items_per_group[group_name])
total_weight += len(top_items_per_group[group_name])
print(f"Total User Satisfaction for Group {group_name}: {total_satisfaction * 100:.2f}%")
# Calculate overall user satisfaction
overall_satisfaction = total_weighted_satisfaction / total_weight
print(f"\nOverall User Satisfaction: {overall_satisfaction * 100:.2f}%")
def calculate_precision1(ground_truth_group_ratings, top_items_per_group, group_names, num_top_items=3, threshold=0.4):
y_true = []
y_pred = []
for group_name in group_names:
# Convert ground truth ratings to a NumPy array
ground_truth_ratings = np.array(ground_truth_group_ratings[group_name])
# Threshold ground truth ratings for the top items
y_true.extend((ground_truth_ratings[:num_top_items] > threshold).astype(int))
# Convert top_items_group to a NumPy array
top_items_group = np.array(top_items_per_group[group_name])
# Threshold predicted ratings for the top items
y_pred.extend((top_items_group[:num_top_items] > threshold).astype(int))
# Convert the lists to numpy arrays
y_true = np.array(y_true)
y_pred = np.array(y_pred)
#print("y_true shape:", y_true.shape)
#print("y_pred shape:", y_pred.shape)
precision = precision_score(y_true, y_pred, average='binary')
recall = recall_score(y_true, y_pred, average='binary')
f1 = f1_score(y_true, y_pred, average='binary')
accuracy = accuracy_score(y_true, y_pred)
return precision, recall,f1,accuracy
# Calculate precision, recall, f1-score, and accuracy
precision, recall, f1, accuracy = calculate_precision1(ground_truth_group_ratings, top_items_per_group, group_names)
print("Precision:", precision)
print("Recall:", recall)
print("F1-score:", f1)
print("Accuracy:", accuracy)