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

Python中更有效的加权基尼系数

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

Python中更有效的加权基尼系数

这是一个比您上面提供的版本快得多的版本,并且在没有权重的情况下也使用简化的公式来获得更快的结果。

def gini(x, w=None):    # The rest of the pre requires numpy arrays.    x = np.asarray(x)    if w is not None:        w = np.asarray(w)        sorted_indices = np.argsort(x)        sorted_x = x[sorted_indices]        sorted_w = w[sorted_indices]        # Force float dtype to avoid overflows        cumw = np.cumsum(sorted_w, dtype=float)        cumxw = np.cumsum(sorted_x * sorted_w, dtype=float)        return (np.sum(cumxw[1:] * cumw[:-1] - cumxw[:-1] * cumw[1:]) /      (cumxw[-1] * cumw[-1]))    else:        sorted_x = np.sort(x)        n = len(x)        cumx = np.cumsum(sorted_x, dtype=float)        # The above formula, with all weights equal to 1 simplifies to:        return (n + 1 - 2 * np.sum(cumx) / cumx[-1]) / n

这是一些测试代码以检查我们是否(大致)获得相同的结果:

>>> x = np.random.rand(1000000)>>> w = np.random.rand(1000000)>>> gini_max_ghenis(x, w)0.33376310938610521>>> gini(x, w)0.33376310938610382

但是速度却大不相同:

%timeit gini(x, w)203 ms ± 3.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)%timeit gini_max_ghenis(x, w)55.6 s ± 3.35 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

如果从函数中删除pandas ops,它已经快得多了:

%timeit gini_max_ghenis_no_pandas_ops(x, w)1.62 s ± 75 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

如果您想获得最后的性能下降,可以使用numba或cython,但这只会获得百分之几的收益,因为大部分时间都花在了排序上。

%timeit ind = np.argsort(x); sx = x[ind]; sw = w[ind]180 ms ± 4.82 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

编辑 :gini_max_ghenis是Max Ghenis的答案中使用的代码



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

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

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