我有两个目录:'ann-1‘和'ann-2',在这两个目录中我有文本文件和.ann文件。有没有一种方法可以使用第一个目录中的所有.ann文件作为y1值,使用第二个目录中的所有.ann文件作为y2值?
sklearn.metrics.cohen_kappa_score(y1, y2, *, labels=None, weights=None, sample_weight=None)我真的不确定我是否正确地解释了scikit的文档,因为他们的文档不是很详细。
发布于 2021-05-05 11:07:38
如果您想了解有关.ann文件的更多详细信息,请向我们展示其中的内容。如果您添加更多详细信息,我将更新答案。
现在,为了计算Cohen的Kappa系数,您需要将两个相同长度的类似数组的结构传递给函数。第一个数组必须是自变量的观测值,第二个数组必须包含预测标签。下面是一个与user guide中的示例类似的示例
# Import function
from sklearn.metrics import cohen_kappa_score
# y1 and y2 as lists
y_true = [0, 0, 0, 1]
y_pred = [0, 0, 1, 1]
# Calculate coefficient
k = cohen_kappa_score(y_true, y_pred)为了将来自ann-1的.ann文件存储在y1中,将来自ann-2的.ann文件存储在y2中,您可以尝试以下内容(您没有显示目录的结构或文件的内容,因此它可能会工作,也可能不会工作,但您可以根据需要修改代码):
# Import file browser library
import os
# Declare paths to each directory
path_1 = './path_to_ann-1/'
path_2 = './path_to_ann-2/'
# Lists of files in each folder
ann_1 = os.listdir(path_1)
ann_2 = os.listdir(path_2)
# For loop to read files and calculate metric
for file_1, file_2 in zip(ann_1, ann_2):
# Check if they're .ann or .txt files
if file_1[-4:] == '.ann' and file_2[-4:] == '.ann':
# Change separator and columns as you see fit
y1 = pd.read_csv(path_1 + file_1, sep='|')['target'].values
y2 = pd.read_csv(path_2 + file_2, sep='|')['perdiction'].values
# Calculate metric
m = cohen_kappa_score(y1, y2)
# Print metric
print(file_1, 'and', file_2, 'yield', m)https://stackoverflow.com/questions/67384318
复制相似问题