选择性排序

Selective sorting

我是一个 Python 新手,我想实现一个处理二进制或分类列表(对数据集的特征建模)的应急措施 table。对于那些不知道的人,偶然性 table 是一个矩阵,在通用元素 m_ij 中有一个数字指定第一个特征的元素 i 在对第二个特征的元素 j 的相同观察。 很明显,每个特征的每个元素(取一次)都应该成为一行或一列 header。 我的问题是当我处理二进制特征时。在这种情况下,偶然性 table 必须具有作为 header 的一对 (1,0) 在这个严格的序列中。

_|1|0|
1| | |
0| | |

虽然,使用我编写的代码不能保证这种刚性:如果二进制特征的第一个元素为 0,则相对 header 不会以 1 开头。

查看我的代码:

def compute_contingency_table(first_f, second_f):
'''
This method compute contingency table of two features
:param first_f: first feature
:param second_f: second feature
:return: the contingency table
'''

first_values = get_values(first_f)
second_values = get_values(second_f)
contingency_table = np.zeros([len(first_values), len(second_values)])
corresponding_values = []

# for each value of the first feature
for h in range(len(first_values)):

    # find all the indeces in which it occurs
    f_indices = [i for i, x in enumerate(first_f) if x == second_f[h]]

    # save the corresponding values in the second feature
    for ind in f_indices:
        corresponding_values.append(second_f[ind])

    # createing contingency_table
    # for each value in corresponding values of the second feature
    for val in corresponding_values:
        # take its index in the values list (i.e. the column of contingency table)
        k = second_values.index(val)

        # increment the value of the corresponding contingency table element
        contingency_table[h, k] += 1

    del corresponding_values[:]

return contingency_table

用例:

first_f=[1,0,0,0,0,0,0]
second_f=[0,1,0,0,0,1,0]

应急事件 table 我的代码输出:

[[ 4.  2.]
 [ 1.  0.]]

虽然应该是:

 [[ 0.  1.]
 [ 2.  4.]]

如您所见,发生这种情况是因为输出 table 的类型为

_|0|1|
0| | |
1| | |

如果用二进制以 (1,0) 方式对 header 进行排序,它应该可以工作;如果它们是教义的,则没有。这就是我所说的选择性排序。

如果您好奇 table 的偶然事件是如何在 Pandas 中完成的:

import pandas as pd

df = pd.DataFrame()
df['first'] = [1,0,0,0,0,0,0]
df['second'] = [0,1,0,0,0,1,0]
contingency_table = df.groupby(['first', 'second']).size().unstack(fill_value=0)

contingency_table = pd.crosstab(df['first'], df['second'])

关于排序,在 compute_contingency_table 中执行以下二进制值的情况下交换顺序应该就足够了。

first_values = list(set(first_f))
if len(first_values) == 2:
    first_values = sorted(first_values, reverse=True)
second_values = list(set(second_f))
if len(second_values) == 2:
    second_values = sorted(second_values, reverse=True)

这样完成的:

def compute_contingency_table(first_f, second_f):
'''
This method compute contingency table of two features
:param first_f: first feature
:param second_f: second feature
:return: the contingency table
'''


first_values = get_values(first_f)
second_values = get_values(second_f)

if first_values == [0,1]:
    first_values = [1,0]
if second_values == [0,1]:
    second_values = [1,0]

contingency_table = np.zeros([len(first_values), len(second_values)])
corrisponding_values = []
for i in range(len(first_values)):

    f_indices = [k for k, x in enumerate(first_f) if x == first_values[i]]
    for ind in f_indices:
        corrisponding_values.append(second_f[ind])

    for s_val in corrisponding_values:
        k = second_values.index(s_val)
        contingency_table[i, k] += 1
    del corrisponding_values[:]

return contingency_table

用例 1:

hair=['black', 'blonde', 'red', 'blonde', 'red', 'red', 'brown']
country = ['usa', 'china', 'usa', 'germany', 'germany','china', 'usa']
print(compute_contingency_table(hair,country))

输出

[[ 1.  0.  0.]
 [ 0.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  0.]]

用例 2:

a = [1, 0, 0, 0, 0, 0, 0]
b = [0, 0, 0, 1, 1, 0, 0]
print(compute_contingency_table(a,b))

输出

[[ 0.  1.]
 [ 2.  4.]]