更新时间:2022-06-12 00:03:19
mad
的计算可以替换为:
x = np.array([1, 2, 3, 6])
c = np.array([2, 3, 1, 2])
count = np.multiply.outer(c, c)
mad = np.abs(np.subtract.outer(x, x) * count).sum() / count.sum()
np.mean(x)
可以替换为:
np.average(x, weights=c)
以下是全部功能:
def gini(x, weights=None):
if weights is None:
weights = np.ones_like(x)
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)
return 0.5 * rmad
检查结果,gini2()
使用numpy.repeat()
重复元素:
to check the result, gini2()
use numpy.repeat()
to repeat elements:
def gini2(x, weights=None):
if weights is None:
weights = np.ones(x.shape[0], dtype=int)
x = np.repeat(x, weights)
mad = np.abs(np.subtract.outer(x, x)).mean()
rmad = mad / np.mean(x)
return 0.5 * rmad