recommenders icon indicating copy to clipboard operation
recommenders copied to clipboard

[ASK] How to get top recommendations for a given user id - LSTURModel

Open sivaramaaa opened this issue 1 year ago • 4 comments

Description

Hi , I am trying to get top 10 news recommendations for given user id . Below is the code which i am using for it :

iterator = MINDIterator
model = LSTURModel(hparams, iterator, seed=seed)
print(model.run_eval(valid_news_file, valid_behaviors_file))
model.fit(train_news_file, train_behaviors_file, valid_news_file, valid_behaviors_file)


news_vecs = model.run_news(valid_news_file)
user_vecs = model.run_user(valid_news_file, valid_behaviors_file)

uid = list(user_vecs.keys())[10] # This a random test user id for which i need news recommendations
user_vec = user_vecs[uid]
scores = {news_id: np.dot(user_vec, news_vec) for news_id, news_vec in news_vecs.items()}
top_10_news = sorted(scores, key=scores.get, reverse=True)[:10]

print("Top 10 news recommendations for user:", uid)
print(top_10_news)

So the output i get is an array of id's like this : [12580, 10755, 13550, 2500, 5302, 4810, 18519, 18187, 3741, 13954]

I am looking to convert these id's to Actual New item id's like : N15366 , N18191 etc -- using which i could get the News titles .

Any help would be greatly appreciated .

sivaramaaa avatar Sep 26 '23 11:09 sivaramaaa

Are these id's same as News id's present in MIND dataset with the "N" removed ?

I also feel the way i am getting userid is not the ideal way , i am just inputting a Vector index . So i would like to know how to give user id like 'U28498' as input to perform the prediction .

sivaramaaa avatar Sep 26 '23 11:09 sivaramaaa

In the MINDIterator, there are two attributes, uid2index and nid2index, which respectively keep track of mappings from user IDs to indices and news IDs to indices. It's important to note that the numbers you input and obtain are indeed indices. To retrieve the corresponding user ID or news ID, you should use these dictionaries for mapping.

cclinet avatar Nov 03 '23 03:11 cclinet

Hi @cclinet :

Thanks a lot for the reply , but i had tried accessing nid2index at the time of filling this issue itself , it was not accessible as i guess it's a private property which is not exposed :

iterator = MINDIterator
nid2index = iterator.nid2index
index2nid = {index: nid for nid, index in nid2index.items()}
top_10_news_ids = [index2nid[index] for index in top_10_news]
print("Top 10 news recommendations for user:", uid)
print(top_10_news_ids)

This is the error i got :

AttributeError: type object 'MINDIterator' has no attribute 'nid2index'

If possible can you let me know if there is a way to access this property .

sivaramaaa avatar Nov 07 '23 12:11 sivaramaaa

Try to get the iterator using model.train_iterator or model.test_iterator

cclinet avatar Nov 08 '23 09:11 cclinet