Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

Python - Sample one element randomly from a list based on the unique elements of another list

I have 2 lists containing user_ids and item_ids. I want to sample one item for each user randomly.

For Ex.

user_ids = [1,2,3 ,1, 2]
item ids = [8,9,10,5,8]

I want to get -

val_user_ids  = [1,2,3]
val_item_ids = [5,9,10]

I know some inefficient ways like looping etc. Is there any efficient way to do so? Or is there exist any python function for the same?

To be precise, I want to create a validation set (from the training set) containing 1 item interaction for each user.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can gather your data in a dictionary with the user_id as the key and the item_ids in a list as the value

import collections

user_ids = [1, 2, 3, 1, 2]
item_ids = [8, 9, 10, 5, 8]

data = collections.defaultdict(list)
for key, value in zip(user_ids, item_ids):
    data[key].append(value)

The result is defaultdict(<class 'list'>, {1: [8, 5], 2: [9, 8], 3: [10]}).

Now we can loop over the dictionary and get a random item from the list.

import random
result = [(key, random.choice(value)) for key, value in data.items()]

The result is [(1, 8), (2, 9), (3, 10)] (or [(1, 8), (2, 8), (3, 10)] or whatever the randomization will give us).


Some more information concerning the defaultdict. This kind of dictionary will create a default item if it doesn't exist. The default is given as a parameter when creating the defaultdict. Using a standard dict we have to handle the creation of the entry ourselves.

This is how it would be done manually:

user_ids = [1, 2, 3, 1, 2]
item_ids = [8, 9, 10, 5, 8]

data = dict()
for key, value in zip(user_ids, item_ids):
    if key not in data:
        data[key] = []
    data[key].append(value)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...