0

我有一个这样的数据框:

Interesting           genre_1        probabilities
    1    no            Empty        0.251306
    2    yes           Empty        0.042043
    3     no          Alternative    5.871099
    4    yes         Alternative    5.723896
    5    no           Blues         0.027028
    6    yes          Blues         0.120248
    7    no          Children's     0.207213
    8    yes         Children's     0.426679
    9    no          Classical      0.306316
    10    yes         Classical      1.044135

我想根据有趣的列对同一类别执行 GINI 索引。之后,我想在新的 pandas 列中添加这样的值。

这是获取基尼指数的函数:

#Gini Function
#a and b are the quantities of each class
def gini(a,b):
    a1 = (a/(a+b))**2
    b1 = (b/(a+b))**2
    return 1 - (a1 + b1) 

编辑* 抱歉,我的最终所需数据框中有错误。在选择 prob(A) 和 prob(B) 时,是否有趣并不重要,但 Gini 分数将是相同的,因为它将衡量我们将多少杂质归类为有趣或不有趣的歌曲。因此,如果概率在 50/50% 左右,则意味着 Gini 分数将达到最大值(0.5),这是因为同样可能会被错误地选择是否有趣。

所以对于前两行,基尼指数将是:

a=no; b=Empty -> gini(0.251306, 0.042043)= 0.245559831601612
a=yes; b=Empty -> gini(0.042043, 0.251306)= 0.245559831601612

然后我想得到类似的东西:

 Interesting           genre_1        percentages.  GINI INDEX
        1    no            Empty        0.251306         0.245559831601612
        2    yes           Empty        0.042043         0.245559831601612
        3     no          Alternative    5.871099         0.4999194135183881
        4    yes         Alternative    5.723896.     0.4999194135183881
        5    no           Blues         0.027028          ..
        6    yes          Blues         0.120248
        7    no          Children's     0.207213
        8    yes         Children's     0.426679
        9    no          Classical      0.306316          ..
        10    yes         Classical      1.044135         ..
4

2 回答 2

1

我不确定该Interesting列是如何影响所有这些的,但我强烈建议您使用numpy.where(). 语法类似于:

import numpy as np
df['GINI INDEX'] = np.where(__condition__,__what to do if true__,__what to do if false__)
于 2020-02-11T01:45:00.250 回答
1

好吧,我想我知道你的意思了。下面的代码并不关心,如果 Interesting 值是“是”或“否”。但是您想要的是根据该行的 Interesting 值中的值以两种不同的方式计算每行的 GINI 系数。因此,如果有趣 == 否,则结果为 0.5,因为 a == b。但如果有趣的是“是”,那么您需要使用 a =probability[i] 和 b =probability[i+1]。因此,请跳过本节以获取下面的更新代码。

import pandas as pd


df = pd.read_csv('df.txt',delim_whitespace=True)

probs = df['probabilities']


def ROLLING_GINI(probabilities):

    a1 = (probabilities[0]/(probabilities[0]+probabilities[0]))**2
    b1 = (probabilities[0]/(probabilities[0]+probabilities[0]))**2
    res = 1 - (a1 + b1)
    yield res

    for i in range(len(probabilities)-1):
        a1 = (probabilities[i]/(probabilities[i]+probabilities[i+1]))**2
        b1 = (probabilities[i+1]/(probabilities[i]+probabilities[i+1]))**2
        res = 1 - (a1 + b1)
        yield res


df['GINI'] = [val for val in ROLLING_GINI(probs)]

print(df)

这是真正的麻烦开始的地方,因为如果我正确理解您的想法,那么您将无法计算最后的 GINI 值,因为您的数据框不允许它。这里重要的一点是数据框中最后一个有趣的值是“是”。这意味着我必须使用 a =probability[i] 和 b =probability[i+1]。但是您的数据框没有第 11 行。您有 10 行并且在第 i == 10 行上,您需要第 11 行中的概率来计算 GINI 系数。所以为了让你的想法起作用,最后一个有趣的值必须是“否”,否则你总是会得到一个索引错误。

无论如何,这是代码:

import pandas as pd

df = pd.read_csv('df.txt',delim_whitespace=True)


def ROLLING_GINI(dataframe):

    probabilities = dataframe['probabilities']
    how_to_calculate = dataframe['Interesting']

    for i in range(len(dataframe)-1):

        if how_to_calculate[i] == 'yes':
            a1 = (probabilities[i]/(probabilities[i]+probabilities[i+1]))**2
            b1 = (probabilities[i+1]/(probabilities[i]+probabilities[i+1]))**2
            res = 1 - (a1 + b1)
            yield res

        elif how_to_calculate[i] == 'no':
            a1 = (probabilities[i]/(probabilities[i]+probabilities[i]))**2
            b1 = (probabilities[i]/(probabilities[i]+probabilities[i]))**2
            res = 1 - (a1 + b1)
            yield res


GINI = [val for val in ROLLING_GINI(df)]

print('All GINI coefficients: %s'%GINI)
print('Length of all calculatable GINI coefficients: %s'%len(GINI))
print('Number of rows in the dataframe: %s'%len(df))
print('The last Interesting value is: %s'%df.iloc[-1,0])

编辑三号(抱歉迟到了):

因此,如果我正确应用索引,它确实有效。问题是我想使用下一个概率,而不是前一个概率。所以它是 a = probabilities[i-1] 和 b = probabilities[i]

import pandas as pd

df = pd.read_csv('df.txt',delim_whitespace=True)


def ROLLING_GINI(dataframe):

    probabilities = dataframe['probabilities']
    how_to_calculate = dataframe['Interesting']

    for i in range(len(dataframe)):

        if how_to_calculate[i] == 'yes':
            a1 = (probabilities[i-1]/(probabilities[i-1]+probabilities[i]))**2
            b1 = (probabilities[i]/(probabilities[i-1]+probabilities[i]))**2
            res = 1 - (a1 + b1)
            yield res

        elif how_to_calculate[i] == 'no':
            a1 = (probabilities[i]/(probabilities[i]+probabilities[i]))**2
            b1 = (probabilities[i]/(probabilities[i]+probabilities[i]))**2
            res = 1 - (a1 + b1)
            yield res


GINI = [val for val in ROLLING_GINI(df)]

print('All GINI coefficients: %s'%GINI)
print('Length of all calculatable GINI coefficients: %s'%len(GINI))
print('Number of rows in the dataframe: %s'%len(df))
print('The last Interesting value is: %s'%df.iloc[-1,0])
于 2020-02-11T02:11:32.700 回答