我有一个包含3000行和3列的数据框,如下所示:
0 col1 col2 col3
ID1 1 0 1
Id2 1 1 0
Id3 0 1 1
Id4 2 1 0
Id5 2 2 3
… .. .. ..
Id3000 3 1 0
在此数据帧中,每一列和每一行的值表示预测问题的结果,如下所示:每列中0表示TP,1表示FP,2表示TN,3指向FN。所以我想计算每列的准确性。像这样的东西:
Accuracy result:
col1 col2 col3
0.67 0.68 0.79
任何想法都可以以非常有效的方式计算出重要的指标,例如准确性或f度量。
答案 0 :(得分:1)
这是一种方法:
data = """
id col1 col2 col3
ID1 1 0 1
Id2 1 1 0
Id3 0 1 1
Id4 2 1 0
Id5 2 2 3
"""
#coding to create a sample DataFrame for testing
df = pd.read_csv(pd.compat.StringIO(data), sep='\s+')
print(df)
#end of creation
accuracy ={} #dict for result final
# i select all columns with name begins by 'col' and create a list
select_cols = [col for col in df.columns if col.startswith('col')]
for col in select_cols:
df1 = df.groupby(col).size()
t = [0,0,0,0] #[TP, FP, TN, FN] 0 = TP, 1 = FP, 2 = TN and 3 = FN
for v in df1.index:
t[v] = df1[v]
accuracy[col] = (t[0] + t[2])/(sum(t)) #Accuracy = (TP + TN)/(TP +TN + FP + FN
df_acc = pd.DataFrame.from_dict(accuracy, orient='index').T
print('Accuracy:');print(df_acc)
输出:
Accuracy:
col1 col2 col3
0 0.6 0.4 0.4
或另一种解决方案(我认为更好):您替换了两个循环for
for col in select_cols:
accuracy[col] = (df[df[col]==0].count()[0] + df[df[col]==2].count()[0]) / df[col].count()
df_acc = pd.DataFrame.from_dict(accuracy, orient='index' ).T.reset_index(drop=True)
print('Accuracy');print(df_acc)