栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

价值计数的百分位数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

价值计数的百分位数

使用

collections.Counter
用于解决第一个问题(计算,结合频率表)以下朱利安Palard的建议下,我实现了第二个问题(从计算频数表百分位数):

from collections import Counterdef calc_percentiles(cnts_dict, percentiles_to_calc=range(101)):    """Returns [(percentile, value)] with nearest rank percentiles.    Percentile 0: <min_value>, 100: <max_value>.    cnts_dict: { <value>: <count> }    percentiles_to_calc: iterable for percentiles to calculate; 0 <= ~ <= 100    """    assert all(0 <= p <= 100 for p in percentiles_to_calc)    percentiles = []    num = sum(cnts_dict.values())    cnts = sorted(cnts_dict.items())    curr_cnts_pos = 0  # current position in cnts    curr_pos = cnts[0][1]  # sum of freqs up to current_cnts_pos    for p in sorted(percentiles_to_calc):        if p < 100: percentile_pos = p / 100.0 * num while curr_pos <= percentile_pos and curr_cnts_pos < len(cnts):     curr_cnts_pos += 1     curr_pos += cnts[curr_cnts_pos][1] percentiles.append((p, cnts[curr_cnts_pos][0]))        else: percentiles.append((p, cnts[-1][0]))  # we could add a small value    return percentilescnts_dict = Counter()for segment in segment_iterator:    cnts_dict += Counter(segment)percentiles = calc_percentiles(cnts_dict)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号