More efficient weighted Gini coefficient in Python

后端 未结 2 1052
夕颜
夕颜 2020-12-18 00:27

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, weight         


        
2条回答
  •  离开以前
    2020-12-18 01:20

    Adapting the StatsGini R function from here:

    import numpy as np
    import pandas as pd
    
    def gini(x, w=None):
        # Array indexing requires reset indexes.
        x = pd.Series(x).reset_index(drop=True)
        if w is None:
            w = np.ones_like(x)
        w = pd.Series(w).reset_index(drop=True)
        n = x.size
        wxsum = sum(w * x)
        wsum = sum(w)
        sxw = np.argsort(x)
        sx = x[sxw] * w[sxw]
        sw = w[sxw]
        pxi = np.cumsum(sx) / wxsum
        pci = np.cumsum(sw) / wsum
        g = 0.0
        for i in np.arange(1, n):
            g = g + pxi.iloc[i] * pci.iloc[i - 1] - pci.iloc[i] * pxi.iloc[i - 1]
        return g
    

    This works for large vectors, at least up to 10M rows:

    n = 1e7
    gini(np.random.rand(n), np.random.rand(n))  # Takes ~15s.
    

    It also produces the same result as the function provided in the question, for example giving 0.2553 for this example:

    gini(np.array([3, 1, 6, 2, 1]), np.array([4, 2, 2, 10, 1]))
    

提交回复
热议问题