Home > Mobile >  How to extract randomically elements from a dictionary considerating a value's attribute in Pyt
How to extract randomically elements from a dictionary considerating a value's attribute in Pyt

Time:11-09

I want to extract, randomically, an element from a dictionary considering the frequency value: I want the output to be one of the highest frequency value everytime BUT it's not excluded that an element with low frequency value is extracted.

Like, if I have "x": 4.5, "y": 7.1, "z": 9.3, "w": 1.2, "k": 5.8, "p": 2.3

I want my output to be often "z", "y", sometimes "x", "k" and seldom "w","p" (hope this makes sense)

{Kitchen_Activity : {'near the bathroom sink': {'frequency': 0, 'average duration': 0, 'standard deviation': 0}, 'near the fridge': {'frequency': 0.2631578947368421, 'average duration': Timedelta('0 days 00:00:08.200000'), 'standard deviation': Timedelta('0 days 00:00:08.288546314')}, 'near the stove': {'frequency': 0.2631578947368421, 'average duration': Timedelta('0 days 00:00:04.200000'), 'standard deviation': Timedelta('0 days 00:00:00.836660026')}, 'on the bed': {'frequency': 0, 'average duration': 0, 'standard deviation': 0}, 'near the shower': {'frequency': 0, 'average duration': 0, 'standard deviation': 0}, 'at the kitchen entrance from the hallway': {'frequency': 0.10526315789473684, 'average duration': Timedelta('0 days 00:00:05'), 'standard deviation': Timedelta('0 days 00:00:01.414213562')}, 'at the bedroom entrance': {'frequency': 0, 'average duration': 0, 'standard deviation': 0}}
Read: {...}
Sleep: {...}
}

In this snippet of my dictionary, I have some elements with frequency = 0 and some with a specific value. Is there a way to extract, randomically, one of this element like 'near the fridge' considering the frequency?

I tried using random.choices() but I think I'm not using it correctly because I get TypeError: '<' not supported between instances of 'float' and 'Timedelta' and others errors. Thanks!

CodePudding user response:

You can do something like this:

import numpy as np

kitchen_activity =  {'near the bathroom sink':{'frequency': 0, 'average duration': 0, 'standard deviation': 0},
                    'near the fridge': {'frequency': 0.2631578947368421}, 
                    'near the stove': {'frequency': 0.2631578947368421},
                    'on the bed': {'frequency': 0, 'average duration': 0, 'standard deviation': 0},
                    'near the shower': {'frequency': 0, 'average duration': 0, 'standard deviation': 0},
                    'at the kitchen entrance from the hallway': {'frequency': 0.10526315789473684},
                    'at the bedroom entrance': {'frequency': 0, 'average duration': 0, 'standard deviation': 0}}


keys = [] #list for keys
freq = [] #list for frequencies
for k,v in kitchen_activity.items():
    keys.append(k)
    freq.append(v['frequency']) 

freq = [f/sum(freq) for f in freq] #normalize the frequency
    
print(np.random.choice(keys, 100, p=freq))
  • Related