使用Python计算基尼系数

import numpy as np

def gini(data_list):
    '''获得列表中的gini系数'''
    data_length = len(data_list)
    total_sum = np.sum(data_list)
    total_gini = 0
    for i in range(data_length):
        temp_denominator = data_list[i][0] + data_list[i][1]
        temp_son = np.sum(data_list[i])
        temp_gini = 1 - np.power(data_list[i][0]/temp_denominator, 2) - np.power(data_list[i][1]/temp_denominator, 2)
        total_gini = temp_gini * temp_son / total_sum + total_gini
    return total_gini

测试:
在这里插入图片描述

你可能感兴趣的:(机器学习,算法)