鉴于两个长度相等的列表,我如何以真正的pythonic方式构造contingency table? 我从skit-learn了解confusion_matrix,但有“手动”和有效的方法吗?
答案 0 :(得分:2)
您可以使用pandas库来创建维基百科示例中显示的表,如下所示,
import pandas as pd
right_handed = [43, 44]
left_handed = [9,4]
df = pd.DataFrame({'right': right_handed, 'left': left_handed}, index = ['males', 'females'])
这会产生一个DataFrame,就像这样,
In [3]:
print (df)
left right
males 9 43
females 4 44
然后,您可以使用sum来获得总数,
print (df.left.sum())
print (df.right.sum())
13
87
In [7]:
print (df.ix['males'].sum())
print (df.ix['females'].sum())
52
48