mardi 21 février 2017

How to drop items from a python list with a given "drop probability"

I am reasonably new to Python. But not to programming as such. I am writing a simple function to "equalize" a list of float values. In particular, I want to remove some of the redundant 0 values from the list below and do so randomly. As an output, I am expecting the list to keep all the non-zero values and remove some of the zeroes. How many zero values are removed from the list is decided by the "drop_probability". Here's the code I am using:

def drop_zero_steering_angles(training_data, drop_probability=0.75):
"""
Accepts training data and conditionally drops the data with zero steering angle, with 
'drop_probability'
traning_data is a dictionary with angles ranging from -0.5 to 0.5
"""
print("training data length before dropping zeroes:{}".format(len(training_data)))
at_index = -1
for data_point in training_data:
    at_index += 1
    if data_point['angle'] == 0:
        rand = random.uniform(0, 1.0)
        if rand <= drop_probability:
            training_data.pop(at_index)

So, the expectation is that, 'rand' will be a random float and as the drop_probability goes up, it is more likely that the generated 'rand' value will be < drop_probability. But it doesn't appear to work that way. For any value of drop_probability, including drop_probability=1 only 30-40% of the zeroes in the list seem to get popped out. I am baffled by this result. Is there something silly that I am missing?




Aucun commentaire:

Enregistrer un commentaire