데이스쿨 할인 리턴즈
분석시각화 대회 코드 공유 게시물은
내용 확인 후
좋아요(투표) 가능합니다.
평가 산식(Metric) 코드
안녕하세요 '제3회 국민대학교 AI빅데이터 분석 경진대회' 참가자 여러분.
본 대회의 리더보드 평가 산식(Metric) 코드를 제공합니다.
import numpy as np
import pandas as pd
def _validate_input(answer_df, submission_df):
# ① 컬럼 개수·이름 일치 여부
if len(answer_df.columns) != len(submission_df.columns) or not all(answer_df.columns == submission_df.columns):
raise ValueError("The columns of the answer and submission dataframes do not match.")
# ② 필수 컬럼에 NaN 존재 여부
if submission_df.isnull().values.any():
raise ValueError("The submission dataframe contains missing values.")
# ③ pair 중복 여부
pairs = list(zip(submission_df["leading_item_id"], submission_df["following_item_id"]))
if len(pairs) != len(set(pairs)):
raise ValueError("The submission dataframe contains duplicate (leading_item_id, following_item_id) pairs.")
def comovement_f1(answer_df, submission_df):
"""공행성쌍 F1 계산"""
ans = answer_df[["leading_item_id", "following_item_id"]].copy()
sub = submission_df[["leading_item_id", "following_item_id"]].copy()
ans["pair"] = list(zip(ans["leading_item_id"], ans["following_item_id"]))
sub["pair"] = list(zip(sub["leading_item_id"], sub["following_item_id"]))
G = set(ans["pair"])
P = set(sub["pair"])
tp = len(G & P)
fp = len(P - G)
fn = len(G - P)
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0.0
return f1
def comovement_nmae(answer_df, submission_df, eps=1e-6):
"""
전체 U = G ∪ P에 대한 clipped NMAE 계산
"""
ans = answer_df[["leading_item_id", "following_item_id", "value"]].copy()
sub = submission_df[["leading_item_id", "following_item_id", "value"]].copy()
ans["pair"] = list(zip(ans["leading_item_id"], ans["following_item_id"]))
sub["pair"] = list(zip(sub["leading_item_id"], sub["following_item_id"]))
G = set(ans["pair"])
P = set(sub["pair"])
U = G | P
ans_val = dict(zip(ans["pair"], ans["value"]))
sub_val = dict(zip(sub["pair"], sub["value"]))
errors = []
for pair in U:
if pair in G and pair in P:
# 정수 변환(반올림)
y_true = int(round(float(ans_val[pair])))
y_pred = int(round(float(sub_val[pair])))
rel_err = abs(y_true - y_pred) / (abs(y_true) + eps)
rel_err = min(rel_err, 1.0) # 오차 100% 이상은 100%로 간주
else:
rel_err = 1.0 # FN, FP는 오차 100%
errors.append(rel_err)
return np.mean(errors) if errors else 1.0
def comovement_score(answer_df, submission_df):
_validate_input(answer_df, submission_df)
S1 = comovement_f1(answer_df, submission_df)
nmae_full = comovement_nmae(answer_df, submission_df, 1e-6)
S2 = 1 - nmae_full
score = 0.6 * S1 + 0.4 * S2
return score
데이콘(주) | 대표 김국진 | 699-81-01021
통신판매업 신고번호: 제 2021-서울영등포-1704호
직업정보제공사업 신고번호: J1204020250004
서울특별시 영등포구 은행로 3 익스콘벤처타워 901호
이메일 dacon@dacon.io |
전화번호: 070-4102-0545
Copyright ⓒ DACON Inc. All rights reserved