我是一个Python新手,我想实现一个处理二进制或分类列表(模拟数据集特征)的列联表。对于那些不知道的人,列联表是一个矩阵,在通用元素m_ij
中有一个数字,用于指定第一个要素的元素i
在同一个维护中的次数第二个特征的元素j
。
很清楚,每个要素的每个元素(一次)都应该成为行或列标题。
我的问题是当我处理二进制功能时。在这种情况下,列联表必须具有这个刚性序列中的偶数(1,0)作为标题。
_|1|0|
1| | |
0| | |
虽然使用我编写的代码,但不能保证这种刚性:如果二进制特征的第一个元素为0,则相对标题不会以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]
我的代码输出的列联表:
[[ 4. 2.]
[ 1. 0.]]
虽然它应该是:
[[ 0. 1.]
[ 2. 4.]]
如您所见,这是因为输出表的类型为
_|0|1|
0| | |
1| | |
如果用二进制对(1,0)中的标题进行排序,它应该有效;没有排序,如果他们是caterogical。这就是我选择性排序的意思。
答案 0 :(得分:1)
如果您对如何在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)
答案 1 :(得分:0)
以这种方式完成:
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.]]