'Decoding the python function - Not able to understand what the function's inner workings are
I'm trying to understand the below code and deduce some kind of a pattern to see what this function this, can you please help me with this?
from math import floor
from itertools import groupby
def function3(l, n):
assert(len(l) > 0 and n > 0)
minimum = min(l)
maximum = max(l)
d = (maximum - minimum) / n
values = [(floor((l[i] - minimum) / d)) for i in range(len(l))]
dictionary = dict([(key, len(list(group))) for key, group in groupby(sorted(values))])
return [(minimum+(i+0.5)*d, dictionary.get(i, 0)) for i in range(0,n,1)]
Solution 1:[1]
I don't have a full explanation of the background of your snippet, but hopefully I can shed some light on the programming going on.
Let's go through it step by step:
# import statements
from math import floor # method to round down to the nearest integer, see [here][1]
from itertools import groupby # method to group by keys, see [here][2]
def function3(l, n):
# as thebadgateway said, this looks like the standard score,
# with l being the interval and n the number of values
assert(len(l) > 0 and n > 0) # tests to see that both length of l and n are > 0
minimum = min(l) # self-explanatory
maximum = max(l)
d = (maximum - minimum) / n # this is a kind of mid-range of the input, see [here][3]
values = [(floor((l[i] - minimum) / d)) for i in range(len(l))]
# this (above) takes each element in l, subtracts the minimum
# and divides the result by our "mid-range" d for all elements in l
dictionary = dict([(key, len(list(group))) for key, group in groupby(sorted(values))])
# this (above) groups the computed values by identity,
# and fills a dictionary with the keys and length of the groups
return [(minimum+(i+0.5)*d, dictionary.get(i, 0)) for i in range(0,n,1)]
# this computes the minimum + the counting variable + 0.5 * the "mid-range",
# puts it into a tuple with the same i-index in the dictionary (0 if element is not found)
# it does this for n steps with a step size of 1 and returns the whole list
I hope this helps! [1]: https://en.wikipedia.org/wiki/Standard_score [2]: https://www.geeksforgeeks.org/itertools-groupby-in-python/ [3]: https://en.wikipedia.org/wiki/Mid-range
Solution 2:[2]
This function looks like it’s producing something akin to the z-score in statistics in the values list, then creating a dict of z-score, z-score count key value pairs in dictionary, and then possibly returning n-tiles. I would review z-score, and quantiles with, eg, Wikipedia to verify.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | thebadgateway |
