KL散度,提示越界,通过率66.67,找了半天没找出问题在哪里
from math import log
if __name__ == "__main__":
p_data = [int(i) for i in input().split()]
q_data = [int(i) for i in input().split()]
p_total = len(p_data) + 1e-5
q_total = len(q_data) + 1e-5
x_unique = []
for x in p_data + q_data:
if x not in x_unique:
x_unique.append(x)
p_stat = {x: 0 for x in x_unique}
q_stat = {x: 0 for x in x_unique}
for x in p_data:
p_stat[x] += 1
for x in q_data:
q_stat[x] += 1
kl_val = 0
for x in x_unique:
p_proba = p_stat[x] / p_total + 1e-5
q_proba = q_stat[x] / q_total + 1e-5
kl_val += p_proba * log(p_proba / q_proba, 2)
print("%.2f" % kl_val)