Python中更有效的加权基尼系数 [英] More efficient weighted Gini coefficient in Python

查看:204
本文介绍了Python中更有效的加权基尼系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 https://stackoverflow.com/a/48981834/1840471 ,这是加权基尼系数的实现Python中的系数:

Per https://stackoverflow.com/a/48981834/1840471, this is an implementation of the weighted Gini coefficient in Python:

import numpy as np
def gini(x, weights=None):
    if weights is None:
        weights = np.ones_like(x)
    # Calculate mean absolute deviation in two steps, for weights.
    count = np.multiply.outer(weights, weights)
    mad = np.abs(np.subtract.outer(x, x) * count).sum() / count.sum()
    rmad = mad / np.average(x, weights=weights)
    # Gini equals half the relative mean absolute deviation.
    return 0.5 * rmad

这很干净,并且对于中型阵列很有效,但是在其最初建议中已警告( https://stackoverflow.com /a/39513799/1840471 )是O(n 2 ).在我的计算机上,这意味着它在大约2万行后会中断:

This is clean and works well for medium-sized arrays, but as warned in its initial suggestion (https://stackoverflow.com/a/39513799/1840471) it's O(n2). On my computer that means it breaks after ~20k rows:

n = 20000  # Works, 30000 fails.
gini(np.random.rand(n), np.random.rand(n))

可以将其调整为适用于较大的数据集吗?我的行约有15万行.

Can this be adjusted to work for larger datasets? Mine is ~150k rows.

推荐答案

此处的版本比您上面提供的版本要快得多,并且在没有权重的情况下也使用简化的公式来获得更快的结果.情况.

Here is a version which is much faster than the one you provided above, and also uses a simplified formula for the case without weight to get even faster results in that case.

def gini(x, w=None):
    # The rest of the code 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

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

Here is some test code to check we get (mostly) the same results:

>>> 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,它已经快得多了:

If you remove the pandas ops from the function, it is already much faster:

%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,但这只会获得百分之几的收益,因为大部分时间都花在了排序上.

If you want to get the last drop of performance you could use numba or cython but that would only gain a few percent because most of the time is spent in sorting.

%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答案中使用的代码

edit: gini_max_ghenis is the code used in Max Ghenis' answer

这篇关于Python中更有效的加权基尼系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆