-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContent_Based.py
More file actions
353 lines (257 loc) · 14.9 KB
/
Content_Based.py
File metadata and controls
353 lines (257 loc) · 14.9 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
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
# coding: utf-8
# Content-Based Filtering
#
# This method uses only information about the description and attributes of the projects donors has previously donated to when modeling the donor's preferences. In other words, these algorithms try to recommend projects that are similar to those that a donor has donated to in the past. In particular, various candidate projects are compared with projects the donor has donated to, and the best-matching projects will be recommended.
# In[1]:
import numpy as np
import scipy
import pandas as pd
import math
import random
import sklearn
from nltk.corpus import stopwords
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from scipy.sparse.linalg import svds
# In[2]:
path='/home/dhanashri/Desktop/DataMining/Project_ContentBased/input/'
# Projects.csv
projects = pd.read_csv(path+"Projects.csv")
projects.drop_duplicates(['Project ID'],inplace=True)
#projects_df.head()
# Donations.csv
donations = pd.read_csv(path+"Donations.csv")
donations.drop_duplicates(['Donation ID'],inplace=True)
# Donors.csv
donors=pd.read_csv(path+"Donors.csv")
donors.drop_duplicates(inplace=True)
#donors_df.head()
#Merge donations_df, donors_df, projects_df
donors_df_cols=['Donor ID','Donor City']
donations_df_cols=['Donor ID','Project ID','Donation Amount'] # 'Donation ID'
projects_df_cols=['Project ID','Project Title', 'Project Type','Project Resource Category','Project Subject Category Tree','Project Subject Subcategory Tree']
donations=pd.merge(donations[donations_df_cols],donors[donors_df_cols],
on='Donor ID',
how='outer')
df=pd.merge(donations,projects[projects_df_cols],
on='Project ID',
how='outer')
# City of Donors to focus recommendations on
city="Oakland"
df = df[df['Donor City']==city]
donations_df = df
# In[3]:
# Deal with missing values
donations["Donation Amount"] = donations["Donation Amount"].fillna(0)
# Define event strength as the donated amount to a certain project
donations_df['eventStrength'] = donations_df['Donation Amount']
def smooth_donor_preference(x):
return math.log(1+x, 2)
donations_full_df = donations_df .groupby(['Donor ID', 'Project ID'])['eventStrength'].sum() .apply(smooth_donor_preference).reset_index()
# Update projects dataset
project_cols = projects.columns
# projects = df[project_cols].drop_duplicates()
print('# of projects: %d' % len(projects))
print('# of unique user/project donations: %d' % len(donations_full_df))
# In[4]:
donations_full_df.head()
# To evaluate our models, we will split the donation dataset into training and validation sets.
# In[5]:
donations_train_df, donations_test_df = train_test_split(donations_full_df,
test_size=0.20,
random_state=42)
print('# donations on Train set: %d' % len(donations_train_df))
print('# donations on Test set: %d' % len(donations_test_df))
#Indexing by Donor Id to speed up the searches during evaluation
donations_full_indexed_df = donations_full_df.set_index('Donor ID')
donations_train_indexed_df = donations_train_df.set_index('Donor ID')
donations_test_indexed_df = donations_test_df.set_index('Donor ID')
# In[6]:
# Preprocessing of text data
textfeats = ["Project Title",'Project Resource Category','Project Subject Category Tree','Project Subject Subcategory Tree', 'Project Type']
for cols in textfeats:
projects[cols] = projects[cols].astype(str)
projects[cols] = projects[cols].astype(str).fillna('') # FILL NA
projects[cols] = projects[cols].str.lower() # Lowercase all text, so that capitalized words dont get treated differently
text = projects["Project Title"] + ' ' + projects["Project Resource Category"] + ' ' + projects["Project Type"] + ' ' + projects["Project Subject Category Tree"] + ' ' + projects["Project Subject Subcategory Tree"]
vectorizer = TfidfVectorizer(strip_accents='unicode',
analyzer='word',
lowercase=True, # Convert all uppercase to lowercase
stop_words='english', # Remove commonly found english words ('it', 'a', 'the') which do not typically contain much signal
max_df = 0.9, # Only consider words that appear in fewer than max_df percent of all documents
# max_features=5000 # Maximum features to be extracted
)
project_ids = projects['Project ID'].tolist()
tfidf_matrix = vectorizer.fit_transform(text)
tfidf_feature_names = vectorizer.get_feature_names()
tfidf_matrix
# In[7]:
def get_project_profile(project_id):
idx = project_ids.index(project_id)
project_profile = tfidf_matrix[idx:idx+1]
return project_profile
def get_project_profiles(ids):
project_profiles_list = [get_project_profile(x) for x in np.ravel([ids])]
project_profiles = scipy.sparse.vstack(project_profiles_list)
return project_profiles
def build_donors_profile(donor_id, donations_indexed_df):
donations_donor_df = donations_indexed_df.loc[donor_id]
donor_project_profiles = get_project_profiles(donations_donor_df['Project ID'])
donor_project_strengths = np.array(donations_donor_df['eventStrength']).reshape(-1,1)
#Weighted average of project profiles by the donations strength
donor_project_strengths_weighted_avg = np.sum(donor_project_profiles.multiply(donor_project_strengths), axis=0) / (np.sum(donor_project_strengths)+1)
donor_profile_norm = sklearn.preprocessing.normalize(donor_project_strengths_weighted_avg)
return donor_profile_norm
from tqdm import tqdm
def build_donors_profiles():
donations_indexed_df = donations_full_df[donations_full_df['Project ID'].isin(projects['Project ID'])].set_index('Donor ID')
donor_profiles = {}
for donor_id in tqdm(donations_indexed_df.index.unique()):
donor_profiles[donor_id] = build_donors_profile(donor_id, donations_indexed_df)
return donor_profiles
donor_profiles = build_donors_profiles()
print("# of donors with profiles: %d" % len(donor_profiles))
# In[8]:
mydonor1 = "a5c69797ed95ffa7f18bc69e8540c676"
mydonor1_profile = pd.DataFrame(sorted(zip(tfidf_feature_names,
donor_profiles[mydonor1].flatten().tolist()),
key=lambda x: -x[1])[:10],
columns=['token', 'relevance'])
# In[9]:
mydonor1_profile
# In[10]:
class ContentBasedRecommender:
MODEL_NAME = 'Content-Based'
def __init__(self, projects_df=None):
self.project_ids = project_ids
self.projects_df = projects_df
def get_model_name(self):
return self.MODEL_NAME
def _get_similar_projects_to_donor_profile(self, donor_id, topn=1000):
#Computes the cosine similarity between the donor profile and all project profiles
cosine_similarities = cosine_similarity(donor_profiles[donor_id], tfidf_matrix)
#Gets the top similar projects
similar_indices = cosine_similarities.argsort().flatten()[-topn:]
#Sort the similar projects by similarity
similar_projects = sorted([(project_ids[i], cosine_similarities[0,i]) for i in similar_indices], key=lambda x: -x[1])
return similar_projects
def recommend_projects(self, donor_id, projects_to_ignore=[], topn=10, verbose=False):
similar_projects = self._get_similar_projects_to_donor_profile(donor_id)
#Ignores projects the donor has already donated
similar_projects_filtered = list(filter(lambda x: x[0] not in projects_to_ignore, similar_projects))
recommendations_df = pd.DataFrame(similar_projects_filtered, columns=['Project ID', 'recStrength']).head(topn)
recommendations_df = recommendations_df.merge(self.projects_df, how = 'left',
left_on = 'Project ID',
right_on = 'Project ID')[['Project ID', 'Project Title', 'Project Type', 'Project Resource Category', 'Project Subject Category Tree','Project Subject Subcategory Tree']]
return recommendations_df
# In[11]:
cbr_model = ContentBasedRecommender(projects)
cbr_model.recommend_projects(mydonor1)
# In[ ]:
def get_projects_donated(donor_id, donations_df):
# Get the donor's data and merge in the movie information.
try:
donated_projects = donations_df.loc[donor_id]['Project ID']
return set(donated_projects if type(donated_projects) == pd.Series else [donated_projects])
except KeyError:
return []
#Top-N accuracy metrics consts
EVAL_RANDOM_SAMPLE_NON_INTERACTED_PROJECTS = 100
class ModelEvaluator:
def get_not_donated_projects_sample(self, donor_id, sample_size, seed=42):
donated_projects = get_projects_donated(donor_id, donations_full_indexed_df)
all_projects = set(projects['Project ID'])
non_donated_projects = all_projects - donated_projects
#random.seed(seed)
non_donated_projects_sample = random.sample(non_donated_projects, sample_size)
return set(non_donated_projects_sample)
def _verify_hit_top_n(self, project_id, recommended_projects, topn):
try:
index = next(i for i, c in enumerate(recommended_projects) if c == project_id)
except:
index = -1
hit = int(index in range(0, topn))
return hit, index
def _verify_hit_top_n(self, project_id, recommended_projects, topn):
try:
index = next(i for i, c in enumerate(recommended_projects) if c == project_id)
except:
index = -1
hit = int(index in range(0, topn))
return hit, index
def evaluate_model_for_donor(self, model, donor_id):
#Getting the projects in test set
donated_values_testset = donations_test_indexed_df.loc[donor_id]
if type(donated_values_testset['Project ID']) == pd.Series:
donor_donated_projects_testset = set(donated_values_testset['Project ID'])
else:
donor_donated_projects_testset = set([donated_values_testset['Project ID']])
donated_projects_count_testset = len(donor_donated_projects_testset)
#Getting a ranked recommendation list from a model for a given donor
donor_recs_df = model.recommend_projects(donor_id,
projects_to_ignore=get_projects_donated(donor_id,
donations_train_indexed_df),
topn=1000)
hits_at_3_count = 0
hits_at_5_count = 0
hits_at_10_count = 0
#For each project the donor has donated in test set
for project_id in donor_donated_projects_testset:
#Getting a random sample (100) projects the donor has not donated
#(to represent projects that are assumed to be no relevant to the donor)
non_donated_projects_sample = self.get_not_donated_projects_sample(donor_id,
sample_size=EVAL_RANDOM_SAMPLE_NON_INTERACTED_PROJECTS,
seed=42)
#Combining the current donated project with the 100 random projects
projects_to_filter_recs = non_donated_projects_sample.union(set([project_id]))
#Filtering only recommendations that are either the donated project or from a random sample of 100 non-donated projects
valid_recs_df = donor_recs_df[donor_recs_df['Project ID'].isin(projects_to_filter_recs)]
valid_recs = valid_recs_df['Project ID'].values
#Verifying if the current donated project is among the Top-N recommended projects
hit_at_3, index_at_3 = self._verify_hit_top_n(project_id, valid_recs, 3)
hits_at_3_count += hit_at_3
hit_at_5, index_at_5 = self._verify_hit_top_n(project_id, valid_recs, 5)
hits_at_5_count += hit_at_5
hit_at_10, index_at_10 = self._verify_hit_top_n(project_id, valid_recs, 10)
hits_at_10_count += hit_at_10
#Recall is the rate of the donated projects that are ranked among the Top-N recommended projects,
#when mixed with a set of non-relevant projects
recall_at_3 = hits_at_3_count / float(donated_projects_count_testset)
recall_at_5 = hits_at_5_count / float(donated_projects_count_testset)
recall_at_10 = hits_at_10_count / float(donated_projects_count_testset)
donor_metrics = {'hits@3_count':hits_at_3_count,
'hits@5_count':hits_at_5_count,
'hits@10_count':hits_at_10_count,
'donated_count': donated_projects_count_testset,
'recall@3': recall_at_3,
'recall@5': recall_at_5,
'recall@10': recall_at_10}
return donor_metrics
def evaluate_model(self, model):
#print('Running evaluation for donors')
people_metrics = []
for idx, donor_id in enumerate(list(donations_test_indexed_df.index.unique().values)):
#if idx % 100 == 0 and idx > 0:
# print('%d donors processed' % idx)
donor_metrics = self.evaluate_model_for_donor(model, donor_id)
donor_metrics['_donor_id'] = donor_id
people_metrics.append(donor_metrics)
print('%d donors processed' % idx)
detailed_results_df = pd.DataFrame(people_metrics) .sort_values('donated_count', ascending=False)
global_recall_at_3 = detailed_results_df['hits@3_count'].sum() / float(detailed_results_df['donated_count'].sum())
global_recall_at_5 = detailed_results_df['hits@5_count'].sum() / float(detailed_results_df['donated_count'].sum())
global_recall_at_10 = detailed_results_df['hits@10_count'].sum() / float(detailed_results_df['donated_count'].sum())
global_metrics = {'modelName': model.get_model_name(),
'recall@3': global_recall_at_3,
'recall@5': global_recall_at_5,
'recall@10': global_recall_at_10}
return global_metrics, detailed_results_df
model_evaluator = ModelEvaluator()
# In[ ]:
print('Evaluating Content-Based Filtering model...')
cb_global_metrics, cb_detailed_results_df = model_evaluator.evaluate_model(cbr_model)
print('\nGlobal metrics:\n%s' % cb_global_metrics)
cb_detailed_results_df = cb_detailed_results_df[['_donor_id', 'donated_count', "hits@3_count", 'hits@5_count','hits@10_count',
'recall@3','recall@5','recall@10']]
cb_detailed_results_df.head(5)