arrays - Python: Calculate metrics from list of APIs -
i have array of data looks this:
#api name, min, max, average ['findproductbypartnumber', '336.0', '336.0', '336.0'] ['findproductbypartnumber', '336.0', '339.0', '337.5'] ['findproductbypartnumber', '336.0', '339.0', '338.0'] ['findproductbypartnumber', '336.0', '341.0', '338.75'] ['findproductbypartnumber', '336.0', '353.0', '341.6'] ['findproductbyid', '841.0', '841.0', '841.0'] ['findproductbypartnumber', '336.0', '920.0', '438.0'] ['findproductbypartnumber', '336.0', '944.0', '510.29'] ['findproductbypartnumber', '336.0', '952.0', '565.5'] ['findproductbypartnumber', '336.0', '975.0', '611.0'] ['findproductsbycategory', '113.0', '113.0', '113.0'] ['findproductbyid', '161.0', '841.0', '501.0'] ['findproductbypartnumber', '255.0', '975.0', '575.4']
what want is, each individual api, generate this:
api, min, max, average, 90th percentile findproductbypartnumber, 278.69, 770.25, 458.69, 565.5 findproductbyid, 373.0, 841.0, 571.67, 501.0 findproductsbycategory, 112.33, 187.17, 154.46, 167.75
this aggregated results of each api above. best way in python?
edit:
i have following java code what want. java best language , trying learn python , i'm not familiar data structures.
double[] apivalues = new double[3]; apivalues[0] = double.valueof(min); apivalues[1] = double.valueof(max); apivalues[2] = double.valueof(average); parseapilogs.registerapi(name, apivalues); ... ... private static void registerapi(string apiname, double[] apivalues) { if(!averageperapi.containskey(apiname)) { apidata data = new apidata(); data.addvalues(apivalues); averageperapi.put(apiname, data); } else { averageperapi.get(apiname).addvalues(apivalues); } }
the apidata java class little big past here, can see idea going with.
how this:
def aggregate(stats): aggregated = {} stat in stats: key = stat.pop(0) stat = map(float, stat) if key not in aggregated: vals = {"avg": []} aggregated[key] = vals aggregated[key]['min'] = min(stat[0], aggregated[key].setdefault('min', stat[0])) aggregated[key]['max'] = max(stat[1], aggregated[key].setdefault('max', stat[1])) aggregated[key]['avg'].append(stat[2]) return aggregated def print_stats(aggregated): k, v in aggregated.items(): print k, k1, v1 in v.items(): if k1 == 'avg': print "%s: %s" % (k1, sum(v1) / len(v1)), else: print "%s: %s" % (k1, v1), print stats = [ ['findproductbypartnumber', '336.0', '336.0', '336.0'], ['findproductbypartnumber', '336.0', '339.0', '337.5'], ['findproductbypartnumber', '336.0', '339.0', '338.0'], ['findproductbypartnumber', '336.0', '341.0', '338.75'], ['findproductbypartnumber', '336.0', '353.0', '341.6'], ['findproductbyid', '841.0', '841.0', '841.0'], ['findproductbypartnumber', '336.0', '920.0', '438.0'], ['findproductbypartnumber', '336.0', '944.0', '510.29'], ['findproductbypartnumber', '336.0', '952.0', '565.5'], ['findproductbypartnumber', '336.0', '975.0', '611.0'], ['findproductsbycategory', '113.0', '113.0', '113.0'], ['findproductbyid', '161.0', '841.0', '501.0'], ['findproductbypartnumber', '255.0', '975.0', '575.4'] ] print_stats(aggregate(stats))
output
findproductsbycategory max: 113.0 avg: 113.0 min: 113.0 findproductbyid max: 841.0 avg: 671.0 min: 161.0 findproductbypartnumber max: 975.0 avg: 439.204 min: 255.0
as far 90th percentile, isn't possible calculate without individual samples unless use .9 * avg.
Comments
Post a Comment