Evaluation (Precision @1)

 avatar
unknown
python
5 months ago
1.1 kB
5
Indexable
import json
import argparse

if __name__ == "__main__":
    # 使用argparse解析命令列參數
    parser = argparse.ArgumentParser(description='Process some paths and files.')
    parser.add_argument('--ground_truth_path', default='../dataset/preliminary/ground_truths_example.json', type=str, help='The json file to ground truth')  
    parser.add_argument('--pred_path', default='preds/output.json', type=str, help='The json file to predictions') 

    args = parser.parse_args()  # 解析參數

    with open(args.ground_truth_path, 'r') as file:
      data = json.load(file)
      ground_truth = data['ground_truths']
    
    with open(args.pred_path, 'r') as file:
      data = json.load(file)
      answers = data['answers']

    if (len(ground_truth) != len(answers)):
      raise ValueError("The number of questions in ground truth and prediction files are not same")
    
    n = len(ground_truth)
    c = 0

    for i in range(n):
      if (ground_truth[i]['retrieve'] == answers[i]['retrieve']):
        c += 1
    
    print(f'Accuracy: {c / n * 100}%')
Editor is loading...
Leave a Comment